enzyme.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {cache} from '@emotion/css'; // eslint-disable-line @emotion/no-vanilla
  2. import {CacheProvider, ThemeProvider} from '@emotion/react';
  3. import {mount, shallow as enzymeShallow} from 'enzyme'; // eslint-disable-line no-restricted-imports
  4. import {act} from 'sentry-test/reactTestingLibrary';
  5. import {lightTheme} from 'sentry/utils/theme';
  6. /**
  7. * @deprecated
  8. * As we are migrating our tests to React Testing Library,
  9. * please avoid using `sentry-test/enzyme/mountWithTheme` and use `sentry-test/reactTestingLibrary/render` instead.
  10. */
  11. export function mountWithTheme(tree, opts = {}) {
  12. const WrappingThemeProvider = props => (
  13. <CacheProvider value={{...cache, compat: true}}>
  14. <ThemeProvider theme={lightTheme}>
  15. {opts.wrappingComponent ? (
  16. <opts.wrappingComponent>{props.children}</opts.wrappingComponent>
  17. ) : (
  18. props.children
  19. )}
  20. </ThemeProvider>
  21. </CacheProvider>
  22. );
  23. return mount(tree, {...opts, wrappingComponent: WrappingThemeProvider});
  24. }
  25. /**
  26. * @deprecated
  27. * As we are migrating our tests to React Testing Library,
  28. * please avoid using `sentry-test/enzyme/shallow` and use `sentry-test/reactTestingLibrary/render` instead.
  29. */
  30. export const shallow = enzymeShallow;
  31. /**
  32. * @deprecated
  33. * Force the useLegacyStore setState updates to be wrapped in act.
  34. *
  35. * This is useful for old-style enzyme tests where enzyme does not correctly
  36. * wrap things in `act()` for you.
  37. *
  38. * Do NOT use this in RTL tests, as setState's triggered by store updates
  39. * should be captured with RTL style tests.
  40. */
  41. export function enforceActOnUseLegacyStoreHook() {
  42. const originalHook = window._legacyStoreHookUpdate;
  43. beforeEach(() => {
  44. window._legacyStoreHookUpdate = update => act(update);
  45. });
  46. afterEach(() => {
  47. window._legacyStoreHookUpdate = originalHook;
  48. });
  49. }