From ce6fa3b65223de6514227749771417e32e90b00a Mon Sep 17 00:00:00 2001 From: "JUST.in DO IT" Date: Wed, 12 Jun 2024 15:04:19 -0700 Subject: [PATCH] fix(sqllab): run previous state query (#29230) (cherry picked from commit a88979631e6abe4de1b00e9c05ad1e411db1c2a7) --- .../src/SqlLab/actions/sqlLab.js | 5 ++- .../src/SqlLab/actions/sqlLab.test.js | 39 ++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/superset-frontend/src/SqlLab/actions/sqlLab.js b/superset-frontend/src/SqlLab/actions/sqlLab.js index 540a0e85368dc..edc3098884b8a 100644 --- a/superset-frontend/src/SqlLab/actions/sqlLab.js +++ b/superset-frontend/src/SqlLab/actions/sqlLab.js @@ -133,11 +133,12 @@ export const convertQueryToClient = fieldConverter(queryClientMapping); export function getUpToDateQuery(rootState, queryEditor, key) { const { - sqlLab: { unsavedQueryEditor }, + sqlLab: { unsavedQueryEditor, queryEditors }, } = rootState; const id = key ?? queryEditor.id; return { - ...queryEditor, + id, + ...queryEditors.find(qe => qe.id === id), ...(id === unsavedQueryEditor.id && unsavedQueryEditor), }; } diff --git a/superset-frontend/src/SqlLab/actions/sqlLab.test.js b/superset-frontend/src/SqlLab/actions/sqlLab.test.js index 20fd53ad389c9..5bb42f7580811 100644 --- a/superset-frontend/src/SqlLab/actions/sqlLab.test.js +++ b/superset-frontend/src/SqlLab/actions/sqlLab.test.js @@ -36,6 +36,29 @@ import { const middlewares = [thunk]; const mockStore = configureMockStore(middlewares); +describe('getUpToDateQuery', () => { + test('should return the up to date query editor state', () => { + const outOfUpdatedQueryEditor = { + ...defaultQueryEditor, + schema: null, + sql: 'SELECT ...', + }; + const queryEditor = { + ...defaultQueryEditor, + sql: 'SELECT * FROM table', + }; + const state = { + sqlLab: { + queryEditors: [queryEditor], + unsavedQueryEditor: {}, + }, + }; + expect(actions.getUpToDateQuery(state, outOfUpdatedQueryEditor)).toEqual( + queryEditor, + ); + }); +}); + describe('async actions', () => { const mockBigNumber = '9223372036854775807'; const queryEditor = { @@ -675,7 +698,13 @@ describe('async actions', () => { it('updates the tab state in the backend', () => { expect.assertions(2); - const store = mockStore(initialState); + const store = mockStore({ + ...initialState, + sqlLab: { + ...initialState.sqlLab, + queryEditors: [queryEditor], + }, + }); const request = actions.queryEditorSetAndSaveSql(queryEditor, sql); return request(store.dispatch, store.getState).then(() => { expect(store.getActions()).toEqual(expectedActions); @@ -691,7 +720,13 @@ describe('async actions', () => { feature => !(feature === 'SQLLAB_BACKEND_PERSISTENCE'), ); - const store = mockStore(initialState); + const store = mockStore({ + ...initialState, + sqlLab: { + ...initialState.sqlLab, + queryEditors: [queryEditor], + }, + }); const request = actions.queryEditorSetAndSaveSql(queryEditor, sql); request(store.dispatch, store.getState);