|
| 1 | +/** |
| 2 | + * @file Utility functions for testing. |
| 3 | + * |
| 4 | + * **IMPORTANT**: This file is supposed to be used instead of `@testing-library/react` |
| 5 | + * It is used to provide a portal root and locale to all tests. |
| 6 | + */ |
| 7 | + |
| 8 | +import { Form, type FormProps, type TSchema } from '#/components/AriaComponents' |
| 9 | +import UIProviders from '#/components/UIProviders' |
| 10 | +import { |
| 11 | + render, |
| 12 | + renderHook, |
| 13 | + type RenderHookOptions, |
| 14 | + type RenderHookResult, |
| 15 | + type RenderOptions, |
| 16 | + type RenderResult, |
| 17 | +} from '@testing-library/react' |
| 18 | +import { type PropsWithChildren, type ReactElement } from 'react' |
| 19 | + |
| 20 | +/** |
| 21 | + * A wrapper that passes through its children. |
| 22 | + */ |
| 23 | +function PassThroughWrapper({ children }: PropsWithChildren) { |
| 24 | + return children |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * A wrapper that provides the {@link UIProviders} context. |
| 29 | + */ |
| 30 | +function UIProvidersWrapper({ children }: PropsWithChildren) { |
| 31 | + return ( |
| 32 | + <UIProviders portalRoot={document.body} locale="en"> |
| 33 | + {children} |
| 34 | + </UIProviders> |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * A wrapper that provides the {@link Form} context. |
| 40 | + */ |
| 41 | +function FormWrapper<Schema extends TSchema, SubmitResult = void>( |
| 42 | + props: FormProps<Schema, SubmitResult>, |
| 43 | +) { |
| 44 | + return <Form {...props} /> |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Custom render function for tests. |
| 49 | + */ |
| 50 | +function renderWithRoot(ui: ReactElement, options?: Omit<RenderOptions, 'queries'>): RenderResult { |
| 51 | + const { wrapper: Wrapper = PassThroughWrapper, ...rest } = options ?? {} |
| 52 | + |
| 53 | + return render(ui, { |
| 54 | + wrapper: ({ children }) => ( |
| 55 | + <UIProvidersWrapper> |
| 56 | + <Wrapper>{children}</Wrapper> |
| 57 | + </UIProvidersWrapper> |
| 58 | + ), |
| 59 | + ...rest, |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Adds a form wrapper to the component. |
| 65 | + */ |
| 66 | +function renderWithForm<Schema extends TSchema, SubmitResult = void>( |
| 67 | + ui: ReactElement, |
| 68 | + options: Omit<RenderOptions, 'queries' | 'wrapper'> & { |
| 69 | + formProps: FormProps<Schema, SubmitResult> |
| 70 | + }, |
| 71 | +): RenderResult { |
| 72 | + const { formProps, ...rest } = options |
| 73 | + |
| 74 | + return renderWithRoot(ui, { |
| 75 | + wrapper: ({ children }) => <FormWrapper {...formProps}>{children}</FormWrapper>, |
| 76 | + ...rest, |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * A custom renderHook function for tests. |
| 82 | + */ |
| 83 | +function renderHookWithRoot<Result, Props>( |
| 84 | + hook: (props: Props) => Result, |
| 85 | + options?: Omit<RenderHookOptions<Props>, 'queries'>, |
| 86 | +): RenderHookResult<Result, Props> { |
| 87 | + return renderHook(hook, { wrapper: UIProvidersWrapper, ...options }) |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * A custom renderHook function for tests that provides the {@link Form} context. |
| 92 | + */ |
| 93 | +function renderHookWithForm<Result, Props, Schema extends TSchema, SubmitResult = void>( |
| 94 | + hook: (props: Props) => Result, |
| 95 | + options: Omit<RenderHookOptions<Props>, 'queries' | 'wrapper'> & { |
| 96 | + formProps: FormProps<Schema, SubmitResult> |
| 97 | + }, |
| 98 | +): RenderHookResult<Result, Props> { |
| 99 | + const { formProps, ...rest } = options |
| 100 | + |
| 101 | + return renderHookWithRoot(hook, { |
| 102 | + wrapper: ({ children }) => <FormWrapper {...formProps}>{children}</FormWrapper>, |
| 103 | + ...rest, |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +export * from '@testing-library/react' |
| 108 | +export { default as userEvent } from '@testing-library/user-event' |
| 109 | +// override render method |
| 110 | +export { |
| 111 | + renderWithRoot as render, |
| 112 | + renderHookWithRoot as renderHook, |
| 113 | + renderHookWithForm, |
| 114 | + renderWithForm, |
| 115 | +} |
0 commit comments