11import { render } from '@testing-library/react' ;
22import { type ReactElement , type ReactNode , useMemo } from 'react' ;
3+ import { MemoryRouter } from 'react-router-dom' ;
34
45import { BaseStyles , ThemeProvider } from '@primer/react' ;
56
67import { mockAuth , mockSettings } from '../__mocks__/state-mocks' ;
78
89import { AppContext , type AppContextState } from '../context/App' ;
910
11+ export { navigateMock } from './vitest.setup' ;
1012export type DeepPartial < T > = { [ K in keyof T ] ?: DeepPartial < T [ K ] > } ;
1113
14+ const EMPTY_APP_CONTEXT : TestAppContext = { } ;
15+
16+ interface RenderOptions extends Partial < AppContextState > {
17+ initialEntries ?: string [ ] ;
18+ }
19+
20+ /**
21+ * Test context
22+ */
23+ type TestAppContext = Partial < AppContextState > ;
24+
1225/**
1326 * Props for the AppContextProvider wrapper
1427 */
1528interface AppContextProviderProps {
1629 readonly children : ReactNode ;
17- readonly value ?: Partial < AppContextState > ;
30+ readonly value ?: TestAppContext ;
31+ readonly initialEntries ?: string [ ] ;
1832}
1933
2034/**
2135 * Wrapper component that provides ThemeProvider, BaseStyles, and AppContext
2236 * with sensible defaults for testing.
2337 */
24- function AppContextProvider ( { children, value = { } } : AppContextProviderProps ) {
25- const defaultValue : AppContextState = useMemo ( ( ) => {
38+ function AppContextProvider ( {
39+ children,
40+ value = EMPTY_APP_CONTEXT ,
41+ initialEntries,
42+ } : AppContextProviderProps ) {
43+ const defaultValue : TestAppContext = useMemo ( ( ) => {
2644 return {
2745 auth : mockAuth ,
2846 settings : mockSettings ,
@@ -58,17 +76,19 @@ function AppContextProvider({ children, value = {} }: AppContextProviderProps) {
5876 updateFilter : vi . fn ( ) ,
5977
6078 ...value ,
61- } as AppContextState ;
79+ } as TestAppContext ;
6280 } , [ value ] ) ;
6381
6482 return (
65- < ThemeProvider >
66- < BaseStyles >
67- < AppContext . Provider value = { defaultValue } >
68- { children }
69- </ AppContext . Provider >
70- </ BaseStyles >
71- </ ThemeProvider >
83+ < MemoryRouter initialEntries = { initialEntries } >
84+ < ThemeProvider >
85+ < BaseStyles >
86+ < AppContext . Provider value = { defaultValue } >
87+ { children }
88+ </ AppContext . Provider >
89+ </ BaseStyles >
90+ </ ThemeProvider >
91+ </ MemoryRouter >
7292 ) ;
7393}
7494
@@ -80,11 +100,13 @@ function AppContextProvider({ children, value = {} }: AppContextProviderProps) {
80100 */
81101export function renderWithAppContext (
82102 ui : ReactElement ,
83- context : Partial < AppContextState > = { } ,
103+ { initialEntries , ... context } : RenderOptions = { } ,
84104) {
85105 return render ( ui , {
86106 wrapper : ( { children } ) => (
87- < AppContextProvider value = { context } > { children } </ AppContextProvider >
107+ < AppContextProvider initialEntries = { initialEntries } value = { context } >
108+ { children }
109+ </ AppContextProvider >
88110 ) ,
89111 } ) ;
90112}
0 commit comments