freshyo/apps/user-ui/tests/components/add-to-cart-dialog.test.tsx
2026-10-23 15:58:40 +05:30

111 lines
4.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Section 24 — AddToCartDialog (A2C-01..06).
*
* The dialog is driven by `useCartStore`'s `addedToCartProduct`; the product is
* queued with `setAddedToCartProduct` before render. Slots come from the CDN
* `slots.json` document (via `seedBoot`), the cart lines from `cart_items`.
*/
import * as React from 'react'
import { ScrollView, TouchableOpacity } from 'react-native'
import { fireEvent, waitFor } from '@testing-library/react-native'
import AddToCartDialog from '@/src/components/AddToCartDialog'
import { useCartStore } from '@/src/store/cartStore'
import { renderWithProviders, seedBoot, flush } from '../helpers/test-utils'
import { resetAllMocks } from '../helpers/reset'
import { __set } from '../mocks/secure-store-mock'
import { CART_REG, P_CHICKEN, P_CUCUMBER, SLOTS } from '../fixtures'
const APPENDED_AT = '2024-01-01T00:00:00.000Z'
/** The dialog keys each slot row by the slot id. */
const slotTestId = (slot: { id: number }) => `slot-option-${slot.id}`
// Chicken (SKU 2) is served by every fixture slot; only the three whose
// freezeTime is still in the future survive the dialog's filter.
const CHICKEN_SLOTS = [SLOTS[0], SLOTS[1], SLOTS[3]].map(slotTestId)
// Cucumber (SKU 5) is served by slots 1849 and 1852.
const CUCUMBER_SLOTS = [SLOTS[0], SLOTS[3]].map(slotTestId)
const cartLine = (id: number, skuId: number, quantity: number, slotId: number) =>
JSON.stringify([{ id, skuId, quantity, slotId, addedAt: APPENDED_AT }])
async function renderDialog(product: any, cart?: string) {
seedBoot()
if (cart) __set('cart_items', cart)
useCartStore.getState().setAddedToCartProduct({ productId: product.id, product })
const utils = renderWithProviders(<AddToCartDialog />, { auth: false })
await flush(2)
return utils
}
describe('AddToCartDialog', () => {
beforeEach(() => resetAllMocks())
it('A2C-01: many slots — the slot list scrolls while the actions stay mounted', async () => {
const { getByTestId, getByText, UNSAFE_getAllByType } = await renderDialog(P_CHICKEN)
await waitFor(() => expect(getByTestId(CHICKEN_SLOTS[2])).toBeTruthy())
CHICKEN_SLOTS.forEach((testID) => expect(getByTestId(testID)).toBeTruthy())
// The only shrinkable region is the slot list; the actions below it are
// rendered outside that scroll view.
const list = UNSAFE_getAllByType(ScrollView).find(
(node) => (node.props.style as any)?.flexShrink === 1
)
expect(list).toBeTruthy()
expect(getByTestId('add-to-cart-confirm')).toBeTruthy()
expect(getByText('Cancel')).toBeTruthy()
})
it('A2C-02: two slots — only that product’s rows render and the sheet is content-sized', async () => {
const { getByTestId, queryByTestId } = await renderDialog(P_CUCUMBER)
await waitFor(() => expect(getByTestId(CUCUMBER_SLOTS[1])).toBeTruthy())
CUCUMBER_SLOTS.forEach((testID) => expect(getByTestId(testID)).toBeTruthy())
// Chicken's second slot (1850) is not a cucumber slot.
expect(queryByTestId(CHICKEN_SLOTS[1])).toBeNull()
expect(getByTestId('add-to-cart-confirm')).toBeTruthy()
})
it('A2C-03: quantity update when an existing line is present — remove button visible', async () => {
const { getByTestId } = await renderDialog(P_CHICKEN, CART_REG)
await waitFor(() => expect(getByTestId('remove-from-cart')).toBeTruthy())
// Cart line quantity (2) is preselected.
expect(getByTestId('quantifier-value')).toHaveTextContent('2')
})
it('A2C-04: no cart line — no remove button', async () => {
const { getByTestId, queryByTestId, getByText } = await renderDialog(P_CHICKEN)
await waitFor(() => expect(getByTestId('add-to-cart-confirm')).toBeTruthy())
expect(queryByTestId('remove-from-cart')).toBeNull()
expect(getByText('Add to Cart')).toBeTruthy()
})
it('A2C-05: a cart line for another SKU is not treated as an update', async () => {
const { getByTestId, queryByTestId, getByText } = await renderDialog(
P_CHICKEN,
cartLine(1, P_CUCUMBER.id, 3, SLOTS[0].id)
)
await waitFor(() => expect(getByTestId('add-to-cart-confirm')).toBeTruthy())
expect(queryByTestId('remove-from-cart')).toBeNull()
expect(getByText('Add to Cart')).toBeTruthy()
})
it('A2C-06: pressing the backdrop closes the dialog and clears the product', async () => {
const { UNSAFE_getAllByType } = await renderDialog(P_CHICKEN, CART_REG)
await waitFor(() => expect(useCartStore.getState().addedToCartProduct).not.toBeNull())
const backdrop = UNSAFE_getAllByType(TouchableOpacity).find(
(node) => node.props.activeOpacity === 1 && typeof node.props.onPress === 'function'
)
expect(backdrop).toBeTruthy()
fireEvent.press(backdrop!)
expect(useCartStore.getState().addedToCartProduct).toBeNull()
})
})