137 lines
5 KiB
TypeScript
137 lines
5 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Harness self-test.
|
|
*
|
|
* Verifies the shared infrastructure the rest of the suite depends on:
|
|
* - the procedure registry (setProcedure / lastProcedureInput)
|
|
* - the regional CDN JSON mock behind `useAllProducts`
|
|
* - provider rendering + central-store seeding
|
|
* - the storage mock round-trip
|
|
*/
|
|
import * as React from 'react'
|
|
import { Text, View } from 'react-native'
|
|
import { waitFor } from '@testing-library/react-native'
|
|
import { trpc } from '@/src/trpc-client'
|
|
import { setProcedure, lastProcedureInput, procedureMutationCalls } from '../mocks/trpc-client'
|
|
import { fetchJson } from '../mocks/fetch-json'
|
|
import { StorageService, StorageServiceCasual } from 'common-ui'
|
|
import { useAllProducts } from '@/src/hooks/prominent-api-hooks'
|
|
import { useCentralProductStore } from '@/src/store/centralProductStore'
|
|
import {
|
|
renderWithProviders,
|
|
seedBoot,
|
|
seedJsonCache,
|
|
seedRegion,
|
|
createTestQueryClient,
|
|
} from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { productsCacheUrl, MERGED_PRODUCTS } from '../fixtures'
|
|
|
|
describe('test harness', () => {
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
})
|
|
|
|
it('returns procedure defaults through the mock hooks', async () => {
|
|
function Probe() {
|
|
const { data, isLoading } = trpc.user.order.getOrders.useQuery({ page: 1, pageSize: 10 })
|
|
if (isLoading) return <Text>loading</Text>
|
|
return <Text>{`orders:${data?.data?.length ?? 0}`}</Text>
|
|
}
|
|
|
|
const { getByText } = renderWithProviders(<Probe />)
|
|
await waitFor(() => expect(getByText('orders:6')).toBeTruthy())
|
|
})
|
|
|
|
it('records mutation input for assertions', async () => {
|
|
function Probe() {
|
|
const mutation = trpc.user.auth.sendOtp.useMutation()
|
|
React.useEffect(() => {
|
|
mutation.mutate({ mobile: '9876543210' })
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [])
|
|
return <Text>probe</Text>
|
|
}
|
|
|
|
renderWithProviders(<Probe />)
|
|
await waitFor(() => expect(lastProcedureInput('user.auth.sendOtp')).toEqual({ mobile: '9876543210' }))
|
|
expect(procedureMutationCalls('user.auth.sendOtp')).toHaveLength(1)
|
|
})
|
|
|
|
it('overrides a procedure outcome per test', async () => {
|
|
setProcedure('user.auth.sendOtp', { mode: 'error', error: new Error('boom') })
|
|
|
|
function Probe() {
|
|
const mutation = trpc.user.auth.sendOtp.useMutation()
|
|
const [message, setMessage] = React.useState('idle')
|
|
React.useEffect(() => {
|
|
mutation.mutateAsync({ mobile: '1' }).catch((error: Error) => setMessage(error.message))
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [])
|
|
return <Text>{message}</Text>
|
|
}
|
|
|
|
const { getByText } = renderWithProviders(<Probe />)
|
|
await waitFor(() => expect(getByText('boom')).toBeTruthy())
|
|
})
|
|
|
|
it('loads regional products from the versioned CDN URL', async () => {
|
|
seedBoot()
|
|
|
|
function Probe() {
|
|
const { data, isLoading } = useAllProducts()
|
|
if (isLoading) return <Text>loading</Text>
|
|
return <Text>{`products:${data?.products?.length ?? 0}`}</Text>
|
|
}
|
|
|
|
const { getByText } = renderWithProviders(<Probe />, { auth: false })
|
|
await waitFor(() => expect(getByText(`products:${MERGED_PRODUCTS.length}`)).toBeTruthy())
|
|
|
|
const documents = await fetchJson(productsCacheUrl())
|
|
expect((documents as any).products).toHaveLength(9)
|
|
})
|
|
|
|
it('seeds the central product store for cart enrichment', () => {
|
|
seedBoot()
|
|
expect(useCentralProductStore.getState().products.length).toBe(MERGED_PRODUCTS.length)
|
|
expect(useCentralProductStore.getState().productsById[2]?.name).toBe('Chicken Breast')
|
|
})
|
|
|
|
it('round-trips secure storage', async () => {
|
|
await StorageService.setItem('authToken', 'token-1')
|
|
await StorageServiceCasual.setItem('cart_items', '[]')
|
|
expect(await StorageService.getItem('authToken')).toBe('token-1')
|
|
expect(await StorageServiceCasual.getItem('cart_items')).toBe('[]')
|
|
})
|
|
|
|
it('re-seeds defaults after a reset', async () => {
|
|
setProcedure('common.healthCheck', { mode: 'error' })
|
|
|
|
function Probe() {
|
|
const { data, isError } = trpc.common.healthCheck.useQuery()
|
|
return <Text>{isError ? 'error' : JSON.stringify(data)}</Text>
|
|
}
|
|
|
|
resetAllMocks()
|
|
const { getByText } = renderWithProviders(<Probe />, { auth: false })
|
|
await waitFor(() => expect(getByText(JSON.stringify({ ok: true, status: 'healthy' }))).toBeTruthy())
|
|
})
|
|
|
|
it('exposes the query client for invalidation assertions', async () => {
|
|
const queryClient = createTestQueryClient()
|
|
const { queryClient: returned } = renderWithProviders(<View />, { queryClient, auth: false })
|
|
expect(returned).toBe(queryClient)
|
|
expect(queryClient.getQueryCache().getAll()).toEqual([])
|
|
})
|
|
|
|
it('hydrates a region without a location round-trip', () => {
|
|
seedRegion()
|
|
expect(useCentralProductStore.getState().products).toEqual([])
|
|
})
|
|
|
|
it('registers the standard CDN documents', async () => {
|
|
seedJsonCache()
|
|
const doc: any = await fetchJson(productsCacheUrl())
|
|
expect(doc.count).toBe(9)
|
|
})
|
|
})
|