Skip to content

Commit eab4e39

Browse files
committed
fix: test helpers
1 parent 7262eea commit eab4e39

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

src/helpers/test.tsx

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import type { Middleware, Reducer, Store } from "@reduxjs/toolkit"
2+
import type { RenderOptions } from "@testing-library/react"
3+
import { render } from "@testing-library/react"
4+
import userEvent from "@testing-library/user-event"
5+
import type { PropsWithChildren, ReactElement } from "react"
6+
import { Provider } from "react-redux"
7+
8+
import { makeStore } from "./store"
9+
10+
export function renderWithUser(
11+
ui: ReactElement,
12+
renderOptions: RenderOptions = {},
13+
) {
14+
return {
15+
user: userEvent.setup(),
16+
...render(ui, renderOptions),
17+
}
18+
}
19+
20+
/**
21+
* Renders the given React element with Redux Provider and custom store.
22+
* This function is useful for testing components that are connected to the
23+
* Redux store.
24+
*
25+
* @param ui - The React component or element to render.
26+
* @param reducer - The root reducer to use for the store.
27+
* @param extendedRenderOptions - Optional configuration options for rendering.
28+
* This includes `preloadedState` for initial Redux state and `store` for a
29+
* specific Redux store instance. Any additional properties are passed to React
30+
* Testing Library's render function.
31+
* @returns An object containing the Redux store used in the render, User event
32+
* API for simulating user interactions in tests, and all of React Testing
33+
* Library's query functions for testing the component.
34+
*/
35+
export function renderWithStore<R extends Reducer>(
36+
ui: ReactElement,
37+
reducer: R,
38+
extendedRenderOptions: RenderOptions & {
39+
/**
40+
* The middlewares used to create the Redux store.
41+
*/
42+
middlewares?: Middleware[]
43+
44+
/**
45+
* Defines a specific portion or the entire initial state for the Redux store.
46+
* This is particularly useful for initializing the state in a
47+
* controlled manner during testing, allowing components to be rendered
48+
* with predetermined state conditions.
49+
*/
50+
preloadedState?: Partial<ReturnType<R>>
51+
52+
/**
53+
* Allows the use of a specific Redux store instance instead of a
54+
* default or global store. This flexibility is beneficial when
55+
* testing components with unique store requirements or when isolating
56+
* tests from a global store state. The custom store should be configured
57+
* to match the structure and middleware of the store used by the application.
58+
*
59+
* @default makeStore({reducer,middlewares,preloadedState})
60+
*/
61+
store?: Store
62+
} = {},
63+
) {
64+
const {
65+
middlewares,
66+
preloadedState,
67+
// Automatically create a store instance if no store was passed in
68+
store = makeStore({ reducer, middlewares, preloadedState }),
69+
...renderOptions
70+
} = extendedRenderOptions
71+
72+
const Wrapper = ({ children }: PropsWithChildren) => (
73+
<Provider store={store}>{children}</Provider>
74+
)
75+
76+
// Return an object with the store and all of RTL's query functions
77+
return {
78+
store,
79+
user: userEvent.setup(),
80+
...render(ui, { wrapper: Wrapper, ...renderOptions }),
81+
}
82+
}

0 commit comments

Comments
 (0)