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.
|
|
|
|
*/
|
2016-10-25 19:44:32 -04:00
|
|
|
import React from 'react';
|
2017-06-22 13:31:13 -04:00
|
|
|
import configureStore from 'redux-mock-store';
|
|
|
|
import thunk from 'redux-thunk';
|
|
|
|
import URI from 'urijs';
|
2020-12-09 14:58:42 -05:00
|
|
|
import { Provider } from 'react-redux';
|
2017-06-22 13:31:13 -04:00
|
|
|
import { shallow, mount } from 'enzyme';
|
|
|
|
import sinon from 'sinon';
|
2021-01-26 02:34:05 -05:00
|
|
|
import { act } from 'react-dom/test-utils';
|
|
|
|
import fetchMock from 'fetch-mock';
|
2020-09-09 16:18:55 -04:00
|
|
|
import { supersetTheme, ThemeProvider } from '@superset-ui/core';
|
2021-04-27 03:15:21 -04:00
|
|
|
import { EditableTabs } from 'src/components/Tabs';
|
2020-05-07 17:18:10 -04:00
|
|
|
import TabbedSqlEditors from 'src/SqlLab/components/TabbedSqlEditors';
|
|
|
|
import SqlEditor from 'src/SqlLab/components/SqlEditor';
|
2017-04-13 18:04:09 -04:00
|
|
|
|
2017-06-22 13:31:13 -04:00
|
|
|
import { table, initialState } from './fixtures';
|
2016-10-25 19:44:32 -04:00
|
|
|
|
2021-01-26 02:34:05 -05:00
|
|
|
fetchMock.get('glob:*/api/v1/database/*', {});
|
|
|
|
fetchMock.get('glob:*/savedqueryviewapi/api/get/*', {});
|
|
|
|
fetchMock.get('glob:*/kv/*', {});
|
|
|
|
|
2016-10-25 19:44:32 -04:00
|
|
|
describe('TabbedSqlEditors', () => {
|
2017-06-22 13:31:13 -04:00
|
|
|
const middlewares = [thunk];
|
|
|
|
const mockStore = configureStore(middlewares);
|
|
|
|
const store = mockStore(initialState);
|
|
|
|
|
2019-11-27 17:02:48 -05:00
|
|
|
const tabHistory = ['dfsadfs', 'newEditorId'];
|
|
|
|
|
|
|
|
const tables = [
|
2020-04-03 20:05:16 -04:00
|
|
|
{ ...table, dataPreviewQueryId: 'B1-VQU1zW', queryEditorId: 'newEditorId' },
|
2017-06-22 13:31:13 -04:00
|
|
|
];
|
2018-07-12 14:50:25 -04:00
|
|
|
|
2019-11-27 17:02:48 -05:00
|
|
|
const queryEditors = [
|
|
|
|
{
|
|
|
|
autorun: false,
|
|
|
|
dbId: 1,
|
|
|
|
id: 'newEditorId',
|
|
|
|
latestQueryId: 'B1-VQU1zW',
|
|
|
|
schema: null,
|
|
|
|
selectedText: null,
|
|
|
|
sql: 'SELECT ds...',
|
|
|
|
title: 'Untitled Query',
|
|
|
|
},
|
|
|
|
];
|
2017-06-22 13:31:13 -04:00
|
|
|
const queries = {
|
|
|
|
'B1-VQU1zW': {
|
|
|
|
id: 'B1-VQU1zW',
|
|
|
|
sqlEditorId: 'newEditorId',
|
2019-11-14 12:44:57 -05:00
|
|
|
tableName: 'ab_user',
|
2021-01-26 02:34:05 -05:00
|
|
|
state: 'success',
|
2017-06-22 13:31:13 -04:00
|
|
|
},
|
|
|
|
};
|
2016-10-25 19:44:32 -04:00
|
|
|
const mockedProps = {
|
|
|
|
actions: {},
|
|
|
|
databases: {},
|
|
|
|
tables: [],
|
|
|
|
queries: {},
|
2018-07-12 14:50:25 -04:00
|
|
|
queryEditors: initialState.sqlLab.queryEditors,
|
|
|
|
tabHistory: initialState.sqlLab.tabHistory,
|
2017-04-28 20:56:29 -04:00
|
|
|
editorHeight: '',
|
2019-11-27 17:02:48 -05:00
|
|
|
getHeight: () => '100px',
|
2018-03-09 17:54:39 -05:00
|
|
|
database: {},
|
2018-11-07 18:57:44 -05:00
|
|
|
defaultQueryLimit: 1000,
|
|
|
|
maxRow: 100000,
|
2016-10-25 19:44:32 -04:00
|
|
|
};
|
2019-11-27 17:02:48 -05:00
|
|
|
const getWrapper = () =>
|
2020-12-09 14:58:42 -05:00
|
|
|
shallow(<TabbedSqlEditors store={store} {...mockedProps} />)
|
|
|
|
.dive()
|
|
|
|
.dive();
|
2017-06-22 13:31:13 -04:00
|
|
|
|
2021-01-26 02:34:05 -05:00
|
|
|
const mountWithAct = async () =>
|
|
|
|
act(async () => {
|
|
|
|
mount(
|
|
|
|
<Provider store={store}>
|
|
|
|
<TabbedSqlEditors {...mockedProps} />
|
|
|
|
</Provider>,
|
|
|
|
{
|
|
|
|
wrappingComponent: ThemeProvider,
|
|
|
|
wrappingComponentProps: { theme: supersetTheme },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-06-22 13:31:13 -04:00
|
|
|
let wrapper;
|
2016-10-25 19:44:32 -04:00
|
|
|
it('is valid', () => {
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(React.isValidElement(<TabbedSqlEditors {...mockedProps} />)).toBe(
|
|
|
|
true,
|
|
|
|
);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
2017-06-22 13:31:13 -04:00
|
|
|
describe('componentDidMount', () => {
|
|
|
|
let uriStub;
|
|
|
|
beforeEach(() => {
|
|
|
|
sinon.stub(window.history, 'replaceState');
|
|
|
|
uriStub = sinon.stub(URI.prototype, 'search');
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
window.history.replaceState.restore();
|
|
|
|
uriStub.restore();
|
|
|
|
});
|
2021-01-26 02:34:05 -05:00
|
|
|
it('should handle id', async () => {
|
2017-06-22 13:31:13 -04:00
|
|
|
uriStub.returns({ id: 1 });
|
2021-01-26 02:34:05 -05:00
|
|
|
await mountWithAct();
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(window.history.replaceState.getCall(0).args[2]).toBe(
|
|
|
|
'/superset/sqllab',
|
|
|
|
);
|
2017-06-22 13:31:13 -04:00
|
|
|
});
|
2021-01-26 02:34:05 -05:00
|
|
|
it('should handle savedQueryId', async () => {
|
2017-06-22 13:31:13 -04:00
|
|
|
uriStub.returns({ savedQueryId: 1 });
|
2021-01-26 02:34:05 -05:00
|
|
|
await mountWithAct();
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(window.history.replaceState.getCall(0).args[2]).toBe(
|
|
|
|
'/superset/sqllab',
|
|
|
|
);
|
2017-06-22 13:31:13 -04:00
|
|
|
});
|
2021-01-26 02:34:05 -05:00
|
|
|
it('should handle sql', async () => {
|
2017-06-22 13:31:13 -04:00
|
|
|
uriStub.returns({ sql: 1, dbid: 1 });
|
2021-01-26 02:34:05 -05:00
|
|
|
await mountWithAct();
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(window.history.replaceState.getCall(0).args[2]).toBe(
|
|
|
|
'/superset/sqllab',
|
|
|
|
);
|
2017-06-22 13:31:13 -04:00
|
|
|
});
|
|
|
|
});
|
2020-08-17 00:29:08 -04:00
|
|
|
describe('UNSAFE_componentWillReceiveProps', () => {
|
2017-06-22 13:31:13 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = getWrapper();
|
|
|
|
wrapper.setProps({ queryEditors, queries, tabHistory, tables });
|
|
|
|
});
|
|
|
|
it('should update queriesArray and dataPreviewQueries', () => {
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(wrapper.state().queriesArray.slice(-1)[0]).toBe(
|
|
|
|
queries['B1-VQU1zW'],
|
|
|
|
);
|
|
|
|
expect(wrapper.state().dataPreviewQueries.slice(-1)[0]).toEqual(
|
|
|
|
queries['B1-VQU1zW'],
|
|
|
|
);
|
2017-06-22 13:31:13 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should rename Tab', () => {
|
2019-11-27 17:02:48 -05:00
|
|
|
global.prompt = () => 'new title';
|
2017-06-22 13:31:13 -04:00
|
|
|
wrapper = getWrapper();
|
|
|
|
sinon.stub(wrapper.instance().props.actions, 'queryEditorSetTitle');
|
|
|
|
|
|
|
|
wrapper.instance().renameTab(queryEditors[0]);
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(
|
|
|
|
wrapper.instance().props.actions.queryEditorSetTitle.getCall(0).args[1],
|
|
|
|
).toBe('new title');
|
2017-06-22 13:31:13 -04:00
|
|
|
|
|
|
|
delete global.prompt;
|
|
|
|
});
|
|
|
|
it('should removeQueryEditor', () => {
|
|
|
|
wrapper = getWrapper();
|
|
|
|
sinon.stub(wrapper.instance().props.actions, 'removeQueryEditor');
|
|
|
|
|
|
|
|
wrapper.instance().removeQueryEditor(queryEditors[0]);
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(
|
|
|
|
wrapper.instance().props.actions.removeQueryEditor.getCall(0).args[0],
|
|
|
|
).toBe(queryEditors[0]);
|
2017-06-22 13:31:13 -04:00
|
|
|
});
|
|
|
|
it('should add new query editor', () => {
|
|
|
|
wrapper = getWrapper();
|
|
|
|
sinon.stub(wrapper.instance().props.actions, 'addQueryEditor');
|
|
|
|
|
|
|
|
wrapper.instance().newQueryEditor();
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(
|
|
|
|
wrapper.instance().props.actions.addQueryEditor.getCall(0).args[0].title,
|
|
|
|
).toContain('Untitled Query');
|
2017-06-22 13:31:13 -04:00
|
|
|
});
|
2019-11-04 12:05:10 -05:00
|
|
|
it('should duplicate query editor', () => {
|
|
|
|
wrapper = getWrapper();
|
|
|
|
sinon.stub(wrapper.instance().props.actions, 'cloneQueryToNewTab');
|
|
|
|
|
|
|
|
wrapper.instance().duplicateQueryEditor(queryEditors[0]);
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(
|
|
|
|
wrapper.instance().props.actions.cloneQueryToNewTab.getCall(0).args[0],
|
|
|
|
).toBe(queryEditors[0]);
|
2019-11-04 12:05:10 -05:00
|
|
|
});
|
2017-06-22 13:31:13 -04:00
|
|
|
it('should handle select', () => {
|
2019-11-14 12:44:57 -05:00
|
|
|
const mockEvent = {
|
|
|
|
target: {
|
|
|
|
getAttribute: () => null,
|
|
|
|
},
|
|
|
|
};
|
2017-06-22 13:31:13 -04:00
|
|
|
wrapper = getWrapper();
|
2019-11-14 12:44:57 -05:00
|
|
|
sinon.stub(wrapper.instance().props.actions, 'switchQueryEditor');
|
2017-06-22 13:31:13 -04:00
|
|
|
|
2019-11-14 12:44:57 -05:00
|
|
|
// cannot switch to current tab, switchQueryEditor never gets called
|
|
|
|
wrapper.instance().handleSelect('dfsadfs', mockEvent);
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(
|
|
|
|
wrapper.instance().props.actions.switchQueryEditor.callCount,
|
|
|
|
).toEqual(0);
|
2020-10-10 02:11:25 -04:00
|
|
|
});
|
|
|
|
it('should handle add tab', () => {
|
|
|
|
wrapper = getWrapper();
|
|
|
|
sinon.spy(wrapper.instance(), 'newQueryEditor');
|
|
|
|
|
|
|
|
wrapper.instance().handleEdit('1', 'add');
|
|
|
|
expect(wrapper.instance().newQueryEditor.callCount).toBe(1);
|
2017-06-22 13:31:13 -04:00
|
|
|
wrapper.instance().newQueryEditor.restore();
|
|
|
|
});
|
|
|
|
it('should render', () => {
|
|
|
|
wrapper = getWrapper();
|
|
|
|
wrapper.setState({ hideLeftBar: true });
|
|
|
|
|
2020-10-10 02:11:25 -04:00
|
|
|
const firstTab = wrapper.find(EditableTabs.TabPane).first();
|
|
|
|
expect(firstTab.props()['data-key']).toContain(
|
2019-11-27 17:02:48 -05:00
|
|
|
initialState.sqlLab.queryEditors[0].id,
|
|
|
|
);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(firstTab.find(SqlEditor)).toHaveLength(1);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
2018-10-22 21:36:07 -04:00
|
|
|
it('should disable new tab when offline', () => {
|
|
|
|
wrapper = getWrapper();
|
2020-10-10 02:11:25 -04:00
|
|
|
expect(wrapper.find(EditableTabs).props().hideAdd).toBe(false);
|
2018-10-22 21:36:07 -04:00
|
|
|
wrapper.setProps({ offline: true });
|
2020-10-10 02:11:25 -04:00
|
|
|
expect(wrapper.find(EditableTabs).props().hideAdd).toBe(true);
|
2018-10-22 21:36:07 -04:00
|
|
|
});
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|