2023-05-17 00:46:20 +00:00
|
|
|
import { PreloadedState } from '@reduxjs/toolkit';
|
|
|
|
import { RenderOptions, render } from '@testing-library/react';
|
|
|
|
import { PropsWithChildren } from 'react';
|
|
|
|
import { Provider } from 'react-redux';
|
|
|
|
import { AppStore, RootState, setupStore } from '~/store';
|
|
|
|
|
|
|
|
// Adapted from: https://redux.js.org/usage/writing-tests
|
|
|
|
|
2023-05-17 23:04:20 +00:00
|
|
|
// eslint-disable-next-line react-refresh/only-export-components
|
2023-05-17 00:46:20 +00:00
|
|
|
export * from '@testing-library/react';
|
|
|
|
|
|
|
|
export interface ExtendedRenderOptions extends RenderOptions {
|
|
|
|
preloadedState?: PreloadedState<RootState>;
|
|
|
|
store?: AppStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function renderWithProviders(
|
|
|
|
ui: React.ReactElement,
|
|
|
|
{ preloadedState = {}, store = setupStore(preloadedState), ...renderOptions }: ExtendedRenderOptions = {}
|
|
|
|
) {
|
2023-05-17 23:04:20 +00:00
|
|
|
function Wrapper({ children }: PropsWithChildren<unknown>): JSX.Element {
|
2023-05-17 00:46:20 +00:00
|
|
|
return <Provider store={store}>{children}</Provider>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { store, ...render(ui, { wrapper: Wrapper, ...renderOptions }) };
|
|
|
|
}
|