fix(SIP-95): missing catalog cache key (#28415)

This commit is contained in:
JUST.in DO IT 2024-05-10 10:18:14 -07:00 committed by GitHub
parent 26df7b4ad5
commit 3a62eab163
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 61 additions and 14 deletions

View File

@ -18,7 +18,7 @@
*/
import React from 'react';
import fetchMock from 'fetch-mock';
import { render, screen, waitFor } from 'spec/helpers/testing-library';
import { render, screen, waitFor, within } from 'spec/helpers/testing-library';
import userEvent from '@testing-library/user-event';
import SqlEditorLeftBar, {
SqlEditorLeftBarProps,
@ -107,14 +107,11 @@ test('renders a TableElement', async () => {
});
test('table should be visible when expanded is true', async () => {
const { container, getByText, getByRole } = await renderAndWait(
mockedProps,
undefined,
{
const { container, getByText, getByRole, getAllByLabelText } =
await renderAndWait(mockedProps, undefined, {
...initialState,
sqlLab: { ...initialState.sqlLab, tables: [table] },
},
);
});
const dbSelect = getByRole('combobox', {
name: 'Select database or type to search databases',
@ -122,14 +119,16 @@ test('table should be visible when expanded is true', async () => {
const schemaSelect = getByRole('combobox', {
name: 'Select schema or type to search schemas',
});
const dropdown = getByText(/Select table/i);
const abUser = getByText(/ab_user/i);
const tableSelect = getAllByLabelText(
/Select table or type to search tables/i,
)[0];
const tableOption = within(tableSelect).getByText(/ab_user/i);
expect(getByText(/Database/i)).toBeInTheDocument();
expect(dbSelect).toBeInTheDocument();
expect(schemaSelect).toBeInTheDocument();
expect(dropdown).toBeInTheDocument();
expect(abUser).toBeInTheDocument();
expect(tableSelect).toBeInTheDocument();
expect(tableOption).toBeInTheDocument();
expect(
container.querySelector('.ant-collapse-content-active'),
).toBeInTheDocument();

View File

@ -278,7 +278,7 @@ export default function DatabaseSelector({
isFetching: loadingCatalogs,
refetch: refetchCatalogs,
} = useCatalogs({
dbId: currentDb?.value,
dbId: showCatalogSelector ? currentDb?.value : undefined,
onSuccess: (catalogs, isFetched) => {
if (!showCatalogSelector) {
changeCatalog(null);

View File

@ -32,6 +32,9 @@ const fakeApiResult = {
const fakeApiResult2 = {
result: ['test schema 2', 'test schema a'],
};
const fakeApiResult3 = {
result: ['test schema 3', 'test schema c'],
};
const expectedResult = fakeApiResult.result.map((value: string) => ({
value,
@ -43,6 +46,11 @@ const expectedResult2 = fakeApiResult2.result.map((value: string) => ({
label: value,
title: value,
}));
const expectedResult3 = fakeApiResult3.result.map((value: string) => ({
value,
label: value,
title: value,
}));
describe('useSchemas hook', () => {
afterEach(() => {
@ -119,7 +127,7 @@ describe('useSchemas hook', () => {
expect(fetchMock.calls(schemaApiRoute).length).toBe(1);
});
it('returns refreshed data after expires', async () => {
test('returns refreshed data after expires', async () => {
const expectDbId = 'db1';
const schemaApiRoute = `glob:*/api/v1/database/*/schemas/*`;
fetchMock.get(schemaApiRoute, url =>
@ -164,4 +172,43 @@ describe('useSchemas hook', () => {
expect(fetchMock.calls(schemaApiRoute)[2][0]).toContain(expectDbId);
await waitFor(() => expect(result.current.data).toEqual(expectedResult));
});
test('returns correct schema list by a catalog', async () => {
const dbId = '1';
const expectCatalog = 'catalog3';
const schemaApiRoute = `glob:*/api/v1/database/*/schemas/*`;
fetchMock.get(schemaApiRoute, url =>
url.includes(`catalog:${expectCatalog}`)
? fakeApiResult3
: fakeApiResult2,
);
const onSuccess = jest.fn();
const { result, rerender, waitFor } = renderHook(
({ dbId, catalog }) =>
useSchemas({
dbId,
catalog,
onSuccess,
}),
{
initialProps: { dbId, catalog: expectCatalog },
wrapper: createWrapper({
useRedux: true,
store,
}),
},
);
await waitFor(() => expect(fetchMock.calls(schemaApiRoute).length).toBe(1));
expect(result.current.data).toEqual(expectedResult3);
expect(onSuccess).toHaveBeenCalledTimes(2);
rerender({ dbId, catalog: 'catalog2' });
await waitFor(() => expect(fetchMock.calls(schemaApiRoute).length).toBe(2));
expect(result.current.data).toEqual(expectedResult2);
rerender({ dbId, catalog: expectCatalog });
expect(result.current.data).toEqual(expectedResult3);
expect(fetchMock.calls(schemaApiRoute).length).toBe(2);
});
});

View File

@ -54,8 +54,9 @@ const schemaApi = api.injectEndpoints({
title: value,
})),
}),
serializeQueryArgs: ({ queryArgs: { dbId } }) => ({
serializeQueryArgs: ({ queryArgs: { dbId, catalog } }) => ({
dbId,
catalog,
}),
}),
}),