70 lines
2.3 KiB
TypeScript
70 lines
2.3 KiB
TypeScript
/**
|
|
* Generic render helpers shared by the screen and component tests.
|
|
*
|
|
* The provider stack mirrors app/_layout.tsx, minus the wrappers that need a
|
|
* real device or navigation container (Location/Health/WebView/Notification).
|
|
* `CentralStoreInitializer` is on by default because most screens read the
|
|
* central product/slot stores it populates; pass `centralStores: false` for
|
|
* pure components that must not trigger CDN fetches.
|
|
*
|
|
* A file that renders router consumers must declare the router mock itself:
|
|
*
|
|
* jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
|
|
*
|
|
* For real-router navigation assertions, use tests/helpers/real-router.tsx
|
|
* instead (and do NOT mock expo-router in that file).
|
|
*/
|
|
|
|
import React from 'react'
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { render } from '@testing-library/react-native'
|
|
import { RefreshProvider } from 'common-ui'
|
|
|
|
import CentralStoreInitializer from '@/src/components/CentralStoreInitializer'
|
|
import { trpc, trpcClient } from '@/src/trpc-client'
|
|
|
|
export function createQueryClient() {
|
|
return new QueryClient({
|
|
defaultOptions: { queries: { retry: false, gcTime: 0 } },
|
|
})
|
|
}
|
|
|
|
export interface ProviderOptions {
|
|
/** Include CentralStoreInitializer (products/slots/cart reconcile). Default true. */
|
|
centralStores?: boolean
|
|
/** Reuse a specific query client (e.g. to assert refetches). */
|
|
queryClient?: QueryClient
|
|
}
|
|
|
|
export function Providers({
|
|
children,
|
|
centralStores = true,
|
|
queryClient,
|
|
}: { children?: React.ReactNode } & ProviderOptions) {
|
|
const client = React.useMemo(
|
|
() => queryClient ?? createQueryClient(),
|
|
[queryClient]
|
|
)
|
|
|
|
return (
|
|
<trpc.Provider client={trpcClient} queryClient={client}>
|
|
<QueryClientProvider client={client}>
|
|
<RefreshProvider queryClient={client}>
|
|
{centralStores ? (
|
|
<CentralStoreInitializer>{children}</CentralStoreInitializer>
|
|
) : (
|
|
<>{children}</>
|
|
)}
|
|
</RefreshProvider>
|
|
</QueryClientProvider>
|
|
</trpc.Provider>
|
|
)
|
|
}
|
|
|
|
/** Render a screen/component inside the standard provider stack. */
|
|
export function renderWithProviders(
|
|
ui: React.ReactElement,
|
|
options?: ProviderOptions
|
|
) {
|
|
return render(<Providers {...options}>{ui}</Providers>)
|
|
}
|