126 lines
4.2 KiB
TypeScript
126 lines
4.2 KiB
TypeScript
/**
|
|
* 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 {
|
|
fireEvent,
|
|
render,
|
|
screen,
|
|
waitFor,
|
|
within,
|
|
} from '@testing-library/react-native'
|
|
import dayjs from 'dayjs'
|
|
|
|
import Dashboard from '@/app/(drawer)/(tabs)/home/index'
|
|
import availability from '../mocks/mock-availability-response.json'
|
|
import productsResponse from '../mocks/mock-products-response.json'
|
|
import { mockSlots } from '../mocks/api'
|
|
// The provider stack moved to tests/helpers/render.tsx; re-exported so the
|
|
// home-page tests keep importing everything from './render'.
|
|
import { Providers } from '../helpers/render'
|
|
|
|
export { Providers }
|
|
|
|
/** 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<number, number>(
|
|
availability.availability.map((a) => [a.id, Number(a.price)])
|
|
)
|
|
export const flashAvailableById = new Map<number, boolean>(
|
|
availability.availability.map((a) => [a.id, a.isFlashAvailable])
|
|
)
|
|
export const marketPriceById = new Map<number, number | null>(
|
|
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 renderHome() {
|
|
return render(
|
|
<Providers>
|
|
<Dashboard />
|
|
</Providers>
|
|
)
|
|
}
|
|
|
|
/** 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'))
|
|
}
|