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: Address regression introduced in #21284 #21470

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion superset-frontend/src/components/TableSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ const TableSelector: FunctionComponent<TableSelectorProps> = ({
? data.options.map(table => ({
value: table.value,
label: <TableOption table={table} />,
text: table.label,
text: table.value,
}))
: [],
[data],
Expand Down
31 changes: 2 additions & 29 deletions superset-frontend/src/hooks/apiResources/tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,46 +103,19 @@ describe('useTables hook', () => {
});
expect(SupersetClient.get).toHaveBeenCalledTimes(1);
expect(SupersetClient.get).toHaveBeenCalledWith({
endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/undefined/${forceRefresh}/`,
endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/${forceRefresh}/`,
});
expect(result.current.data).toEqual(expectedData);
await act(async () => {
result.current.refetch();
});
expect(SupersetClient.get).toHaveBeenCalledTimes(2);
expect(SupersetClient.get).toHaveBeenCalledWith({
endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/undefined/true/`,
endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/true/`,
});
expect(result.current.data).toEqual(expectedData);
});

it('returns api response for search keyword', async () => {
const expectDbId = 'db1';
const expectedSchema = 'schemaA';
const expectedKeyword = 'my work';
const forceRefresh = false;
renderHook(
() =>
useTables({
dbId: expectDbId,
schema: expectedSchema,
keyword: expectedKeyword,
}),
{
wrapper: QueryProvider,
},
);
await act(async () => {
jest.runAllTimers();
});
expect(SupersetClient.get).toHaveBeenCalledTimes(1);
expect(SupersetClient.get).toHaveBeenCalledWith({
endpoint: `/superset/tables/${expectDbId}/${expectedSchema}/${encodeURIComponent(
expectedKeyword,
)}/${forceRefresh}/`,
});
});

it('returns hasMore when total is larger than result size', async () => {
(SupersetClient.get as jest.Mock).mockResolvedValueOnce(
fakeHasMoreApiResult,
Expand Down
11 changes: 4 additions & 7 deletions superset-frontend/src/hooks/apiResources/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export type FetchTablesQueryParams = {
dbId?: string | number;
schema?: string;
forceRefresh?: boolean;
keyword?: string;
};
export interface Table {
label: string;
Expand Down Expand Up @@ -52,26 +51,24 @@ export function fetchTables({
dbId,
schema,
forceRefresh,
keyword,
}: FetchTablesQueryParams) {
const encodedSchema = schema ? encodeURIComponent(schema) : '';
const encodedKeyword = keyword ? encodeURIComponent(keyword) : 'undefined';
// TODO: Would be nice to add pagination in a follow-up. Needs endpoint changes.
const endpoint = `/superset/tables/${
dbId ?? 'undefined'
}/${encodedSchema}/${encodedKeyword}/${forceRefresh}/`;
}/${encodedSchema}/${forceRefresh}/`;
return SupersetClient.get({ endpoint }) as Promise<QueryData>;
}

type Params = FetchTablesQueryParams &
Pick<UseQueryOptions, 'onSuccess' | 'onError'>;

export function useTables(options: Params) {
const { dbId, schema, keyword, onSuccess, onError } = options || {};
const { dbId, schema, onSuccess, onError } = options || {};
const forceRefreshRef = useRef(false);
const params = { dbId, schema, keyword };
const params = { dbId, schema };
const result = useQuery<QueryData, Error, Data>(
['tables', { dbId, schema, keyword }],
['tables', { dbId, schema }],
() => fetchTables({ ...params, forceRefresh: forceRefreshRef.current }),
{
select: ({ json }) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export default function LeftPanel({
useEffect(() => {
if (loadTables) {
const endpoint = encodeURI(
`/superset/tables/${dbId}/${encodedSchema}/undefined/${refresh}/`,
`/superset/tables/${dbId}/${encodedSchema}/${refresh}/`,
);
getTablesList(endpoint);
}
Expand All @@ -191,7 +191,7 @@ export default function LeftPanel({
const encodeTableName =
value === '' ? undefined : encodeURIComponent(value);
const endpoint = encodeURI(
`/superset/tables/${dbId}/${encodedSchema}/${encodeTableName}/`,
`/superset/tables/${dbId}/${encodedSchema}/`,
);
getTablesList(endpoint);
}, FAST_DEBOUNCE),
Expand Down