reactTestingLibrary.tsx 4.1 KB

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