/**
* 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 (
{centralStores ? (
{children}
) : (
<>{children}>
)}
)
}
/** Render a screen/component inside the standard provider stack. */
export function renderWithProviders(
ui: React.ReactElement,
options?: ProviderOptions
) {
return render({ui})
}