testing-library.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import '@testing-library/jest-dom';
  20. import { ReactNode, ReactElement } from 'react';
  21. // eslint-disable-next-line no-restricted-imports
  22. import {
  23. render,
  24. RenderOptions,
  25. screen,
  26. waitFor,
  27. within,
  28. } from '@testing-library/react';
  29. import {
  30. ThemeProvider,
  31. // eslint-disable-next-line no-restricted-imports
  32. supersetTheme,
  33. themeObject,
  34. } from '@superset-ui/core';
  35. import { SupersetThemeProvider } from 'src/theme/ThemeProvider';
  36. import { ThemeController } from 'src/theme/ThemeController';
  37. import { BrowserRouter } from 'react-router-dom';
  38. import { Provider } from 'react-redux';
  39. import { DndProvider } from 'react-dnd';
  40. import { HTML5Backend } from 'react-dnd-html5-backend';
  41. import reducerIndex from 'spec/helpers/reducerIndex';
  42. import { QueryParamProvider } from 'use-query-params';
  43. import { configureStore, Store } from '@reduxjs/toolkit';
  44. import { api } from 'src/hooks/apiResources/queryApi';
  45. import userEvent from '@testing-library/user-event';
  46. import { ExtensionsProvider } from 'src/extensions/ExtensionsContext';
  47. type Options = Omit<RenderOptions, 'queries'> & {
  48. useRedux?: boolean;
  49. useDnd?: boolean;
  50. useQueryParams?: boolean;
  51. useRouter?: boolean;
  52. useTheme?: boolean;
  53. initialState?: {};
  54. reducers?: {};
  55. store?: Store;
  56. };
  57. const themeController = new ThemeController({ themeObject });
  58. export const createStore = (initialState: object = {}, reducers: object = {}) =>
  59. configureStore({
  60. preloadedState: initialState,
  61. reducer: {
  62. ...reducers,
  63. [api.reducerPath]: api.reducer,
  64. },
  65. middleware: getDefaultMiddleware =>
  66. getDefaultMiddleware().concat(api.middleware),
  67. devTools: false,
  68. });
  69. export const defaultStore = createStore();
  70. export function createWrapper(options?: Options) {
  71. const {
  72. useDnd,
  73. useRedux,
  74. useQueryParams,
  75. useRouter,
  76. useTheme,
  77. initialState,
  78. reducers,
  79. store,
  80. } = options || {};
  81. return ({ children }: { children?: ReactNode }) => {
  82. let result = (
  83. <ThemeProvider theme={supersetTheme}>
  84. <ExtensionsProvider>{children}</ExtensionsProvider>
  85. </ThemeProvider>
  86. );
  87. if (useTheme) {
  88. result = (
  89. <SupersetThemeProvider themeController={themeController}>
  90. {result}
  91. </SupersetThemeProvider>
  92. );
  93. }
  94. if (useDnd) {
  95. result = <DndProvider backend={HTML5Backend}>{result}</DndProvider>;
  96. }
  97. if (useRedux || store) {
  98. const mockStore =
  99. store ?? createStore(initialState, reducers || reducerIndex);
  100. result = <Provider store={mockStore}>{result}</Provider>;
  101. }
  102. if (useQueryParams) {
  103. result = <QueryParamProvider>{result}</QueryParamProvider>;
  104. }
  105. if (useRouter) {
  106. result = <BrowserRouter>{result}</BrowserRouter>;
  107. }
  108. return result;
  109. };
  110. }
  111. const customRender = (ui: ReactElement, options?: Options) =>
  112. render(ui, { wrapper: createWrapper(options), ...options });
  113. export function sleep(time: number) {
  114. return new Promise(resolve => {
  115. setTimeout(resolve, time);
  116. });
  117. }
  118. // eslint-disable-next-line no-restricted-imports
  119. export * from '@testing-library/react';
  120. export { customRender as render };
  121. export { default as userEvent } from '@testing-library/user-event';
  122. export async function selectOption(option: string, selectName?: string) {
  123. const select = screen.getByRole(
  124. 'combobox',
  125. selectName ? { name: selectName } : {},
  126. );
  127. await userEvent.click(select);
  128. const item = await waitFor(() =>
  129. within(
  130. // eslint-disable-next-line testing-library/no-node-access
  131. document.querySelector('.rc-virtual-list')!,
  132. ).getByText(option),
  133. );
  134. await userEvent.click(item);
  135. }