2019-01-15 18:53:27 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-02-11 18:32:22 -05:00
|
|
|
import '@testing-library/jest-dom/extend-expect';
|
2024-06-05 07:13:24 -04:00
|
|
|
import { ReactNode, ReactElement } from 'react';
|
2024-06-14 13:26:58 -04:00
|
|
|
import {
|
|
|
|
render,
|
|
|
|
RenderOptions,
|
|
|
|
screen,
|
|
|
|
waitFor,
|
|
|
|
within,
|
|
|
|
} from '@testing-library/react';
|
2020-12-01 14:14:37 -05:00
|
|
|
import { ThemeProvider, supersetTheme } from '@superset-ui/core';
|
2021-07-13 14:14:18 -04:00
|
|
|
import { BrowserRouter } from 'react-router-dom';
|
2021-02-25 13:59:36 -05:00
|
|
|
import { Provider } from 'react-redux';
|
2021-04-01 12:11:33 -04:00
|
|
|
import { DndProvider } from 'react-dnd';
|
|
|
|
import { HTML5Backend } from 'react-dnd-html5-backend';
|
2021-02-25 13:59:36 -05:00
|
|
|
import reducerIndex from 'spec/helpers/reducerIndex';
|
2021-05-06 00:58:40 -04:00
|
|
|
import { QueryParamProvider } from 'use-query-params';
|
2023-04-12 13:26:32 -04:00
|
|
|
import { configureStore, Store } from '@reduxjs/toolkit';
|
2023-05-08 17:25:23 -04:00
|
|
|
import { api } from 'src/hooks/apiResources/queryApi';
|
2024-06-14 13:26:58 -04:00
|
|
|
import userEvent from '@testing-library/user-event';
|
2016-11-02 15:57:44 -04:00
|
|
|
|
2021-02-25 13:59:36 -05:00
|
|
|
type Options = Omit<RenderOptions, 'queries'> & {
|
|
|
|
useRedux?: boolean;
|
2021-04-01 12:11:33 -04:00
|
|
|
useDnd?: boolean;
|
2021-05-06 00:58:40 -04:00
|
|
|
useQueryParams?: boolean;
|
2021-07-13 14:14:18 -04:00
|
|
|
useRouter?: boolean;
|
2021-02-25 13:59:36 -05:00
|
|
|
initialState?: {};
|
|
|
|
reducers?: {};
|
2022-07-20 12:56:55 -04:00
|
|
|
store?: Store;
|
2021-02-25 13:59:36 -05:00
|
|
|
};
|
|
|
|
|
2023-07-26 14:24:28 -04:00
|
|
|
export const createStore = (initialState: object = {}, reducers: object = {}) =>
|
2023-05-08 17:25:23 -04:00
|
|
|
configureStore({
|
|
|
|
preloadedState: initialState,
|
|
|
|
reducer: {
|
|
|
|
...reducers,
|
|
|
|
[api.reducerPath]: api.reducer,
|
|
|
|
},
|
|
|
|
middleware: getDefaultMiddleware =>
|
|
|
|
getDefaultMiddleware().concat(api.middleware),
|
|
|
|
devTools: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const defaultStore = createStore();
|
|
|
|
|
2022-10-02 23:00:53 -04:00
|
|
|
export function createWrapper(options?: Options) {
|
2021-07-13 14:14:18 -04:00
|
|
|
const {
|
|
|
|
useDnd,
|
|
|
|
useRedux,
|
|
|
|
useQueryParams,
|
|
|
|
useRouter,
|
|
|
|
initialState,
|
|
|
|
reducers,
|
2022-07-20 12:56:55 -04:00
|
|
|
store,
|
2021-07-13 14:14:18 -04:00
|
|
|
} = options || {};
|
2021-02-25 13:59:36 -05:00
|
|
|
|
2021-04-01 12:11:33 -04:00
|
|
|
return ({ children }: { children?: ReactNode }) => {
|
|
|
|
let result = (
|
|
|
|
<ThemeProvider theme={supersetTheme}>{children}</ThemeProvider>
|
2021-02-25 13:59:36 -05:00
|
|
|
);
|
|
|
|
|
2021-04-01 12:11:33 -04:00
|
|
|
if (useDnd) {
|
|
|
|
result = <DndProvider backend={HTML5Backend}>{result}</DndProvider>;
|
|
|
|
}
|
|
|
|
|
2023-05-23 13:42:00 -04:00
|
|
|
if (useRedux || store) {
|
2022-07-20 12:56:55 -04:00
|
|
|
const mockStore =
|
2023-05-08 17:25:23 -04:00
|
|
|
store ?? createStore(initialState, reducers || reducerIndex);
|
2022-07-20 12:56:55 -04:00
|
|
|
result = <Provider store={mockStore}>{result}</Provider>;
|
2021-04-01 12:11:33 -04:00
|
|
|
}
|
2021-02-25 13:59:36 -05:00
|
|
|
|
2021-05-06 00:58:40 -04:00
|
|
|
if (useQueryParams) {
|
|
|
|
result = <QueryParamProvider>{result}</QueryParamProvider>;
|
|
|
|
}
|
|
|
|
|
2021-07-13 14:14:18 -04:00
|
|
|
if (useRouter) {
|
|
|
|
result = <BrowserRouter>{result}</BrowserRouter>;
|
|
|
|
}
|
|
|
|
|
2021-04-01 12:11:33 -04:00
|
|
|
return result;
|
|
|
|
};
|
2020-12-01 14:14:37 -05:00
|
|
|
}
|
|
|
|
|
2021-02-25 13:59:36 -05:00
|
|
|
const customRender = (ui: ReactElement, options?: Options) =>
|
|
|
|
render(ui, { wrapper: createWrapper(options), ...options });
|
2020-12-01 14:14:37 -05:00
|
|
|
|
|
|
|
export function sleep(time: number) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(resolve, time);
|
2016-11-02 15:57:44 -04:00
|
|
|
});
|
2020-12-01 14:14:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export * from '@testing-library/react';
|
|
|
|
export { customRender as render };
|
2024-06-14 13:26:58 -04:00
|
|
|
|
|
|
|
export async function selectOption(option: string, selectName?: string) {
|
|
|
|
const select = screen.getByRole(
|
|
|
|
'combobox',
|
|
|
|
selectName ? { name: selectName } : {},
|
|
|
|
);
|
|
|
|
await userEvent.click(select);
|
|
|
|
const item = await waitFor(() =>
|
|
|
|
within(
|
|
|
|
// eslint-disable-next-line testing-library/no-node-access
|
|
|
|
document.querySelector('.rc-virtual-list')!,
|
|
|
|
).getByText(option),
|
|
|
|
);
|
|
|
|
await userEvent.click(item);
|
|
|
|
}
|