/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 10 — Store detail (`tests/pages/store-detail.test.tsx`, * STORED-01..24). * * The screen loads `stores/.json` through `useStoreWithProducts`, merges the * store's product ids with the cached catalogue, renders a tag chip strip and a * two-column product grid, and keeps `storeHeaderStore` / the drawer title in * sync with the store name. * * STORED-22 needs a *second* focus for `useMarkDataFetchers`, which reads * `@react-navigation/native`; that module is narrowed to a registry that still * runs on mount. */ import * as React from 'react' import { DeviceEventEmitter } from 'react-native' import { act, fireEvent, waitFor, within } from '@testing-library/react-native' import { REFRESH_EVENT } from 'common-ui/src/lib/const-strs' import StoreDetail from '@/app/(drawer)/(tabs)/stores/store-detail/[id]' import { useStoreHeaderStore } from '@/src/store/storeHeaderStore' import { useCartStore } from '@/src/store/cartStore' import { flush, renderWithProviders, seedBoot } from '../helpers/test-utils' import { resetAllMocks } from '../helpers/reset' import { router, __setParams } from '../mocks/expo-router-mock' import { jsonCallsFor, setJsonError, setJsonPending, setJsonResponse } from '../mocks/fetch-json' import { P_CHICKEN, P_CUCUMBER, P_OUT, STORE_DETAIL_1, productsCacheUrl, storeDetailCacheUrl, } from '../fixtures' /** Focus listeners registered through the narrowed `@react-navigation/native`. */ const mockFocusCallbacks = new Set<() => void>() jest.mock('@react-navigation/native', () => { const React = require('react') const actual = jest.requireActual(__dirname + '/../mocks/react-navigation-mock') return { ...actual, useFocusEffect: (callback: () => void | (() => void)) => { React.useEffect(() => { mockFocusCallbacks.add(callback) const cleanup = callback() return () => { mockFocusCallbacks.delete(callback) if (typeof cleanup === 'function') cleanup() } // eslint-disable-next-line react-hooks/exhaustive-deps }, [callback]) }, } }) async function emitFocus() { await act(async () => { Array.from(mockFocusCallbacks).forEach((callback) => callback()) }) } async function load() { await flush(3) } function renderPage() { return renderWithProviders(, { auth: false, withProductStore: true }) } function seedDetail(detail: any) { setJsonResponse(storeDetailCacheUrl(1), detail) } describe('Store detail (STORED)', () => { beforeEach(() => { resetAllMocks() mockFocusCallbacks.clear() seedBoot() __setParams({ id: '1' }) }) it('STORED-01: a pending store request shows the store loading copy', async () => { setJsonPending(storeDetailCacheUrl(1)) const { getByText, getByTestId } = renderPage() await waitFor(() => expect(getByTestId('store-detail-loading')).toBeTruthy()) expect(getByText('Loading store...')).toBeTruthy() }) it('STORED-02: a loaded store with pending products shows the products loading copy', async () => { setJsonPending(productsCacheUrl()) const { getByText } = renderPage() await waitFor(() => expect(getByText('Loading products...')).toBeTruthy()) }) it('STORED-03: a failed store request shows the error state', async () => { setJsonError(storeDetailCacheUrl(1), new Error('offline')) const { getByText, getByTestId } = renderPage() await waitFor(() => expect(jsonCallsFor(storeDetailCacheUrl(1)).length).toBeGreaterThan(0) ) await load() expect(getByTestId('store-detail-error')).toBeTruthy() expect(getByText('Store not found or error loading')).toBeTruthy() }) it('STORED-04: an invalid store id shows the error state without crashing', async () => { __setParams({ id: 'not-a-number' }) const { getByText, getByTestId } = renderPage() await waitFor(() => expect(jsonCallsFor(storeDetailCacheUrl(NaN)).length).toBeGreaterThan(0) ) await load() expect(getByTestId('store-detail-error')).toBeTruthy() expect(getByText('Store not found or error loading')).toBeTruthy() }) it('STORED-05: a loaded store renders its name, description, tags and products', async () => { const { getByTestId, getAllByTestId, getByText } = renderPage() await load() expect(getByTestId('store-detail-name').props.children).toBe('The Freshyo Meat House') expect(getByTestId('store-detail-description').props.children).toBe( 'Fresh meat and produce' ) expect(getAllByTestId('store-tag-chip')).toHaveLength(2) expect(getAllByTestId('product-card')).toHaveLength(2) expect(getByTestId('store-detail-heading').props.children).toBe('2 products') expect(getByText('Chicken Breast')).toBeTruthy() expect(getByText('Cucumber')).toBeTruthy() }) it('STORED-06: a store without a description renders no description block', async () => { seedDetail({ ...STORE_DETAIL_1, store: { ...STORE_DETAIL_1.store, description: null }, }) const { queryByTestId, getByTestId } = renderPage() await load() expect(getByTestId('store-detail-name')).toBeTruthy() expect(queryByTestId('store-detail-description')).toBeNull() }) it('STORED-07: every tag renders as a chip with its product count', async () => { const { getAllByTestId } = renderPage() await load() const chips = getAllByTestId('store-tag-chip') expect(within(chips[0]).getByText('Chicken (1)')).toBeTruthy() expect(within(chips[1]).getByText('Veg (1)')).toBeTruthy() }) it('STORED-08: tapping a tag filters the grid to that tag products', async () => { const { getAllByTestId, getByTestId, queryByText } = renderPage() await load() fireEvent.press(getAllByTestId('store-tag-chip')[0]) expect(getByTestId('store-detail-heading').props.children).toBe('Chicken items') expect(getAllByTestId('product-card')).toHaveLength(1) expect(queryByText('Cucumber')).toBeNull() }) it('STORED-09: tapping the selected tag again clears the filter', async () => { const { getAllByTestId, getByTestId, queryByTestId } = renderPage() await load() const chip = getAllByTestId('store-tag-chip')[0] fireEvent.press(chip) expect(getByTestId('store-detail-heading').props.children).toBe('Chicken items') fireEvent.press(chip) expect(getByTestId('store-detail-heading').props.children).toBe('2 products') expect(queryByTestId('store-detail-clear')).toBeNull() }) it('STORED-10: tapping Clear removes the tag filter', async () => { const { getAllByTestId, getByTestId, queryByTestId } = renderPage() await load() fireEvent.press(getAllByTestId('store-tag-chip')[0]) expect(getByTestId('store-detail-clear')).toBeTruthy() fireEvent.press(getByTestId('store-detail-clear')) expect(getByTestId('store-detail-heading').props.children).toBe('2 products') expect(queryByTestId('store-detail-clear')).toBeNull() }) it('STORED-11: a tag without products empties the grid and keeps the tag heading', async () => { seedDetail({ ...STORE_DETAIL_1, tags: [{ id: 9, tagName: 'Empty', tagDescription: null, productIds: [] }], }) const { getByTestId, queryAllByTestId } = renderPage() await load() fireEvent.press(getByTestId('store-tag-chip')) expect(getByTestId('store-detail-heading').props.children).toBe('Empty items') expect(queryAllByTestId('product-card')).toHaveLength(0) }) it('STORED-12: a store product missing from the catalogue is skipped', async () => { seedDetail({ ...STORE_DETAIL_1, products: [P_CHICKEN, { ...P_CUCUMBER, id: 999 }], }) const { getAllByTestId, getByTestId } = renderPage() await load() expect(getAllByTestId('product-card')).toHaveLength(1) expect(getByTestId('store-detail-heading').props.children).toBe('1 products') }) it('STORED-13: the grid follows the store detail payload order', async () => { seedDetail({ ...STORE_DETAIL_1, products: [P_CUCUMBER, P_CHICKEN], }) const { getAllByTestId } = renderPage() await load() const cards = getAllByTestId('product-card') expect(within(cards[0]).getByText('Cucumber')).toBeTruthy() expect(within(cards[1]).getByText('Chicken Breast')).toBeTruthy() }) it('STORED-14: a duplicated product id is rendered once per payload entry', async () => { seedDetail({ ...STORE_DETAIL_1, products: [P_CHICKEN, P_CHICKEN], }) const { getAllByTestId } = renderPage() await load() expect(getAllByTestId('product-card')).toHaveLength(2) expect(getAllByTestId('product-card').map((card) => within(card).getByText('Chicken Breast'))).toHaveLength(2) }) it('STORED-15: tapping a product pushes the store product-detail route', async () => { const { getAllByTestId } = renderPage() await load() fireEvent.press(getAllByTestId('product-card')[0]) expect(router.push).toHaveBeenCalledWith( '/(drawer)/(tabs)/stores/store-detail/product-detail/2' ) }) it('STORED-16: tapping the add icon queues the add-to-cart dialog product', async () => { const { getAllByTestId } = renderPage() await load() fireEvent.press(getAllByTestId('add-to-cart-icon')[0]) expect(useCartStore.getState().addedToCartProduct?.productId).toBe(2) }) it('STORED-17: an out-of-stock store product carries the unavailable badge', async () => { seedDetail({ ...STORE_DETAIL_1, products: [P_OUT] }) const { getAllByTestId, getByText } = renderPage() await load() expect(getByText('Mutton')).toBeTruthy() expect(getAllByTestId('out-of-stock-badge')).toHaveLength(1) }) it('STORED-18: a store name containing meat shows the banner images', async () => { const { getAllByTestId, getByLabelText } = renderPage() await load() expect(getAllByTestId('store-detail-meat-banner')).toHaveLength(1) expect(getByLabelText(/halal\.jpg/)).toBeTruthy() expect(getByLabelText(/preservs\.jpg/)).toBeTruthy() }) it('STORED-19: the store name is published to the header and drawer title', async () => { const titles: any[] = [] const subscription = DeviceEventEmitter.addListener('updateDrawerTitle', (title) => titles.push(title) ) renderPage() await load() expect(useStoreHeaderStore.getState().title).toBe('The Freshyo Meat House') expect(titles).toContain('The Freshyo Meat House') subscription.remove() }) it('STORED-20: a refetched store name updates the header title', async () => { seedDetail({ ...STORE_DETAIL_1, store: { ...STORE_DETAIL_1.store, name: 'Alpha Store' }, }) const { getByTestId } = renderPage() await load() expect(useStoreHeaderStore.getState().title).toBe('Alpha Store') seedDetail({ ...STORE_DETAIL_1, store: { ...STORE_DETAIL_1.store, name: 'Beta Store' }, }) await act(async () => { DeviceEventEmitter.emit(REFRESH_EVENT) }) await flush(3) await waitFor(() => expect(useStoreHeaderStore.getState().title).toBe('Beta Store') ) expect(getByTestId('store-detail-name').props.children).toBe('Beta Store') }) it('STORED-21: a refresh event refetches the store detail document', async () => { renderPage() await load() const before = jsonCallsFor(storeDetailCacheUrl(1)).length await act(async () => { DeviceEventEmitter.emit(REFRESH_EVENT) }) await flush(3) expect(jsonCallsFor(storeDetailCacheUrl(1)).length).toBeGreaterThan(before) }) it('STORED-22: a re-focus through useMarkDataFetchers refetches store detail', async () => { renderPage() await load() const before = jsonCallsFor(storeDetailCacheUrl(1)).length await emitFocus() await flush(3) expect(jsonCallsFor(storeDetailCacheUrl(1)).length).toBeGreaterThan(before) }) it('STORED-23: the floating cart bar is visible', async () => { const { getByTestId } = renderPage() await load() expect(getByTestId('go-to-cart')).toBeTruthy() }) it('STORED-24: a store without tags renders no tag strip', async () => { seedDetail({ ...STORE_DETAIL_1, tags: [] }) const { queryByTestId, getByTestId } = renderPage() await load() expect(getByTestId('store-detail-name')).toBeTruthy() expect(queryByTestId('store-detail-tags')).toBeNull() }) })