/** * Shared render helpers + expectations for the mocked-router home-page tests. * * Unlike tests/home-page/navigation.test.tsx (which drives the real router), * every file that imports this must also declare the mocked router itself: * * jest.mock('expo-router', () => require('../mocks/router').expoRouterMock()) * * The provider stack mirrors app/_layout.tsx: CentralStoreInitializer populates * the slot/availability/product stores the same way the app does, so * productSlotsMap is non-empty and the out-of-stock / add-to-cart paths work. */ import React from 'react' import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { fireEvent, render, screen, waitFor, within, } from '@testing-library/react-native' import { RefreshProvider } from 'common-ui' import dayjs from 'dayjs' import Dashboard from '@/app/(drawer)/(tabs)/home/index' import CentralStoreInitializer from '@/src/components/CentralStoreInitializer' import { trpc, trpcClient } from '@/src/trpc-client' import availability from '../mocks/mock-availability-response.json' import productsResponse from '../mocks/mock-products-response.json' import { mockSlots } from '../mocks/api' /** Products the page can show: useAllProducts drops combo-only SKUs. */ export const products = productsResponse.products.filter((p) => !p.isComboOnly) export const tags = productsResponse.tags export const knownProductIds = new Set(products.map((p) => p.id)) /** price/isFlashAvailable as the app sees them (from the availability cache). */ export const priceById = new Map( availability.availability.map((a) => [a.id, Number(a.price)]) ) export const flashAvailableById = new Map( availability.availability.map((a) => [a.id, a.isFlashAvailable]) ) export const marketPriceById = new Map( availability.availability.map((a) => [ a.id, a.marketPrice == null ? null : Number(a.marketPrice), ]) ) /** Mirrors the backend: a slot is offerable only while both times are future. */ export const isUpcomingSlot = (slot: { deliveryTime: string freezeTime: string }) => dayjs(slot.deliveryTime).isAfter(dayjs()) && dayjs(slot.freezeTime).isAfter(dayjs()) /** Upcoming slots in the order the home page renders them (delivery asc). */ export const upcomingSlots = () => mockSlots.slots.filter(isUpcomingSlot).sort((a, b) => { const byDelivery = dayjs(a.deliveryTime).diff(dayjs(b.deliveryTime)) return byDelivery !== 0 ? byDelivery : dayjs(a.freezeTime).diff(dayjs(b.freezeTime)) }) /** Products a tag points at that exist in the (combo-filtered) cache. */ export const productIdsForTag = (tag: { productIds: number[] }) => tag.productIds.filter((id) => knownProductIds.has(id)) export function Providers({ children }: { children?: React.ReactNode }) { const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 0 } }, }) return ( {children} ) } export function renderHome() { return render( ) } /** Renders and waits for the first tag tab and the slot strip to be live. */ export async function renderLoadedHome() { const result = renderHome() await waitFor(() => { expect(screen.getByTestId(`tag-tab-${tags[0].id}`)).toBeTruthy() }) await waitFor(() => { expect(screen.getAllByTestId(/^slot-card-/).length).toBeGreaterThan(0) }) return result } /** Selects a tag and expands its grid past the 6-item preview. */ export function expandTag(tagId: number) { fireEvent.press(screen.getByTestId(`tag-tab-${tagId}`)) const expand = screen.queryByTestId(`tag-show-more-${tagId}`) if (expand) fireEvent.press(expand) } /** * Adds a product from the tag grid: add icon -> first open slot -> confirm. * The dialog only offers slots for the product whose freezeTime is still ahead. */ export function addToCartFromTagGrid(productId: number) { fireEvent.press( within(screen.getByTestId(`tag-product-${productId}`)).getByTestId( 'add-to-cart-icon' ) ) const slot = mockSlots.slots.find( (s) => s.products.some((p) => p.id === productId) && dayjs(s.freezeTime).isAfter(dayjs()) ) if (!slot) throw new Error(`[tests] no open slot for product ${productId}`) fireEvent.press( screen.getByTestId( `slot-${dayjs(slot.deliveryTime).format('YYYY-MM-DD-HH-mm')}` ) ) fireEvent.press(screen.getByTestId('add-to-cart-confirm')) }