/** * Cart money math (components/cart-page.tsx) — the arithmetic that decides what * the user is quoted before checkout: item total, savings, delivery charge and * the grand total, computed over a real local cart. * * Coupon application and the discount formula live in * tests/screens/cart-coupons.test.tsx. This file covers the rest of the bill. * Known display bugs are pinned and listed in tests/KNOWN-GAPS.md. */ jest.mock('expo-router', () => require('../mocks/router').expoRouterMock()) import React from 'react' import { Alert } from 'react-native' import { 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 { renderWithProviders } from '../helpers/render' /** * Prices and flags come from the availability cache, which the product merge * treats as the source of truth (useAllProducts). * SKU_BREAST 215 / market 220 in stock * SKU_LOLLIPOP 99 / market 120 OUT OF STOCK * SKU_MUTTON 839 in stock * SKU_CUCUMBER 15 / market 19 in stock * SKU_CHICKEN price 15, flashPrice 19 (flash is dearer than regular) * SKU_LIVER price 99, flashPrice null (flash falls back to the price) */ const SKU_BREAST = 2 const SKU_LOLLIPOP = 3 const SKU_MUTTON = 4 const SKU_CUCUMBER = 5 const SKU_CHICKEN = 19 const SKU_LIVER = 1 const SLOT_A = 1849 // consts fixture: freeDeliveryThreshold 249, deliveryCharge 15. const futureSlot = () => ({ id: SLOT_A, deliveryTime: dayjs().add(3, 'hour').toISOString(), freezeTime: dayjs().add(2, 'hour').toISOString(), }) const line = (id: number, skuId: number, quantity = 1) => ({ id, skuId, quantity, slotId: SLOT_A, addedAt: new Date().toISOString(), }) const seedCart = async (items: unknown[], key = 'cart_items') => StorageServiceCasual.setItem(key, JSON.stringify(items)) const procedures = { 'user.cart.getCartSlots': Object.fromEntries( [SKU_BREAST, SKU_LOLLIPOP, SKU_MUTTON, SKU_CUCUMBER, SKU_CHICKEN, SKU_LIVER].map( (sku) => [sku, [futureSlot()]] ) ), 'user.coupon.getEligible': { data: [] }, } describe('cart money math', () => { beforeEach(async () => { resetAuthMock() authMock.user = { id: 1 } await resetStorage() jest.spyOn(Alert, 'alert').mockImplementation(() => {}) }) it('charges the delivery fee below the free threshold', async () => { setupApiMocks({ procedures }) await seedCart([line(1, SKU_BREAST)]) renderWithProviders() await waitFor(() => expect(screen.getByText('Item Total')).toBeVisible()) // 215 < 249 -> +15 delivery -> 230. 230 is unique to the To Pay row. expect(screen.getByText('₹15')).toBeVisible() expect(screen.getByText('₹230')).toBeVisible() expect(screen.queryByText('₹235')).toBeNull() }) it('waives the delivery fee at or above the free threshold', async () => { setupApiMocks({ procedures }) await seedCart([line(1, SKU_MUTTON)]) renderWithProviders() await waitFor(() => expect(screen.getByText('Item Total')).toBeVisible()) // 839 >= 249 -> free delivery. The line, the item total and the grand // total all read 839. expect(screen.getByText('Free')).toBeVisible() expect(screen.getAllByText('₹839')).toHaveLength(3) }) it('excludes out-of-stock lines from the item total and the payable amount', async () => { setupApiMocks({ procedures }) await seedCart([line(1, SKU_BREAST), line(2, SKU_LOLLIPOP)]) renderWithProviders() await waitFor(() => expect(screen.getByText('Item Total')).toBeVisible()) // The out-of-stock line is still listed and flagged, but does not count. expect(screen.getByText('Out of Stock')).toBeVisible() expect(screen.getByText('₹15')).toBeVisible() expect(screen.getByText('₹230')).toBeVisible() // 314 + 15 is what the user would have paid had the OOS line counted. expect(screen.queryByText('₹329')).toBeNull() }) it('totals several lines by price x quantity', async () => { setupApiMocks({ procedures }) await seedCart([line(1, SKU_BREAST), line(2, SKU_CUCUMBER, 3)]) renderWithProviders() await waitFor(() => expect(screen.getByText('Item Total')).toBeVisible()) // 215 + 15 * 3 = 260 >= 249 -> free delivery. Item total and grand total // both read 260. expect(screen.getByText('Free')).toBeVisible() expect(screen.getAllByText('₹260')).toHaveLength(2) }) it('shows the savings row from the market-price gap', async () => { setupApiMocks({ procedures }) // 220 - 215 = 5 saved on the one line. await seedCart([line(1, SKU_BREAST)]) renderWithProviders() await waitFor(() => expect(screen.getByText('Total Discount')).toBeVisible()) expect(screen.getByText('₹5')).toBeVisible() }) describe('flash cart pricing', () => { it('prices the flash cart from flashPrice, not the regular price', async () => { setupApiMocks({ procedures }) // SKU_CHICKEN: price 15, flashPrice 19. await seedCart([line(1, SKU_CHICKEN, 2)], 'flash_cart_items') renderWithProviders() await waitFor(() => expect(screen.getByText('Item Total')).toBeVisible()) // 19 * 2 = 38, not 15 * 2 = 30. Flash also has its own free-delivery // threshold (399), so the 39 fee applies. expect(screen.getAllByText('₹38')).toHaveLength(2) expect(screen.getByText('₹39')).toBeVisible() expect(screen.getByText('₹77')).toBeVisible() }) it('falls back to the regular price when flashPrice is absent', async () => { setupApiMocks({ procedures }) // SKU_LIVER: price 99, flashPrice null. await seedCart([line(1, SKU_LIVER)], 'flash_cart_items') renderWithProviders() await waitFor(() => expect(screen.getByText('Item Total')).toBeVisible()) expect(screen.getAllByText('₹99')).toHaveLength(2) }) }) })