/* 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 { 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' /** * 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 = {}) { seedBoot() const utils = renderWithProviders(, { 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() }) })