enzyme.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 'app/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/mountWithTheme` 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/mountWithTheme` instead.
  23. */
  24. export const shallow = enzymeShallow;
  25. /**
  26. * Force the useLegacyStore setState updates to be wrapped in act.
  27. *
  28. * This is useful for old-style enzyme tests where enzyme does not correctly
  29. * wrap things in `act()` for you.
  30. *
  31. * Do NOT use this in RTL tests, as setState's triggered by store updates
  32. * should be captured with RTL style tests.
  33. */
  34. export function enforceActOnUseLegacyStoreHook() {
  35. const originalHook = window._legacyStoreHookUpdate;
  36. beforeEach(() => {
  37. window._legacyStoreHookUpdate = update => act(update);
  38. });
  39. afterEach(() => {
  40. window._legacyStoreHookUpdate = originalHook;
  41. });
  42. }