reactTestingLibrary.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * Migrating from enzyme?
  66. * Try avoiding unnecessary context and just mount your component. If it works, then you dont need anything else.
  67. * render(<TestedComponent />);
  68. *
  69. * If your component requires routerContext or organization to render, pass it via context options argument.
  70. * render(<TestedComponent />, {context: routerContext, organization});
  71. */
  72. function render(ui: React.ReactElement, options?: Options) {
  73. options = options ?? {};
  74. const {context, organization, project, projects, ...otherOptions} = options;
  75. let {router} = options;
  76. if (router === undefined && context?.context?.router) {
  77. router = context.context.router;
  78. }
  79. const AllTheProviders = makeAllTheProviders({
  80. context,
  81. organization,
  82. project,
  83. projects,
  84. router,
  85. });
  86. return rtl.render(ui, {wrapper: AllTheProviders, ...otherOptions});
  87. }
  88. /**
  89. * @deprecated
  90. * Use userEvent over fireEvent where possible.
  91. * More details: https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#not-using-testing-libraryuser-event
  92. */
  93. const fireEvent = rtl.fireEvent;
  94. function renderGlobalModal(options?: Options) {
  95. return render(<GlobalModal />, options);
  96. }
  97. /**
  98. * jest-sentry-environment attaches a global Sentry object that can be used.
  99. * The types on it conflicts with the existing window.Sentry object so it's using any here.
  100. */
  101. const globalSentry = (global as any).Sentry;
  102. /**
  103. * This cannot be implemented as a Sentry Integration because Jest creates an
  104. * isolated environment for each test suite. This means that if we were to apply
  105. * the monkey patching ahead of time, it would be shadowed by Jest.
  106. */
  107. instrumentUserEvent(globalSentry?.getCurrentHub.bind(globalSentry));
  108. // eslint-disable-next-line no-restricted-imports, import/export
  109. export * from '@testing-library/react';
  110. // eslint-disable-next-line import/export
  111. export {render, renderGlobalModal, userEvent, reactHooks, fireEvent};