mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
// https://redux.js.org/usage/writing-tests
|
|
import { setupStore } from "../app/store";
|
|
import type { AppStore, RootState } from "../app/store";
|
|
import type { PreloadedState } from "@reduxjs/toolkit";
|
|
import { render } from "@testing-library/react";
|
|
import type { RenderOptions } from "@testing-library/react";
|
|
import React, { PropsWithChildren } from "react";
|
|
import { Provider } from "react-redux";
|
|
|
|
// This type interface extends the default options for render from RTL, as well
|
|
// as allows the user to specify other things such as initialState, store.
|
|
interface ExtendedRenderOptions extends Omit<RenderOptions, "queries"> {
|
|
preloadedState?: PreloadedState<RootState>;
|
|
store?: AppStore;
|
|
}
|
|
|
|
export function renderWithProviders(
|
|
ui: React.ReactElement,
|
|
{
|
|
preloadedState = {},
|
|
// Automatically create a store instance if no store was passed in
|
|
store = setupStore(preloadedState),
|
|
...renderOptions
|
|
}: ExtendedRenderOptions = {}
|
|
) {
|
|
function Wrapper({ children }: PropsWithChildren<{}>): JSX.Element {
|
|
return <Provider store={store}>{children}</Provider>;
|
|
}
|
|
return { store, ...render(ui, { wrapper: Wrapper, ...renderOptions }) };
|
|
}
|