enzyme.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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}>
  14. <ThemeProvider theme={lightTheme}>{props.children}</ThemeProvider>
  15. </CacheProvider>
  16. );
  17. return mount(tree, {wrappingComponent: WrappingThemeProvider, ...opts});
  18. }
  19. /**
  20. * @deprecated
  21. * As we are migrating our tests to React Testing Library,
  22. * please avoid using `sentry-test/enzyme/shallow` and use `sentry-test/reactTestingLibrary/render` instead.
  23. */
  24. export const shallow = enzymeShallow;
  25. /**
  26. * @deprecated
  27. * Force the useLegacyStore setState updates to be wrapped in act.
  28. *
  29. * This is useful for old-style enzyme tests where enzyme does not correctly
  30. * wrap things in `act()` for you.
  31. *
  32. * Do NOT use this in RTL tests, as setState's triggered by store updates
  33. * should be captured with RTL style tests.
  34. */
  35. export function enforceActOnUseLegacyStoreHook() {
  36. const originalHook = window._legacyStoreHookUpdate;
  37. beforeEach(() => {
  38. window._legacyStoreHookUpdate = update => act(update);
  39. });
  40. afterEach(() => {
  41. window._legacyStoreHookUpdate = originalHook;
  42. });
  43. }