reactTestingLibrary.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import {Component, Fragment} from 'react';
  2. import {InjectedRouter} from 'react-router';
  3. import {cache} from '@emotion/css'; // eslint-disable-line @emotion/no-vanilla
  4. import {CacheProvider, ThemeProvider} from '@emotion/react';
  5. import * as rtl from '@testing-library/react'; // eslint-disable-line no-restricted-imports
  6. import * as reactHooks from '@testing-library/react-hooks'; // eslint-disable-line no-restricted-imports
  7. import userEvent from '@testing-library/user-event'; // eslint-disable-line no-restricted-imports
  8. import {makeTestQueryClient} from 'sentry-test/queryClient';
  9. import GlobalModal from 'sentry/components/globalModal';
  10. import {Organization} from 'sentry/types';
  11. import {QueryClientProvider} from 'sentry/utils/queryClient';
  12. import {lightTheme} from 'sentry/utils/theme';
  13. import {OrganizationContext} from 'sentry/views/organizationContext';
  14. import {RouteContext} from 'sentry/views/routeContext';
  15. import {instrumentUserEvent} from '../instrumentedEnv/userEventIntegration';
  16. import {initializeOrg} from './initializeOrg';
  17. type ProviderOptions = {
  18. context?: Record<string, any>;
  19. organization?: Partial<Organization>;
  20. project?: string;
  21. projects?: string[];
  22. router?: Partial<InjectedRouter>;
  23. };
  24. type Options = ProviderOptions & rtl.RenderOptions;
  25. function createProvider(contextDefs: Record<string, any>) {
  26. return class ContextProvider extends Component {
  27. static childContextTypes = contextDefs.childContextTypes;
  28. getChildContext() {
  29. return contextDefs.context;
  30. }
  31. render() {
  32. return this.props.children;
  33. }
  34. };
  35. }
  36. function makeAllTheProviders({context, ...initializeOrgOptions}: ProviderOptions) {
  37. const ContextProvider = context ? createProvider(context) : Fragment;
  38. const {organization, router} = initializeOrg(initializeOrgOptions as any);
  39. return function ({children}: {children?: React.ReactNode}) {
  40. return (
  41. <ContextProvider>
  42. <CacheProvider value={{...cache, compat: true}}>
  43. <ThemeProvider theme={lightTheme}>
  44. <QueryClientProvider client={makeTestQueryClient()}>
  45. <RouteContext.Provider
  46. value={{
  47. router,
  48. location: router.location,
  49. params: router.params,
  50. routes: router.routes,
  51. }}
  52. >
  53. <OrganizationContext.Provider value={organization}>
  54. {children}
  55. </OrganizationContext.Provider>
  56. </RouteContext.Provider>
  57. </QueryClientProvider>
  58. </ThemeProvider>
  59. </CacheProvider>
  60. </ContextProvider>
  61. );
  62. };
  63. }
  64. /**
  65. * Try avoiding unnecessary context and just mount your component. If it works,
  66. * then you dont need anything else.
  67. *
  68. * render(<TestedComponent />);
  69. *
  70. * If your component requires routerContext or organization to render, pass it
  71. * via context options argument. render(<TestedComponent />, {context:
  72. * routerContext, organization});
  73. */
  74. function render(ui: React.ReactElement, options?: Options) {
  75. options = options ?? {};
  76. const {context, organization, project, projects, ...otherOptions} = options;
  77. let {router} = options;
  78. if (router === undefined && context?.context?.router) {
  79. router = context.context.router;
  80. }
  81. const AllTheProviders = makeAllTheProviders({
  82. context,
  83. organization,
  84. project,
  85. projects,
  86. router,
  87. });
  88. return rtl.render(ui, {wrapper: AllTheProviders, ...otherOptions});
  89. }
  90. /**
  91. * @deprecated
  92. * Use userEvent over fireEvent where possible.
  93. * More details: https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#not-using-testing-libraryuser-event
  94. */
  95. const fireEvent = rtl.fireEvent;
  96. function renderGlobalModal(options?: Options) {
  97. const result = render(<GlobalModal />, options);
  98. /**
  99. * Helper that waits for the modal to be removed from the DOM. You may need to
  100. * wait for the modal to be removed to avoid any act warnings.
  101. */
  102. function waitForModalToHide() {
  103. return rtl.waitForElementToBeRemoved(() => rtl.screen.getByRole('dialog'));
  104. }
  105. return {...result, waitForModalToHide};
  106. }
  107. /**
  108. * jest-sentry-environment attaches a global Sentry object that can be used.
  109. * The types on it conflicts with the existing window.Sentry object so it's using any here.
  110. */
  111. const globalSentry = (global as any).Sentry;
  112. /**
  113. * This cannot be implemented as a Sentry Integration because Jest creates an
  114. * isolated environment for each test suite. This means that if we were to apply
  115. * the monkey patching ahead of time, it would be shadowed by Jest.
  116. */
  117. instrumentUserEvent(globalSentry?.getCurrentHub.bind(globalSentry));
  118. // eslint-disable-next-line no-restricted-imports, import/export
  119. export * from '@testing-library/react';
  120. // eslint-disable-next-line import/export
  121. export {render, renderGlobalModal, userEvent, reactHooks, fireEvent};