138 lines
5 KiB
TypeScript
138 lines
5 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Section 24 — FloatingCartBar (FCB-01..05).
|
|
*
|
|
* The bar reads the local cart through `useGetCart`, so lines come from the
|
|
* storage mock's `cart_items` / `flash_cart_items` keys and the central stores
|
|
* supply the product names/prices (`seedBoot`).
|
|
*/
|
|
import * as React from 'react'
|
|
import { StyleSheet, View } from 'react-native'
|
|
import { QueryClient } from '@tanstack/react-query'
|
|
import { fireEvent, waitFor } from '@testing-library/react-native'
|
|
import FloatingCartBar from '@/components/floating-cart-bar'
|
|
import { useCentralProductStore } from '@/src/store/centralProductStore'
|
|
import { renderWithProviders, seedBoot, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { __set, __snapshot } from '../mocks/secure-store-mock'
|
|
import { CART_REG, CART_FLASH } from '../fixtures'
|
|
|
|
/** 10% capped ₹40 on Chicken Breast (sku 2). */
|
|
const CHICKEN_OFFER = {
|
|
couponId: 24,
|
|
couponCode: 'TARGET10',
|
|
discountPercent: '10',
|
|
flatDiscount: null,
|
|
maxValue: 40,
|
|
}
|
|
|
|
/**
|
|
* Same defaults as the shared harness client, except mutations use `gcTime: 0`:
|
|
* React Query otherwise schedules a 5-minute GC timeout on unmount, which keeps
|
|
* the jest process alive after the run has finished.
|
|
*/
|
|
function createCartQueryClient(): QueryClient {
|
|
return new QueryClient({
|
|
defaultOptions: {
|
|
queries: {
|
|
retry: false,
|
|
gcTime: Infinity,
|
|
staleTime: Infinity,
|
|
refetchOnWindowFocus: false,
|
|
},
|
|
mutations: { retry: false, gcTime: 0 },
|
|
},
|
|
})
|
|
}
|
|
|
|
async function renderBar(props: React.ComponentProps<typeof FloatingCartBar> = {}) {
|
|
seedBoot()
|
|
const utils = renderWithProviders(<FloatingCartBar {...props} />, {
|
|
auth: false,
|
|
queryClient: createCartQueryClient(),
|
|
})
|
|
await flush(2)
|
|
return utils
|
|
}
|
|
|
|
describe('FloatingCartBar', () => {
|
|
beforeEach(() => resetAllMocks())
|
|
|
|
it('FCB-01: a controlled isExpanded prop makes the parent own the sheet', async () => {
|
|
__set('cart_items', CART_REG)
|
|
const { getByText, queryByTestId } = await renderBar({ isExpanded: true })
|
|
|
|
expect(getByText('Your Cart')).toBeTruthy()
|
|
// The collapsed bar is not rendered while expanded.
|
|
expect(queryByTestId('go-to-cart')).toBeNull()
|
|
})
|
|
|
|
it('FCB-02: tapping the collapsed bar calls the controlled setIsExpanded with true', async () => {
|
|
__set('cart_items', CART_REG)
|
|
const setIsExpanded = jest.fn()
|
|
const { getByText, queryByText } = await renderBar({ isExpanded: false, setIsExpanded })
|
|
|
|
expect(queryByText('Your Cart')).toBeNull()
|
|
fireEvent.press(getByText(/1 Item/))
|
|
|
|
expect(setIsExpanded).toHaveBeenCalledWith(true)
|
|
})
|
|
|
|
it('FCB-03: the flash cart renders with the pink theme', async () => {
|
|
__set('flash_cart_items', CART_FLASH)
|
|
const { UNSAFE_getAllByType } = await renderBar({ isFlashDelivery: true })
|
|
|
|
const bar = UNSAFE_getAllByType(View).find(
|
|
(node) => StyleSheet.flatten(node.props.style as any)?.backgroundColor === '#f81260'
|
|
)
|
|
expect(bar).toBeTruthy()
|
|
expect(StyleSheet.flatten(bar!.props.style as any).borderColor).toBe('#e11d48')
|
|
})
|
|
|
|
it('FCB-04: a quantity mutation refetches the cart and rebuilds the local quantities', async () => {
|
|
__set('cart_items', CART_REG)
|
|
const { getByTestId } = await renderBar({ isExpanded: true })
|
|
|
|
fireEvent.press(getByTestId('mini-quantifier-increment'))
|
|
|
|
await waitFor(() =>
|
|
expect(JSON.parse(__snapshot()['cart_items'])[0].quantity).toBe(3)
|
|
)
|
|
expect(getByTestId('mini-quantifier-value')).toHaveTextContent('3')
|
|
})
|
|
|
|
it('FCB-05: no cart data still renders the empty bar without crashing', async () => {
|
|
const { getByText, getByTestId } = await renderBar()
|
|
|
|
expect(getByText('No Items In Cart')).toBeTruthy()
|
|
expect(getByTestId('go-to-cart')).toBeTruthy()
|
|
})
|
|
|
|
it('FCB-06: the expanded sheet points at the checkout coupon step', async () => {
|
|
__set('cart_items', CART_REG)
|
|
const { getByTestId, getByText } = await renderBar({ isExpanded: true })
|
|
|
|
expect(getByTestId('apply-coupons-hint')).toBeTruthy()
|
|
expect(getByText('Apply Coupons at checkout')).toBeTruthy()
|
|
})
|
|
|
|
it('FCB-07: a couponed line shows its price after the coupon, with the label', async () => {
|
|
__set('cart_items', CART_REG)
|
|
const { getByTestId, getByText } = await renderBar({ isExpanded: true })
|
|
|
|
useCentralProductStore.getState().setCouponOffers({ 2: [CHICKEN_OFFER] })
|
|
|
|
// Chicken qty 2 at ₹215 = ₹430; 10% capped ₹40 = ₹390.
|
|
await waitFor(() => expect(getByTestId('cart-item-coupon-price-1')).toBeTruthy())
|
|
expect(getByTestId('cart-item-coupon-price-1')).toHaveTextContent('₹390')
|
|
expect(getByText('With Coupon')).toBeTruthy()
|
|
})
|
|
|
|
it('FCB-08: a line with no coupon shows only its price', async () => {
|
|
__set('cart_items', CART_REG)
|
|
const { queryByTestId, queryByText } = await renderBar({ isExpanded: true })
|
|
|
|
expect(queryByTestId('cart-item-coupon-price-1')).toBeNull()
|
|
expect(queryByText('With Coupon')).toBeNull()
|
|
})
|
|
})
|