freshyo/apps/user-ui/tests/pages/flash-checkout.test.tsx
2026-10-20 16:13:09 +05:30

105 lines
4.4 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Thin route coverage for the flash funnel:
* - flash cart (`flash-delivery/(cart)/cart.tsx`) — CART-17
* - flash checkout (`flash-delivery/checkout.tsx`) — CHECK-09/28
* - flash order success (`flash-delivery/order-success.tsx`) — SUCCESS-08..10
*
* The detailed assertions live in cart.test.tsx / checkout.test.tsx /
* order-success.test.tsx; this file only proves the routes are wired.
*/
import * as React from 'react'
import { QueryClient } from '@tanstack/react-query'
import { fireEvent } from '@testing-library/react-native'
import FlashCart from '@/app/(drawer)/(tabs)/flash-delivery/(cart)/cart'
import FlashCheckout from '@/app/(drawer)/(tabs)/flash-delivery/checkout'
import FlashOrderSuccess from '@/app/(drawer)/(tabs)/flash-delivery/order-success'
import { flush, renderWithProviders, seedBoot } from '../helpers/test-utils'
import { resetAllMocks } from '../helpers/reset'
import { lastProcedureInput, setProcedure } from '../mocks/trpc-client'
import { __set, __snapshot } from '../mocks/secure-store-mock'
import { __setParams, router } from '../mocks/expo-router-mock'
import { A1, CART_FLASH, CART_REG, CART_SLOTS_RESPONSE } from '../fixtures'
const FLASH_KEY = 'flash_cart_items'
function createClient(): QueryClient {
return new QueryClient({
defaultOptions: {
queries: {
retry: false,
gcTime: Infinity,
staleTime: Infinity,
refetchOnWindowFocus: false,
},
mutations: { retry: false, gcTime: 0 },
},
})
}
async function mount(ui: React.ReactElement, params: Record<string, any> = {}) {
seedBoot({ addressId: A1.id })
setProcedure('user.cart.getCartSlots', { data: CART_SLOTS_RESPONSE })
__set(FLASH_KEY, CART_FLASH)
__setParams(params)
const result = renderWithProviders(ui, { auth: 'in', queryClient: createClient() })
await flush(5)
return result
}
describe('Flash funnel routes', () => {
beforeEach(() => resetAllMocks())
it('FLASH-01: the flash cart route shows "1 Hr Delivery Cart" and flash prices (CART-17)', async () => {
const { getByText, getAllByText } = await mount(<FlashCart />)
expect(getByText('1 Hr Delivery Cart')).toBeTruthy()
expect(getAllByText('₹75').length).toBeGreaterThan(0)
})
it('FLASH-02: the flash checkout route shows the header and one unlabeled group (CHECK-09)', async () => {
const { getByTestId, queryAllByTestId } = await mount(<FlashCheckout />)
expect(getByTestId('checkout-header-title')).toHaveTextContent('1 Hr Delivery Checkout')
expect(getByTestId('checkout-slot-group-flash')).toBeTruthy()
expect(queryAllByTestId(/^checkout-slot-label-/)).toHaveLength(0)
})
it('FLASH-03: flash checkout prices items, delivery and total from the flash config (CHECK-28)', async () => {
const { getByTestId } = await mount(<FlashCheckout />)
expect(getByTestId('bill-item-total-amount')).toHaveTextContent('₹75')
expect(getByTestId('bill-delivery-amount')).toHaveTextContent('₹25')
expect(getByTestId('bill-to-pay-amount')).toHaveTextContent('₹100')
})
it('FLASH-04: placing a flash order sends slotId null, sets the flag and clears the flash cart', async () => {
__set('cart_items', CART_REG)
const { getByTestId } = await mount(<FlashCheckout />)
fireEvent.press(getByTestId('place-order-button'))
await flush(5)
expect(lastProcedureInput('user.order.placeOrder')).toMatchObject({
selectedItems: [{ skuId: 8, quantity: 1, slotId: null }],
addressId: A1.id,
regionId: 1,
isFlashDelivery: true,
})
expect(JSON.parse(__snapshot()[FLASH_KEY])).toEqual([])
expect(__snapshot()['cart_items']).toBe(CART_REG)
})
it('FLASH-05: the flash success copy and Continue Shopping route (SUCCESS-08/09)', async () => {
const { getByText, getByTestId } = await mount(<FlashOrderSuccess />)
expect(getByText('1 Hr Order Placed!')).toBeTruthy()
fireEvent.press(getByTestId('order-success-continue'))
expect(router.dismissTo).toHaveBeenCalledWith('/(drawer)/(tabs)/flash-delivery')
})
it('FLASH-06: the flash success View My Orders route (SUCCESS-10)', async () => {
const { getByTestId } = await mount(<FlashOrderSuccess />)
fireEvent.press(getByTestId('order-success-view-orders'))
expect(router.dismissAll).toHaveBeenCalled()
expect(router.push).toHaveBeenCalledWith('/(drawer)/(tabs)/me/my-orders')
})
})