aboutsummaryrefslogtreecommitdiffstats
path: root/__tests__/utils/test-utils.tsx
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-03-24 21:43:44 +0100
committerArmand Philippot <git@armandphilippot.com>2022-03-24 21:43:44 +0100
commit73131da73da41c64135ed4236bd84ddba6e0c149 (patch)
tree6849f0d2884b103e432980330f866b995b7f5947 /__tests__/utils/test-utils.tsx
parent6b5c861eea12c67c4440d9e086513bd8b196c150 (diff)
parent2de1ca173ee3d8e5886a1b3fdc4afbe102fad22f (diff)
build: configure Jest
Since Next.js v12, we can use next/js package with some preconfigured Jest options.
Diffstat (limited to '__tests__/utils/test-utils.tsx')
-rw-r--r--__tests__/utils/test-utils.tsx42
1 files changed, 42 insertions, 0 deletions
diff --git a/__tests__/utils/test-utils.tsx b/__tests__/utils/test-utils.tsx
new file mode 100644
index 0000000..e47bbe1
--- /dev/null
+++ b/__tests__/utils/test-utils.tsx
@@ -0,0 +1,42 @@
+import { render, RenderOptions } from '@testing-library/react';
+import { ThemeProvider } from 'next-themes';
+import { FC } from 'react';
+import { IntlProvider } from 'react-intl';
+
+type ProvidersConfig = {
+ locale?: 'en' | 'fr';
+};
+
+type CustomRenderOptions = {
+ providers: ProvidersConfig;
+ testingLibrary: Omit<RenderOptions, 'wrapper'>;
+};
+
+/**
+ * Return a component wrapped with Intl and Theme Provider.
+ *
+ * @returns A component wrapped Intl and Theme providers.
+ */
+const AllTheProviders: FC<ProvidersConfig> = ({ children, locale = 'en' }) => {
+ return (
+ <IntlProvider locale={locale}>
+ <ThemeProvider>{children}</ThemeProvider>
+ </IntlProvider>
+ );
+};
+
+/**
+ * Render a component with all the providers.
+ *
+ * @param {JSX.Element} ui - A React component.
+ * @param {CustomRenderOptions} [options] - An object of render options and providers options.
+ * @returns A React component wrapped with all the providers.
+ */
+const customRender = (ui: JSX.Element, options?: CustomRenderOptions) =>
+ render(ui, {
+ wrapper: () => <AllTheProviders {...options?.providers} />,
+ ...options?.testingLibrary,
+ });
+
+export * from '@testing-library/react';
+export { customRender as render };