139 lines
4.7 KiB
TypeScript
139 lines
4.7 KiB
TypeScript
/**
|
|
* Navigation tests for the home page using the REAL expo-router.
|
|
*
|
|
* Unlike tests/home.test.tsx (which mocks the router and asserts the intended
|
|
* push target), this file renders the actual router with a route map mirroring
|
|
* the app's group structure. That proves the pushed URLs resolve to a route and
|
|
* that params arrive.
|
|
*
|
|
* Route keys mirror app/ (see the directory listing); renderRouter strips the
|
|
* group segments, so '(drawer)/(tabs)/home/index' is reachable at '/home'.
|
|
*
|
|
* expo-router is deliberately not mocked in tests/setup.ts - that is what makes
|
|
* this file possible.
|
|
*/
|
|
|
|
import React from 'react'
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { fireEvent, within } from '@testing-library/react-native'
|
|
import { renderRouter, screen } from 'expo-router/testing-library'
|
|
import { useLocalSearchParams } from 'expo-router'
|
|
import { MyText, RefreshProvider } from 'common-ui'
|
|
|
|
import Dashboard from '@/app/(drawer)/(tabs)/home/index'
|
|
import CentralStoreInitializer from '@/src/components/CentralStoreInitializer'
|
|
import { useNavigationStore } from '@/src/store/navigationStore'
|
|
import { trpc, trpcClient } from '@/src/trpc-client'
|
|
import { mockSlots, outOfStockIds, resetStorage, setupApiMocks } from '../mocks/api'
|
|
import productsResponse from '../mocks/mock-products-response.json'
|
|
import storesResponse from '../mocks/mock-stores-response.json'
|
|
|
|
const products = productsResponse.products.filter((p) => !p.isComboOnly)
|
|
|
|
function Providers({ children }: { children?: React.ReactNode }) {
|
|
const queryClient = React.useMemo(
|
|
() =>
|
|
new QueryClient({
|
|
defaultOptions: { queries: { retry: false, gcTime: 0 } },
|
|
}),
|
|
[]
|
|
)
|
|
|
|
return (
|
|
<trpc.Provider client={trpcClient} queryClient={queryClient}>
|
|
<QueryClientProvider client={queryClient}>
|
|
<RefreshProvider queryClient={queryClient}>
|
|
<CentralStoreInitializer>{children}</CentralStoreInitializer>
|
|
</RefreshProvider>
|
|
</QueryClientProvider>
|
|
</trpc.Provider>
|
|
)
|
|
}
|
|
|
|
const HomeRoute = () => (
|
|
<Providers>
|
|
<Dashboard />
|
|
</Providers>
|
|
)
|
|
|
|
const DetailStub = () => {
|
|
const { id } = useLocalSearchParams<{ id: string }>()
|
|
return <MyText testID="product-detail-screen">{String(id)}</MyText>
|
|
}
|
|
|
|
const SlotViewStub = () => {
|
|
const { slotId } = useLocalSearchParams<{ slotId: string }>()
|
|
return <MyText testID="slot-view-screen">{String(slotId)}</MyText>
|
|
}
|
|
|
|
const StoresStub = () => <MyText testID="stores-screen">stores</MyText>
|
|
const SearchStub = () => <MyText testID="search-screen">search</MyText>
|
|
const CartStub = () => <MyText testID="cart-screen">cart</MyText>
|
|
|
|
const ROUTES = {
|
|
'(drawer)/(tabs)/home/index': HomeRoute,
|
|
'(drawer)/(tabs)/home/product-detail/[id]': DetailStub,
|
|
'(drawer)/(tabs)/home/slot-view/index': SlotViewStub,
|
|
'(drawer)/(tabs)/home/search-results/index': SearchStub,
|
|
'(drawer)/(tabs)/home/cart': CartStub,
|
|
'(drawer)/(tabs)/stores/index': StoresStub,
|
|
}
|
|
|
|
async function renderHomeRoute() {
|
|
const result = renderRouter(ROUTES, { initialUrl: '/home' })
|
|
|
|
await screen.findByTestId(`tag-tab-${productsResponse.tags[0].id}`)
|
|
|
|
return result
|
|
}
|
|
|
|
describe('Home navigation (real router)', () => {
|
|
beforeEach(async () => {
|
|
setupApiMocks()
|
|
await resetStorage()
|
|
useNavigationStore.setState({
|
|
isNavigatedFromHome: false,
|
|
selectedStoreId: null,
|
|
})
|
|
})
|
|
|
|
it('routes to the product detail screen carrying the id', async () => {
|
|
const nav = await renderHomeRoute()
|
|
|
|
// Must be in stock: unavailable products sort last and are not mounted.
|
|
const productId = products.find((p) => !outOfStockIds.has(p.id))!.id
|
|
const card = await screen.findByTestId(`product-item-${productId}`)
|
|
|
|
fireEvent.press(within(card).getByTestId('product-card'))
|
|
|
|
expect(nav.getPathname()).toBe(`/home/product-detail/${productId}`)
|
|
expect(screen.getByTestId('product-detail-screen')).toHaveTextContent(
|
|
String(productId)
|
|
)
|
|
})
|
|
|
|
it('routes to the slot view carrying the slotId', async () => {
|
|
const nav = await renderHomeRoute()
|
|
|
|
const slot = mockSlots.slots.find(
|
|
(s) => new Date(s.freezeTime).getTime() > Date.now()
|
|
)!
|
|
|
|
fireEvent.press(screen.getByTestId(`slot-card-${slot.id}`))
|
|
|
|
expect(nav.getPathname()).toBe('/home/slot-view')
|
|
expect(nav.getSearchParams()).toMatchObject({ slotId: String(slot.id) })
|
|
expect(screen.getByTestId('slot-view-screen')).toHaveTextContent(
|
|
String(slot.id)
|
|
)
|
|
})
|
|
|
|
it('routes to the stores screen', async () => {
|
|
const nav = await renderHomeRoute()
|
|
|
|
fireEvent.press(screen.getByTestId(`store-item-${storesResponse.stores[0].id}`))
|
|
|
|
expect(nav.getPathname()).toBe('/stores')
|
|
expect(screen.getByTestId('stores-screen')).toBeVisible()
|
|
})
|
|
})
|