2019-01-15 18:53:27 -05:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2018-10-24 22:41:32 -04:00
|
|
|
import React from 'react';
|
|
|
|
import configureStore from 'redux-mock-store';
|
|
|
|
import thunk from 'redux-thunk';
|
|
|
|
import fetchMock from 'fetch-mock';
|
2020-02-19 12:51:50 -05:00
|
|
|
import * as featureFlags from 'src/featureFlags';
|
2018-10-24 22:41:32 -04:00
|
|
|
import { shallow } from 'enzyme';
|
|
|
|
|
2020-05-07 17:18:10 -04:00
|
|
|
import * as utils from 'src/utils/common';
|
|
|
|
import ShareSqlLabQuery from 'src/SqlLab/components/ShareSqlLabQuery';
|
2021-01-07 17:04:43 -05:00
|
|
|
import CopyToClipboard from 'src/components/CopyToClipboard';
|
|
|
|
import { Tooltip } from 'src/common/components/Tooltip';
|
2018-10-24 22:41:32 -04:00
|
|
|
|
|
|
|
const mockStore = configureStore([thunk]);
|
|
|
|
const store = mockStore();
|
2020-02-19 12:51:50 -05:00
|
|
|
let isFeatureEnabledMock;
|
2018-10-24 22:41:32 -04:00
|
|
|
|
2021-01-07 17:04:43 -05:00
|
|
|
const clipboardSpy = jest.fn();
|
|
|
|
|
|
|
|
describe('ShareSqlLabQuery', () => {
|
2018-10-24 22:41:32 -04:00
|
|
|
const storeQueryUrl = 'glob:*/kv/store/';
|
|
|
|
const storeQueryMockId = '123';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
fetchMock.post(storeQueryUrl, () => ({ id: storeQueryMockId }), {
|
|
|
|
overwriteRoutes: true,
|
|
|
|
});
|
|
|
|
fetchMock.resetHistory();
|
2021-01-07 17:04:43 -05:00
|
|
|
jest.clearAllMocks();
|
2018-10-24 22:41:32 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(fetchMock.reset);
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
queryEditor: {
|
|
|
|
dbId: 0,
|
|
|
|
title: 'query title',
|
|
|
|
schema: 'query_schema',
|
|
|
|
autorun: false,
|
|
|
|
sql: 'SELECT * FROM ...',
|
2020-02-19 12:51:50 -05:00
|
|
|
remoteId: 999,
|
2018-10-24 22:41:32 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-02-19 12:51:50 -05:00
|
|
|
const storedQueryAttributes = {
|
|
|
|
dbId: 0,
|
|
|
|
title: 'query title',
|
|
|
|
schema: 'query_schema',
|
|
|
|
autorun: false,
|
|
|
|
sql: 'SELECT * FROM ...',
|
|
|
|
};
|
|
|
|
|
2018-10-24 22:41:32 -04:00
|
|
|
function setup(overrideProps) {
|
2019-11-27 17:02:48 -05:00
|
|
|
const wrapper = shallow(
|
2020-12-09 14:58:42 -05:00
|
|
|
<ShareSqlLabQuery store={store} {...defaultProps} {...overrideProps} />,
|
2019-11-27 17:02:48 -05:00
|
|
|
).dive(); // wrapped in withToasts HOC
|
2018-10-24 22:41:32 -04:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
2020-02-19 12:51:50 -05:00
|
|
|
describe('via /kv/store', () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
isFeatureEnabledMock = jest
|
|
|
|
.spyOn(featureFlags, 'isFeatureEnabled')
|
|
|
|
.mockImplementation(() => true);
|
|
|
|
});
|
2018-10-24 22:41:32 -04:00
|
|
|
|
2020-02-19 12:51:50 -05:00
|
|
|
afterAll(() => {
|
|
|
|
isFeatureEnabledMock.restore();
|
|
|
|
});
|
2018-10-24 22:41:32 -04:00
|
|
|
|
2021-01-07 17:04:43 -05:00
|
|
|
it('calls storeQuery() with the query when getCopyUrl() is called', () => {
|
2020-02-19 12:51:50 -05:00
|
|
|
expect.assertions(4);
|
|
|
|
const storeQuerySpy = jest.spyOn(utils, 'storeQuery');
|
2018-10-24 22:41:32 -04:00
|
|
|
|
2020-02-19 12:51:50 -05:00
|
|
|
const wrapper = setup();
|
|
|
|
const instance = wrapper.instance();
|
2018-10-24 22:41:32 -04:00
|
|
|
|
2021-01-07 17:04:43 -05:00
|
|
|
return instance.getCopyUrl(clipboardSpy).then(() => {
|
2020-02-19 12:51:50 -05:00
|
|
|
expect(storeQuerySpy.mock.calls).toHaveLength(1);
|
2018-10-24 22:41:32 -04:00
|
|
|
expect(fetchMock.calls(storeQueryUrl)).toHaveLength(1);
|
2020-02-19 12:51:50 -05:00
|
|
|
expect(storeQuerySpy.mock.calls[0][0]).toMatchObject(
|
|
|
|
storedQueryAttributes,
|
|
|
|
);
|
2021-01-07 17:04:43 -05:00
|
|
|
expect(clipboardSpy).toHaveBeenCalledWith(
|
|
|
|
expect.stringContaining('?id='),
|
|
|
|
);
|
2020-02-19 12:51:50 -05:00
|
|
|
|
|
|
|
storeQuerySpy.mockRestore();
|
2018-10-24 22:41:32 -04:00
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
2020-02-19 12:51:50 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches an error toast upon fetching failure', () => {
|
|
|
|
expect.assertions(3);
|
2021-01-07 17:04:43 -05:00
|
|
|
const error = 'There was an error with your request';
|
2020-02-19 12:51:50 -05:00
|
|
|
const addDangerToastSpy = jest.fn();
|
|
|
|
fetchMock.post(
|
|
|
|
storeQueryUrl,
|
|
|
|
{ throws: error },
|
|
|
|
{ overwriteRoutes: true },
|
|
|
|
);
|
|
|
|
const wrapper = setup();
|
|
|
|
wrapper.setProps({ addDangerToast: addDangerToastSpy });
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
.instance()
|
2021-01-07 17:04:43 -05:00
|
|
|
.getCopyUrl(clipboardSpy)
|
2020-02-19 12:51:50 -05:00
|
|
|
.then(() => {
|
2020-04-21 14:36:01 -04:00
|
|
|
// Fails then retries thrice
|
|
|
|
expect(fetchMock.calls(storeQueryUrl)).toHaveLength(4);
|
2020-02-19 12:51:50 -05:00
|
|
|
expect(addDangerToastSpy.mock.calls).toHaveLength(1);
|
|
|
|
expect(addDangerToastSpy.mock.calls[0][0]).toBe(error);
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('via saved query', () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
isFeatureEnabledMock = jest
|
|
|
|
.spyOn(featureFlags, 'isFeatureEnabled')
|
|
|
|
.mockImplementation(() => false);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
isFeatureEnabledMock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not call storeQuery() with the query when getCopyUrl() is called', () => {
|
|
|
|
const storeQuerySpy = jest.spyOn(utils, 'storeQuery');
|
|
|
|
|
|
|
|
const wrapper = setup();
|
|
|
|
const instance = wrapper.instance();
|
|
|
|
|
2021-01-07 17:04:43 -05:00
|
|
|
instance.getCopyUrl(clipboardSpy);
|
2020-02-19 12:51:50 -05:00
|
|
|
|
|
|
|
expect(storeQuerySpy.mock.calls).toHaveLength(0);
|
|
|
|
expect(fetchMock.calls(storeQueryUrl)).toHaveLength(0);
|
2021-01-07 17:04:43 -05:00
|
|
|
expect(clipboardSpy).toHaveBeenCalledWith(
|
|
|
|
expect.stringContaining('savedQueryId'),
|
|
|
|
);
|
2020-02-19 12:51:50 -05:00
|
|
|
|
|
|
|
storeQuerySpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows a request to save the query when the query is not yet saved', () => {
|
2021-01-07 17:04:43 -05:00
|
|
|
const wrapper = setup({
|
|
|
|
queryEditor: {
|
|
|
|
...defaultProps.queryEditor,
|
|
|
|
remoteId: undefined,
|
|
|
|
},
|
|
|
|
});
|
2020-02-19 12:51:50 -05:00
|
|
|
|
2021-01-07 17:04:43 -05:00
|
|
|
expect(wrapper.find(CopyToClipboard)).toHaveLength(0);
|
|
|
|
expect(wrapper.find('.btn-disabled')).toHaveLength(1);
|
|
|
|
expect(wrapper.find(Tooltip)).toHaveProp(
|
|
|
|
'title',
|
|
|
|
expect.stringContaining('Save the query'),
|
|
|
|
);
|
2020-02-19 12:51:50 -05:00
|
|
|
});
|
2018-10-24 22:41:32 -04:00
|
|
|
});
|
|
|
|
});
|