Skip to content

Commit

Permalink
fix: queryEditor bug (apache#16452)
Browse files Browse the repository at this point in the history
* queryEditor bug

* update tests

Co-authored-by: Elizabeth Thompson <eschutho@gmail.com>
(cherry picked from commit ee2eccd)
  • Loading branch information
AAfghahi authored and Steven Uray committed Aug 31, 2021
1 parent 9068dd3 commit f8212ad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
40 changes: 30 additions & 10 deletions superset-frontend/spec/javascripts/sqllab/actions/sqlLab_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,15 @@ describe('async actions', () => {

let isFeatureEnabledMock;

beforeAll(() => {
beforeEach(() => {
isFeatureEnabledMock = jest
.spyOn(featureFlags, 'isFeatureEnabled')
.mockImplementation(
feature => feature === 'SQLLAB_BACKEND_PERSISTENCE',
);
});

afterAll(() => {
afterEach(() => {
isFeatureEnabledMock.mockRestore();
});

Expand Down Expand Up @@ -612,9 +612,29 @@ describe('async actions', () => {
});

describe('queryEditorSetSql', () => {
describe('with backend persistence flag on', () => {
it('does not update the tab state in the backend', () => {
expect.assertions(2);

const sql = 'SELECT * ';
const store = mockStore({});

return store
.dispatch(actions.queryEditorSetSql(queryEditor, sql))
.then(() => {
expect(store.getActions()).toHaveLength(0);
expect(fetchMock.calls(updateTabStateEndpoint)).toHaveLength(1);
});
});
});
});
describe('with backend persistence flag off', () => {
it('updates the tab state in the backend', () => {
expect.assertions(2);

const backendPersistenceOffMock = jest
.spyOn(featureFlags, 'isFeatureEnabled')
.mockImplementation(
feature => !(feature === 'SQLLAB_BACKEND_PERSISTENCE'),
);
const sql = 'SELECT * ';
const store = mockStore({});
const expectedActions = [
Expand All @@ -624,12 +644,12 @@ describe('async actions', () => {
sql,
},
];
return store
.dispatch(actions.queryEditorSetSql(queryEditor, sql))
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
expect(fetchMock.calls(updateTabStateEndpoint)).toHaveLength(1);
});

store.dispatch(actions.queryEditorSetSql(queryEditor, sql));

expect(store.getActions()).toEqual(expectedActions);
expect(fetchMock.calls(updateTabStateEndpoint)).toHaveLength(0);
backendPersistenceOffMock.mockRestore();
});
});

Expand Down
17 changes: 7 additions & 10 deletions superset-frontend/src/SqlLab/actions/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,16 +898,11 @@ export function updateSavedQuery(query) {

export function queryEditorSetSql(queryEditor, sql) {
return function (dispatch) {
const sync = isFeatureEnabled(FeatureFlag.SQLLAB_BACKEND_PERSISTENCE)
? SupersetClient.put({
endpoint: encodeURI(`/tabstateview/${queryEditor.id}`),
postPayload: { sql, latest_query_id: queryEditor.latestQueryId },
})
: Promise.resolve();

return sync
.then(() => dispatch({ type: QUERY_EDITOR_SET_SQL, queryEditor, sql }))
.catch(() =>
if (isFeatureEnabled(FeatureFlag.SQLLAB_BACKEND_PERSISTENCE)) {
return SupersetClient.put({
endpoint: encodeURI(`/tabstateview/${queryEditor.id}`),
postPayload: { sql, latest_query_id: queryEditor.latestQueryId },
}).catch(() =>
dispatch(
addDangerToast(
t(
Expand All @@ -918,6 +913,8 @@ export function queryEditorSetSql(queryEditor, sql) {
),
),
);
}
return dispatch({ type: QUERY_EDITOR_SET_SQL, queryEditor, sql });
};
}

Expand Down

0 comments on commit f8212ad

Please sign in to comment.