sadpandajoe commented on code in PR #38860: URL: https://github.com/apache/superset/pull/38860#discussion_r2991688937
########## superset-frontend/src/core/sqlLab/sqlLab.test.ts: ########## @@ -0,0 +1,570 @@ +/** + * 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. + */ +import { + configureStore, + createListenerMiddleware, +} from '@reduxjs/toolkit'; +import type { QueryEditor } from 'src/SqlLab/types'; +import sqlLabReducer from 'src/SqlLab/reducers/sqlLab'; +import { + START_QUERY, + QUERY_SUCCESS, + STOP_QUERY, + QUERY_FAILED, + QUERY_EDITOR_SETDB, + QUERY_EDITOR_SET_SCHEMA, + QUERY_EDITOR_SET_TITLE, + SET_ACTIVE_SOUTHPANE_TAB, + REMOVE_QUERY_EDITOR, + SET_ACTIVE_QUERY_EDITOR, + ADD_QUERY_EDITOR, +} from 'src/SqlLab/actions/sqlLab'; + +// --------------------------------------------------------------------------- +// Constants +// --------------------------------------------------------------------------- + +const EDITOR_ID = 'editor-1'; +const IMMUTABLE_ID = 'immutable-1'; + +// --------------------------------------------------------------------------- +// Test infrastructure — real reducer + real listener middleware +// --------------------------------------------------------------------------- + +const mockListenerMiddleware = createListenerMiddleware(); + +function makeInitialSqlLabState() { + return { + queryEditors: [ + { + id: EDITOR_ID, + immutableId: IMMUTABLE_ID, + name: 'Untitled Query 1', + dbId: 1, + catalog: null, + schema: 'public', + sql: 'SELECT 1', + autorun: false, + remoteId: null, + loaded: true, + inLocalStorage: true, + } as unknown as QueryEditor, + ], + tabHistory: [EDITOR_ID], + unsavedQueryEditor: {} as Record<string, unknown>, + queries: {} as Record<string, unknown>, + tables: [] as unknown[], + databases: { + 1: { id: 1, database_name: 'test_db', allow_run_async: false }, + } as Record<string, unknown>, + activeSouthPaneTab: 'Results' as string | number, + queriesLastUpdate: 0, + errorMessage: null as string | null, + alerts: [] as unknown[], + dbConnect: false, + offline: false, + editorTabLastUpdatedAt: 0, + lastUpdatedActiveTab: EDITOR_ID, + destroyedQueryEditors: {} as Record<string, number>, + }; +} + +function createMockStore() { + return configureStore({ + reducer: { sqlLab: sqlLabReducer }, + middleware: getDefault => + getDefault({ serializableCheck: false, immutableCheck: false }).prepend( + mockListenerMiddleware.middleware, + ), + preloadedState: { sqlLab: makeInitialSqlLabState() as any }, + }); +} + +let mockStore = createMockStore(); + +// --------------------------------------------------------------------------- +// Mocks — must use "mock" prefix for jest.mock() scoping rules +// --------------------------------------------------------------------------- + +jest.mock('nanoid', () => ({ + nanoid: jest.fn(() => 'mock-nanoid'), +})); Review Comment: Updated the mock to use an incrementing counter so each `nanoid()` call returns a unique value (`mock-anoid-1`, `mock-nanoid-2`, ...). This prevents `id === immutableId` collisions and keeps future multi-`createTab` tests safe. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
