2021-07-31 15:57:43 +00:00
|
|
|
import { RouterContext, BlitzRouter, BlitzProvider } from "blitz";
|
|
|
|
import { render as defaultRender } from "@testing-library/react";
|
|
|
|
import { renderHook as defaultRenderHook } from "@testing-library/react-hooks";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
export * from "@testing-library/react";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
// This file customizes the render() and renderHook() test functions provided
|
|
|
|
// by React testing library. It adds a router context wrapper with a mocked router.
|
|
|
|
//
|
|
|
|
// You should always import `render` and `renderHook` from this file
|
|
|
|
//
|
|
|
|
// This is the place to add any other context providers you need while testing.
|
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
// render()
|
|
|
|
// --------------------------------------------------
|
|
|
|
// Override the default test render with our own
|
|
|
|
//
|
|
|
|
// You can override the router mock like this:
|
|
|
|
//
|
|
|
|
// const { baseElement } = render(<MyComponent />, {
|
|
|
|
// router: { pathname: '/my-custom-pathname' },
|
|
|
|
// });
|
|
|
|
// --------------------------------------------------
|
2021-08-01 14:03:49 +00:00
|
|
|
export function render(ui: RenderUI, { wrapper, router, dehydratedState, ...options }: RenderOptions = {}) {
|
2021-07-31 14:33:18 +00:00
|
|
|
if (!wrapper) {
|
|
|
|
// Add a default context wrapper if one isn't supplied from the test
|
|
|
|
wrapper = ({ children }) => (
|
|
|
|
<BlitzProvider dehydratedState={dehydratedState}>
|
2021-08-01 14:03:49 +00:00
|
|
|
<RouterContext.Provider value={{ ...mockRouter, ...router }}>{children}</RouterContext.Provider>
|
2021-07-31 14:33:18 +00:00
|
|
|
</BlitzProvider>
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
2021-07-31 15:57:43 +00:00
|
|
|
return defaultRender(ui, { wrapper, ...options });
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
// renderHook()
|
|
|
|
// --------------------------------------------------
|
|
|
|
// Override the default test renderHook with our own
|
|
|
|
//
|
|
|
|
// You can override the router mock like this:
|
|
|
|
//
|
|
|
|
// const result = renderHook(() => myHook(), {
|
|
|
|
// router: { pathname: '/my-custom-pathname' },
|
|
|
|
// });
|
|
|
|
// --------------------------------------------------
|
2021-08-01 14:03:49 +00:00
|
|
|
export function renderHook(hook: RenderHook, { wrapper, router, dehydratedState, ...options }: RenderHookOptions = {}) {
|
2021-07-31 14:33:18 +00:00
|
|
|
if (!wrapper) {
|
|
|
|
// Add a default context wrapper if one isn't supplied from the test
|
|
|
|
wrapper = ({ children }) => (
|
|
|
|
<BlitzProvider dehydratedState={dehydratedState}>
|
2021-08-01 14:03:49 +00:00
|
|
|
<RouterContext.Provider value={{ ...mockRouter, ...router }}>{children}</RouterContext.Provider>
|
2021-07-31 14:33:18 +00:00
|
|
|
</BlitzProvider>
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
2021-07-31 15:57:43 +00:00
|
|
|
return defaultRenderHook(hook, { wrapper, ...options });
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const mockRouter: BlitzRouter = {
|
|
|
|
basePath: "",
|
|
|
|
pathname: "/",
|
|
|
|
route: "/",
|
|
|
|
asPath: "/",
|
|
|
|
params: {},
|
|
|
|
query: {},
|
|
|
|
isReady: true,
|
|
|
|
isLocaleDomain: false,
|
|
|
|
isPreview: false,
|
|
|
|
push: jest.fn(),
|
|
|
|
replace: jest.fn(),
|
|
|
|
reload: jest.fn(),
|
|
|
|
back: jest.fn(),
|
|
|
|
prefetch: jest.fn(),
|
|
|
|
beforePopState: jest.fn(),
|
|
|
|
events: {
|
|
|
|
on: jest.fn(),
|
|
|
|
off: jest.fn(),
|
|
|
|
emit: jest.fn(),
|
|
|
|
},
|
|
|
|
isFallback: false,
|
2021-07-31 15:57:43 +00:00
|
|
|
};
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
type DefaultParams = Parameters<typeof defaultRender>;
|
|
|
|
type RenderUI = DefaultParams[0];
|
|
|
|
type RenderOptions = DefaultParams[1] & {
|
|
|
|
router?: Partial<BlitzRouter>;
|
|
|
|
dehydratedState?: unknown;
|
|
|
|
};
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
type DefaultHookParams = Parameters<typeof defaultRenderHook>;
|
|
|
|
type RenderHook = DefaultHookParams[0];
|
2021-07-31 14:33:18 +00:00
|
|
|
type RenderHookOptions = DefaultHookParams[1] & {
|
2021-07-31 15:57:43 +00:00
|
|
|
router?: Partial<BlitzRouter>;
|
|
|
|
dehydratedState?: unknown;
|
|
|
|
};
|