112 lines
3.7 KiB
TypeScript
112 lines
3.7 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
import * as React from 'react'
|
|
import { render as rtlRender, RenderResult } from '@testing-library/react-native'
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { RefreshProvider } from 'common-ui'
|
|
import { trpc, trpcClient } from '@/src/trpc-client'
|
|
import { recordQueryInvalidation } from '../mocks/trpc-client'
|
|
import { StaffAuthProvider } from '@/components/context/staff-auth-context'
|
|
import { __set as setSecureItem, __delete as deleteSecureItem } from '../mocks/secure-store-mock'
|
|
import { STAFF, TEST_JWT } from '../fixtures'
|
|
|
|
export function createTestQueryClient(): QueryClient {
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: {
|
|
retry: false,
|
|
gcTime: Infinity,
|
|
staleTime: Infinity,
|
|
refetchOnWindowFocus: false,
|
|
},
|
|
mutations: { retry: false },
|
|
},
|
|
})
|
|
|
|
const originalInvalidate = queryClient.invalidateQueries.bind(queryClient)
|
|
;(queryClient as any).invalidateQueries = ((filters?: any, options?: any) => {
|
|
recordQueryInvalidation(filters)
|
|
return originalInvalidate(filters, options)
|
|
}) as any
|
|
|
|
return queryClient
|
|
}
|
|
|
|
type AuthMode = 'in' | 'out' | false
|
|
|
|
export interface RenderOptions {
|
|
queryClient?: QueryClient
|
|
auth?: AuthMode
|
|
withRefresh?: boolean
|
|
withAuth?: boolean
|
|
}
|
|
|
|
/** Persist (or clear) the staff session the real StaffAuthProvider bootstraps from. */
|
|
export function seedAuthStorage(mode: AuthMode, staff = STAFF, token = TEST_JWT) {
|
|
if (mode === 'in') {
|
|
setSecureItem('jwt_token', token)
|
|
setSecureItem('staff_session', JSON.stringify(staff))
|
|
} else {
|
|
deleteSecureItem('jwt_token')
|
|
deleteSecureItem('staff_session')
|
|
}
|
|
}
|
|
|
|
export interface RenderWithProvidersResult extends RenderResult {
|
|
queryClient: QueryClient
|
|
}
|
|
|
|
export function renderWithProviders(
|
|
ui: React.ReactElement,
|
|
options: RenderOptions = {}
|
|
): RenderWithProvidersResult {
|
|
const queryClient = options.queryClient ?? createTestQueryClient()
|
|
const auth = options.auth ?? 'out'
|
|
const withRefresh = options.withRefresh ?? true
|
|
const withAuth = options.withAuth ?? true
|
|
|
|
if (auth !== false) {
|
|
// Synchronous seeding so AuthProvider's bootstrap effect reads the token.
|
|
seedAuthStorage(auth)
|
|
}
|
|
|
|
const Wrapper = ({ children }: { children: React.ReactNode }) => {
|
|
let tree: React.ReactElement = React.createElement(React.Fragment, null, children)
|
|
if (withAuth && auth !== false) {
|
|
tree = React.createElement(StaffAuthProvider, null, tree)
|
|
}
|
|
if (withRefresh) {
|
|
tree = React.createElement(RefreshProvider, { queryClient, children: tree } as any)
|
|
}
|
|
tree = React.createElement(trpc.Provider, { client: trpcClient, queryClient, children: tree })
|
|
tree = React.createElement(QueryClientProvider, { client: queryClient }, tree)
|
|
return tree
|
|
}
|
|
|
|
const result = rtlRender(ui, { wrapper: Wrapper })
|
|
return { ...result, queryClient }
|
|
}
|
|
|
|
/** Render and let queued effects / microtasks settle. */
|
|
export async function renderAndFlush(
|
|
ui: React.ReactElement,
|
|
options: RenderOptions = {}
|
|
): Promise<RenderWithProvidersResult> {
|
|
const result = renderWithProviders(ui, options)
|
|
await flush()
|
|
return result
|
|
}
|
|
|
|
/** Await a macrotask so promise chains and effects settle. */
|
|
export async function flush(times = 2) {
|
|
const { act } = require('@testing-library/react-native')
|
|
for (let index = 0; index < times; index += 1) {
|
|
// eslint-disable-next-line no-await-in-loop
|
|
await act(async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
})
|
|
}
|
|
}
|
|
|
|
export function setConsoleErrorMock() {
|
|
return jest.spyOn(console, 'error').mockImplementation(() => undefined)
|
|
}
|