freshyo/apps/user-ui/tests/screens/checkout-crash.test.tsx
2026-09-26 17:50:33 +05:30

100 lines
3.7 KiB
TypeScript

/**
* Checkout page (components/checkout-page.tsx) — the guard branches plus the
* resolved-bill path.
*
* FIXED (this crash is no longer in tests/KNOWN-GAPS.md): the file used to call
* `trpc.user.coupon.getEligible.useQuery` and four `useMemo`s AFTER its early return
* for the empty-selection case, so the hook count changed (4 -> 9) as soon as
* `useGetCart` resolved from `[]` to real lines and React threw "Rendered more hooks
* than during the previous render" — the screen could not render at all with slotted
* items. The hooks now sit above the guard; the last case asserts the bill that the
* crash used to prevent.
*/
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
import React from 'react'
import { screen, waitFor } from '@testing-library/react-native'
import { StorageServiceCasual } from 'common-ui/src/services/StorageServiceCasual'
import CheckoutPage from '@/components/checkout-page'
import { resetStorage, setupApiMocks } from '../mocks/api'
import { authMock, resetAuthMock } from '../mocks/auth'
import { searchParamsMock } from '../mocks/router'
import { renderWithProviders } from '../helpers/render'
const SKU_BREAST = 2 // ₹215, in stock
const SKU_LOLLIPOP = 3 // isFlashAvailable: false
const SLOT_A = 1849
const line = (id: number, skuId: number, over: Record<string, unknown> = {}) => ({
id,
skuId,
quantity: 1,
slotId: SLOT_A,
addedAt: new Date().toISOString(),
...over,
})
const procedures = {
'user.address.getUserAddresses': {
data: [{ id: 1, name: 'Home', isDefault: true, addressLine1: 'A St' }],
},
'user.slots.getSlots': { slots: [] },
'user.coupon.getEligible': { data: [] },
}
describe('Checkout page guards', () => {
beforeEach(async () => {
resetAuthMock()
authMock.user = { id: 1 }
await resetStorage()
searchParamsMock.current = {}
setupApiMocks({ procedures })
})
it('asks for delivery slots when the cart lines have none', async () => {
await StorageServiceCasual.setItem('cart_items', JSON.stringify([line(1, SKU_BREAST)]))
renderWithProviders(<CheckoutPage />)
await waitFor(() => expect(screen.getByText('No items to checkout')).toBeVisible())
expect(
screen.getByText('Please select delivery slots for your items')
).toBeVisible()
})
it('reports flash-unavailable lines on the flash checkout', async () => {
await StorageServiceCasual.setItem(
'flash_cart_items',
JSON.stringify([line(1, SKU_LOLLIPOP, { slotId: 0 })])
)
renderWithProviders(<CheckoutPage isFlashDelivery />)
await waitFor(() => expect(screen.getByText('No items to checkout')).toBeVisible())
expect(
screen.getByText('None of your cart items are available for flash delivery')
).toBeVisible()
})
it('shows the empty-cart state when there is nothing to pay for', async () => {
renderWithProviders(<CheckoutPage />)
await waitFor(() => expect(screen.getByText('Your cart is empty')).toBeVisible())
})
it('renders the bill once the cart resolves to slotted items', async () => {
await StorageServiceCasual.setItem('cart_items', JSON.stringify([line(1, SKU_BREAST)]))
searchParamsMock.current = { slots: `${SLOT_A}:1` }
renderWithProviders(<CheckoutPage />)
// This mount used to throw "Rendered more hooks than during the previous render."
await waitFor(() => expect(screen.getByText('To Pay')).toBeVisible())
expect(screen.getByText('Scheduled Delivery Checkout')).toBeVisible()
expect(screen.getByText('₹215')).toBeVisible() // Item Total: 1 x 215
expect(screen.getByText('₹230')).toBeVisible() // + 15 delivery, under the 249 threshold
expect(screen.queryByText('Product Discount')).toBeNull()
})
})