/** * Cart screen (components/cart-page.tsx, mounted by home/cart + flash cart). * * Cart data is local (StorageServiceCasual); the screen also queries * user.cart.getCartSlots (per-product slots) and user.coupon.getEligible. */ jest.mock('expo-router', () => require('../mocks/router').expoRouterMock()) import React from 'react' import { Alert } from 'react-native' import { fireEvent, screen, waitFor } from '@testing-library/react-native' import dayjs from 'dayjs' import { StorageServiceCasual } from 'common-ui/src/services/StorageServiceCasual' import CartPage from '@/components/cart-page' import { resetStorage, setupApiMocks } from '../mocks/api' import { authMock, resetAuthMock } from '../mocks/auth' import { expectProcedureCalled, lastProcedureInput } from '../mocks/procedures' import { routerMock } from '../mocks/router' import { renderWithProviders } from '../helpers/render' const PRODUCT_ID = 2 // Chicken Breast 500 g, price 215 (in stock, in a slot) const OTHER_PRODUCT_ID = 40 // Chicken regular cut Large pieces (in stock) const SLOT_ID = 1849 const OTHER_SLOT_ID = 1850 const futureSlot = () => ({ id: SLOT_ID, deliveryTime: dayjs().add(3, 'hour').toISOString(), freezeTime: dayjs().add(2, 'hour').toISOString(), }) const otherFutureSlot = () => ({ id: OTHER_SLOT_ID, deliveryTime: dayjs().add(5, 'hour').toISOString(), freezeTime: dayjs().add(4, 'hour').toISOString(), }) async function seedCart(items: unknown[]) { await StorageServiceCasual.setItem('cart_items', JSON.stringify(items)) } const cartItem = (over: Record = {}) => ({ id: 1, skuId: PRODUCT_ID, quantity: 1, slotId: SLOT_ID, addedAt: new Date().toISOString(), ...over, }) describe('Cart screen', () => { beforeEach(async () => { resetAuthMock() // Signed in by default: the cart screen shows a login wall when signed out, // and the first-address gate when the user has zero addresses. Individual // cases flip `authMock.user` / `getUserAddresses` to exercise those. authMock.user = { id: 1 } await resetStorage() jest.spyOn(Alert, 'alert').mockImplementation(() => {}) }) it('shows the empty state and Start Shopping navigates home', async () => { setupApiMocks() renderWithProviders(, { centralStores: true }) await waitFor(() => expect(screen.getByText('Your cart is empty')).toBeVisible()) fireEvent.press(screen.getByText('Start Shopping')) expect(routerMock.push).toHaveBeenCalledWith('/(drawer)/(tabs)/home') }) it('renders the seeded line, the bill and queries the cart slots', async () => { const api = setupApiMocks({ procedures: { 'user.cart.getCartSlots': { [PRODUCT_ID]: [futureSlot()] } }, }) await seedCart([cartItem()]) renderWithProviders() await waitFor(() => expect(screen.getByText('Chicken Breast 500 g')).toBeVisible()) expect(screen.getByText('Scheduled Delivery Cart')).toBeVisible() expect(screen.getByText('To Pay')).toBeVisible() // Item 215 < threshold 249 -> +15 delivery. expect(screen.getByText('₹230')).toBeVisible() await waitFor(() => { const calls = expectProcedureCalled(api.fetchMock, 'user.cart.getCartSlots') expect(lastProcedureInput(api.fetchMock, 'user.cart.getCartSlots')).toEqual({ productIds: [PRODUCT_ID], }) expect(calls.length).toBeGreaterThan(0) }) }) it('queries eligible coupons only when authenticated', async () => { authMock.user = { id: 1 } const api = setupApiMocks({ procedures: { 'user.cart.getCartSlots': { [PRODUCT_ID]: [futureSlot()] }, 'user.coupon.getEligible': { data: [] }, }, }) await seedCart([cartItem()]) renderWithProviders() await waitFor(() => expect(screen.getByText('Chicken Breast 500 g')).toBeVisible()) await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.coupon.getEligible')) expect(screen.getByText('Offers & Coupons')).toBeVisible() }) it('navigates to checkout with the selected slot', async () => { setupApiMocks({ procedures: { 'user.cart.getCartSlots': { [PRODUCT_ID]: [futureSlot()] } }, }) await seedCart([cartItem()]) renderWithProviders() await waitFor(() => expect(screen.getByText('Chicken Breast 500 g')).toBeVisible()) fireEvent.press(screen.getByTestId('checkout-button')) await waitFor(() => expect(routerMock.push).toHaveBeenCalledWith( expect.stringContaining('/(drawer)/(tabs)/home/checkout') ) ) }) it('shows an enabled merge row when every item can use the majority slot', async () => { setupApiMocks({ procedures: { 'user.cart.getCartSlots': { [PRODUCT_ID]: [futureSlot(), otherFutureSlot()], }, }, }) await seedCart([ cartItem({ id: 1, slotId: SLOT_ID }), cartItem({ id: 2, slotId: OTHER_SLOT_ID }), ]) renderWithProviders() await waitFor(() => expect(screen.getAllByText('Chicken Breast 500 g').length).toBeGreaterThan(0) ) await waitFor(() => expect(screen.getByTestId('delivery-slot-merge')).toBeVisible()) expect(screen.getByText(/Deliver All items at/)).toBeVisible() expect(screen.getByTestId('delivery-slot-merge-apply')).toBeVisible() fireEvent.press(screen.getByTestId('delivery-slot-merge-apply')) // Merging collapses to one slot, so the whole merge row is hidden. await waitFor(() => expect(screen.queryByTestId('delivery-slot-merge')).toBeNull()) }) it('hides the merge row when one item cannot use the majority slot', async () => { setupApiMocks({ procedures: { 'user.cart.getCartSlots': { [PRODUCT_ID]: [futureSlot(), otherFutureSlot()], [OTHER_PRODUCT_ID]: [otherFutureSlot()], }, }, }) // Majority (lowest id) is SLOT_ID, which OTHER_PRODUCT_ID cannot use. await seedCart([ cartItem({ id: 1, slotId: SLOT_ID }), cartItem({ id: 2, skuId: OTHER_PRODUCT_ID, slotId: OTHER_SLOT_ID }), ]) renderWithProviders() await waitFor(() => expect(screen.getByTestId('delivery-slot-summary')).toBeVisible()) expect(screen.queryByTestId('delivery-slot-merge')).toBeNull() expect(screen.queryByTestId('delivery-slot-merge-apply')).toBeNull() }) it('hides the merge row when only one slot is selected', async () => { setupApiMocks({ procedures: { 'user.cart.getCartSlots': { [PRODUCT_ID]: [futureSlot()] } }, }) await seedCart([cartItem()]) renderWithProviders() await waitFor(() => expect(screen.getByText('Chicken Breast 500 g')).toBeVisible()) await waitFor(() => expect(screen.getByTestId('delivery-slot-summary')).toBeVisible()) expect(screen.queryByTestId('delivery-slot-merge')).toBeNull() }) it('renders the flash cart variant without slot selection', async () => { setupApiMocks() await StorageServiceCasual.setItem( 'flash_cart_items', JSON.stringify([cartItem({ slotId: 0 })]) ) renderWithProviders() await waitFor(() => expect(screen.getByText('1 Hr Delivery Cart')).toBeVisible()) expect(screen.queryByText('Scheduled Delivery Cart')).toBeNull() }) it('shows the login wall when signed out', async () => { authMock.user = null setupApiMocks() await seedCart([cartItem()]) renderWithProviders() await waitFor(() => expect(screen.getByText('Please log in')).toBeVisible()) expect(screen.getByText('Log In')).toBeVisible() expect(screen.queryByText('Chicken Breast 500 g')).toBeNull() }) it('shows the add-address gate before the cart when the user has no addresses', async () => { setupApiMocks({ procedures: { 'user.cart.getCartSlots': { [PRODUCT_ID]: [futureSlot()] }, 'user.address.getUserAddresses': { data: [] }, }, }) await seedCart([cartItem()]) renderWithProviders() await waitFor(() => expect(screen.getByText('Where should we deliver?')).toBeVisible()) expect(screen.getByTestId('cart-address-other')).toBeVisible() expect(screen.queryByText('Chicken Breast 500 g')).toBeNull() }) it('shows the add-address gate even when the cart is empty', async () => { setupApiMocks({ procedures: { 'user.address.getUserAddresses': { data: [] } }, }) renderWithProviders() await waitFor(() => expect(screen.getByText('Where should we deliver?')).toBeVisible()) expect(screen.queryByText('Your cart is empty')).toBeNull() }) })