|
| 1 | +# `@scaleway/jest-helpers` |
| 2 | + |
| 3 | +## A package for utilities jest functions |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```bash |
| 8 | +$ yarn add @scaleway/jest-functions |
| 9 | +``` |
| 10 | + |
| 11 | +## How to use |
| 12 | + |
| 13 | +### Create the helpers functions |
| 14 | + |
| 15 | +```tsx |
| 16 | +import makeHelpers from '@scaleway/jest-helpers' |
| 17 | + |
| 18 | +const Wrapper = ({ children }) => ( |
| 19 | + <ThemeProvider>{children}</ThemeProvider> |
| 20 | +) |
| 21 | + |
| 22 | +export const { |
| 23 | + renderWithTheme, |
| 24 | + shouldMatchEmotionSnapshot, |
| 25 | + shouldMatchEmotionSnapshotWithPortal, |
| 26 | +} = makeHelpers(Wrapper) |
| 27 | +``` |
| 28 | + |
| 29 | +#### With a theme prop |
| 30 | + |
| 31 | +```tsx |
| 32 | +import makeHelpers from '@scaleway/jest-helpers' |
| 33 | +import defaultTheme from '..' |
| 34 | + |
| 35 | +interface WrapperProps { |
| 36 | + theme?: typeof defaultTheme |
| 37 | +} |
| 38 | + |
| 39 | +const Wrapper = ({ theme, children }) => ( |
| 40 | + <ThemeProvider theme={theme}>{children}</ThemeProvider> |
| 41 | +) |
| 42 | + |
| 43 | +export const { |
| 44 | + renderWithTheme, |
| 45 | + shouldMatchEmotionSnapshot, |
| 46 | + shouldMatchEmotionSnapshotWithPortal, |
| 47 | +} = makeHelpers(Wrapper) |
| 48 | +``` |
| 49 | + |
| 50 | +#### With CreateSerializerOptions |
| 51 | + |
| 52 | +```tsx |
| 53 | +import makeHelpers from '@scaleway/jest-helpers' |
| 54 | + |
| 55 | +const Wrapper = ({ children }) => ( |
| 56 | + <ThemeProvider>{children}</ThemeProvider> |
| 57 | +) |
| 58 | + |
| 59 | +export const { |
| 60 | + renderWithTheme, |
| 61 | + shouldMatchEmotionSnapshot, |
| 62 | + shouldMatchEmotionSnapshotWithPortal, |
| 63 | +} = makeHelpers(Wrapper, { classNameReplacer: className => className }) |
| 64 | +``` |
| 65 | + |
| 66 | +### renderWithTheme |
| 67 | + |
| 68 | +Automatically uses `CacheProvider` from `@emotion/cache`. Use it with a component, optional options & optional theme. |
| 69 | + |
| 70 | +```tsx |
| 71 | +const renderWithTheme = ( |
| 72 | + component: ReactNode, // The component to render |
| 73 | + options?: RenderOptions, // RenderOptions from @testing-library/react |
| 74 | + theme?: Theme, // Optional theme to use which will be passed to the Wrapper above |
| 75 | +) => ReturnType<typeof render> |
| 76 | +``` |
| 77 | + |
| 78 | +### shouldMatchEmotionSnapshot / shouldMatchEmotionSnapshotWithPortal |
| 79 | + |
| 80 | +Internally it uses the `renderWithTheme` generated from above. |
| 81 | + |
| 82 | +```tsx |
| 83 | +const shouldMatchEmotionSnapshot = ( |
| 84 | + component: ReactNode, // The component to render |
| 85 | + options: { // In an object to make it backward-compatible and don't introduce any API breaking changes |
| 86 | + options?: RenderOptions // RenderOptions from @testing-library/react |
| 87 | + transform?: (node: ReturnType<typeof render>) => Promise<void> | void // (a)sync function execute between the render and the expect. You can use this if you need mockAllIsIntersecting |
| 88 | + theme?: Theme // Optional theme to use which will be passed to the Wrapper above |
| 89 | + }, |
| 90 | +) => Promise<void> |
| 91 | +``` |
0 commit comments