freshyo/apps/user-ui/tests/home-page/home-slots.test.tsx
2026-09-26 00:46:08 +05:30

100 lines
3.3 KiB
TypeScript

/**
* Slot card details beyond the ordering coverage in home.test.tsx: the
* CLOSING SOON badge (freeze within 4 hours) and the delivery / order-by labels.
*
* The slot fixture is rebased so the earliest delivery is 2h out; because the
* rebase is uniform, the earliest two slots freeze within 4h and the rest do
* not, giving both sides of the badge rule deterministically.
*/
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
import { screen, within } from '@testing-library/react-native'
import dayjs from 'dayjs'
import { resetStorage, setupApiMocks } from '../mocks/api'
import { useNavigationStore } from '@/src/store/navigationStore'
import { renderLoadedHome, upcomingSlots } from './render'
/** Mirrors formatTimeRange in the page / SlotCard. */
const formatTimeRange = (deliveryTime: string) => {
const time = dayjs(deliveryTime)
const endTime = time.add(1, 'hour')
const startPeriod = time.format('A')
const endPeriod = endTime.format('A')
if (startPeriod === endPeriod) {
return `${time.format('h')}-${endTime.format('h')} ${startPeriod}`
}
return `${time.format('h:mm')} ${startPeriod} - ${endTime.format('h:mm')} ${endPeriod}`
}
describe('Home page slots', () => {
beforeEach(async () => {
setupApiMocks()
await resetStorage()
useNavigationStore.setState({
isNavigatedFromHome: false,
selectedStoreId: null,
})
})
// The strip renders only the first 5 upcoming slots.
const renderedSlots = () => upcomingSlots().slice(0, 5)
it('marks only the slots closing within 4 hours', async () => {
await renderLoadedHome()
const slots = renderedSlots()
expect(slots.length).toBeGreaterThan(0)
let withBadge = 0
for (const slot of slots) {
const card = screen.getByTestId(`slot-card-${slot.id}`)
const freezeTime = dayjs(slot.freezeTime)
const closingSoon =
freezeTime.diff(dayjs(), 'hour') < 4 && freezeTime.isAfter(dayjs())
expect(within(card).queryByText('CLOSING SOON') !== null).toBe(closingSoon)
if (closingSoon) withBadge += 1
}
// Both branches are exercised by the fixture.
expect(withBadge).toBeGreaterThan(0)
expect(withBadge).toBeLessThan(slots.length)
})
it('labels each slot with its delivery range and order-by time', async () => {
await renderLoadedHome()
for (const slot of renderedSlots()) {
const card = screen.getByTestId(`slot-card-${slot.id}`)
// Delivery range and order-by clock time are each unique within the card.
expect(
within(card).getByText(formatTimeRange(slot.deliveryTime))
).toBeVisible()
expect(
within(card).getByText(dayjs(slot.freezeTime).format('h:mm A'))
).toBeVisible()
// The date line is shared by "Delivery At" and "Order By", so it appears
// twice when both fall on the same day.
expect(
within(card).getAllByText(dayjs(slot.deliveryTime).format('ddd, MMM DD'))
.length
).toBeGreaterThan(0)
}
})
it('shows how many products each slot holds', async () => {
await renderLoadedHome()
for (const slot of renderedSlots()) {
const card = screen.getByTestId(`slot-card-${slot.id}`)
expect(
within(card).getByText(`View all ${slot.products.length} items`)
).toBeVisible()
}
})
})