Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sqllab): async query broken due to #21320 #21667

Merged
merged 4 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const RunQueryActionButton = ({
return (
<StyledButton>
<ButtonComponent
data-test="run-query-action"
onClick={() =>
onClick(shouldShowStopBtn, allowAsync, runQuery, stopQuery)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
import React from 'react';
import { mount } from 'enzyme';
import { render } from 'spec/helpers/testing-library';
import { fireEvent, render, waitFor } from 'spec/helpers/testing-library';
import { supersetTheme, ThemeProvider } from '@superset-ui/core';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
Expand All @@ -33,7 +33,6 @@ import AceEditorWrapper from 'src/SqlLab/components/AceEditorWrapper';
import ConnectedSouthPane from 'src/SqlLab/components/SouthPane/state';
import SqlEditor from 'src/SqlLab/components/SqlEditor';
import QueryProvider from 'src/views/QueryProvider';
import SqlEditorLeftBar from 'src/SqlLab/components/SqlEditorLeftBar';
import { AntdDropdown } from 'src/components';
import {
queryEditorSetFunctionNames,
Expand All @@ -55,10 +54,15 @@ jest.mock('src/components/AsyncAceEditor', () => ({
<div data-test="react-ace">{JSON.stringify(props)}</div>
),
}));
jest.mock('src/SqlLab/components/SqlEditorLeftBar', () => () => (
<div data-test="mock-sql-editor-left-bar" />
));

const MOCKED_SQL_EDITOR_HEIGHT = 500;

fetchMock.get('glob:*/api/v1/database/*', { result: [] });
fetchMock.get('glob:*/superset/tables/*', { options: [] });
fetchMock.post('glob:*/sql_json/*', { result: [] });

const middlewares = [thunk];
const mockStore = configureStore(middlewares);
Expand Down Expand Up @@ -134,9 +138,10 @@ describe('SqlEditor', () => {
});

it('render a SqlEditorLeftBar', async () => {
const wrapper = buildWrapper();
await waitForComponentToPaint(wrapper);
expect(wrapper.find(SqlEditorLeftBar)).toExist();
const { getByTestId } = setup(mockedProps, store);
await waitFor(() =>
expect(getByTestId('mock-sql-editor-left-bar')).toBeInTheDocument(),
);
});

it('render an AceEditorWrapper', async () => {
Expand Down Expand Up @@ -187,6 +192,46 @@ describe('SqlEditor', () => {
expect(wrapper.find(ConnectedSouthPane)).toExist();
});

it('runs query action with ctas false', async () => {
const expectedStore = mockStore({
...initialState,
sqlLab: {
...initialState.sqlLab,
databases: {
5667: {
allow_ctas: false,
allow_cvas: false,
allow_dml: false,
allow_file_upload: false,
allow_run_async: true,
backend: 'postgresql',
database_name: 'examples',
expose_in_sqllab: true,
force_ctas_schema: null,
id: 1,
},
},
unsavedQueryEditor: {
id: defaultQueryEditor.id,
dbId: 5667,
sql: 'expectedSql',
},
},
});
const { findByTestId } = setup(mockedProps, expectedStore);
const runButton = await findByTestId('run-query-action');
fireEvent.click(runButton);
await waitFor(() =>
expect(expectedStore.getActions()).toContainEqual({
type: 'START_QUERY',
query: expect.objectContaining({
ctas: false,
sqlEditorId: defaultQueryEditor.id,
}),
}),
);
});

// TODO eschutho convert tests to RTL
// eslint-disable-next-line jest/no-disabled-tests
it.skip('does not overflow the editor window', async () => {
Expand Down
12 changes: 9 additions & 3 deletions superset-frontend/src/SqlLab/components/SqlEditor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,19 @@ const SqlEditor = ({
}
};

useState(() => {
const runQuery = () => {
if (database) {
startQuery();
}
};

useEffect(() => {
if (autorun) {
setAutorun(false);
dispatch(queryEditorSetAutorun(queryEditor, false));
startQuery();
}
});
}, []);

// One layer of abstraction for easy spying in unit tests
const getSqlEditorHeight = () =>
Expand Down Expand Up @@ -542,7 +548,7 @@ const SqlEditor = ({
allowAsync={database ? database.allow_run_async : false}
queryEditor={queryEditor}
queryState={latestQuery?.state}
runQuery={startQuery}
runQuery={runQuery}
stopQuery={stopQuery}
overlayCreateAsMenu={showMenu ? runMenuBtn : null}
/>
Expand Down