/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 4.1 — Product list (`app/(drawer)/dashboard/products/index.tsx`). */ import React from 'react' import { DeviceEventEmitter, RefreshControl } from 'react-native' import { act, fireEvent } from '@testing-library/react-native' import Products from '@/app/(drawer)/dashboard/products/index' import { renderWithProviders, flush } from '../helpers/test-utils' import { resetAllMocks } from '../helpers/reset' import { setProcedure } from '../mocks/trpc-client' import { router } from '../mocks/expo-router-mock' import { P_CHICKEN, P_CUCUMBER } from '../fixtures' import { REFRESH_EVENT } from 'common-ui/src/lib/const-strs' jest.mock('@react-navigation/native', () => { const React = require('react') const actual = jest.requireActual('../mocks/react-navigation-mock') const callbacks: Array<() => any> = [] const useFocusEffect = (callback: any) => { React.useEffect(() => { callbacks.push(callback) const cleanup = callback() return () => { const index = callbacks.indexOf(callback) if (index >= 0) callbacks.splice(index, 1) if (typeof cleanup === 'function') cleanup() } }, [callback]) } return { ...actual, useFocusEffect, __focusCallbacks: callbacks, __runFocus: () => callbacks.forEach((callback) => callback()), } }) // eslint-disable-next-line import/first const navMock = require('@react-navigation/native') as { __runFocus: () => void __focusCallbacks: Array<() => any> } function renderProducts() { return renderWithProviders(, { auth: 'in' }) } const search = (view: ReturnType) => view.getByPlaceholderText('Search products...') describe('Product list (APLIST-01..26)', () => { beforeEach(() => resetAllMocks()) it('APLIST-01: shows the loading copy', () => { setProcedure('admin.product.getProducts', { mode: 'pending' }) const view = renderProducts() expect(view.getByText('Loading products...')).toBeTruthy() }) it('APLIST-02: shows the error copy and a Retry action', () => { setProcedure('admin.product.getProducts', { mode: 'error', error: new Error('boom') }) const view = renderProducts() expect(view.getByText('Error loading products')).toBeTruthy() expect(view.getByText('Retry')).toBeTruthy() // Pressing Retry invokes the query's refetch without crashing. fireEvent.press(view.getByText('Retry')) expect(view.getByText('Error loading products')).toBeTruthy() }) it('APLIST-03: renders the empty state', () => { setProcedure('admin.product.getProducts', { data: { products: [], count: 0 } }) const view = renderProducts() expect(view.getByText('No products found')).toBeTruthy() expect(view.getByText('Start by adding your first product')).toBeTruthy() }) it('APLIST-04: renders a populated row with name, description, variant count and image', () => { const view = renderProducts() expect(view.getByText('Chicken Breast')).toBeTruthy() expect(view.getByText('Tender boneless breast')).toBeTruthy() expect(view.getByText('2 Variants')).toBeTruthy() // expo-image is shimmed with the uri as its accessibility label. expect(view.getAllByLabelText('chicken.jpg').length).toBeGreaterThan(0) }) it('APLIST-05: renders a placeholder icon when a product has no image', () => { const view = renderProducts() expect(view.getByTestId('product-image-placeholder-7')).toBeTruthy() expect(view.queryByTestId('product-image-placeholder-2')).toBeNull() }) it('APLIST-06: shows the GLOBAL badge for global products', () => { const view = renderProducts() expect(view.getByText('GLOBAL')).toBeTruthy() }) it('APLIST-07: shows green "In" when all SKUs are in stock', () => { setProcedure('admin.product.getProducts', { data: { products: [P_CHICKEN], count: 1 } }) const view = renderProducts() expect(view.getByText('In')).toBeTruthy() expect(view.queryByText('Partial')).toBeNull() expect(view.queryByText('Out')).toBeNull() expect(view.getByText('2 Variants')).toBeTruthy() }) it('APLIST-08: shows red "Out" and the out-of-stock variant count', () => { const view = renderProducts() expect(view.getByText('Out')).toBeTruthy() expect(view.getByText('1 Variants · 1 out')).toBeTruthy() }) it('APLIST-09: shows amber "Partial" for mixed stock', () => { const view = renderProducts() expect(view.getByText('Partial')).toBeTruthy() expect(view.getByText('2 Variants · 1 out')).toBeTruthy() }) it('APLIST-10: treats a product with zero SKUs as in stock', () => { setProcedure('admin.product.getProducts', { data: { products: [{ ...P_CUCUMBER, id: 20, name: 'Empty SKUs', skus: [] }], count: 1, }, }) const view = renderProducts() expect(view.getByText('0 Variants')).toBeTruthy() expect(view.getByText('In')).toBeTruthy() }) it('APLIST-11: filters by product name', () => { const view = renderProducts() fireEvent.changeText(search(view), 'Chicken') expect(view.getByText('Chicken Breast')).toBeTruthy() expect(view.queryByText('Cucumber')).toBeNull() }) it('APLIST-12: filters by short description', () => { const view = renderProducts() fireEvent.changeText(search(view), 'boneless') expect(view.getByText('Chicken Breast')).toBeTruthy() expect(view.queryByText('Cucumber')).toBeNull() }) it('APLIST-13: searches case-insensitively', () => { const view = renderProducts() fireEvent.changeText(search(view), 'cHicKeN') expect(view.getByText('Chicken Breast')).toBeTruthy() expect(view.queryByText('Mutton')).toBeNull() }) it('APLIST-14: shows the no-match copy and Clear Filters resets everything', () => { const view = renderProducts() fireEvent.changeText(search(view), 'zzzz') expect(view.getByText('No products match your filters')).toBeTruthy() fireEvent.press(view.getByText('Clear Filters')) expect(view.getByText('Chicken Breast')).toBeTruthy() expect(search(view).props.value).toBe('') }) it('APLIST-15: the In Stock filter keeps only all-in-stock products', () => { const view = renderProducts() fireEvent.press(view.getByText('In Stock (4)')) expect(view.getByText('Cucumber')).toBeTruthy() expect(view.queryByText('Mutton')).toBeNull() expect(view.queryByText('Mixed Stock')).toBeNull() }) it('APLIST-16: the Out of Stock filter keeps partial and fully-out products', () => { const view = renderProducts() fireEvent.press(view.getByText('Out of Stock (2)')) expect(view.getByText('Mutton')).toBeTruthy() expect(view.getByText('Mixed Stock')).toBeTruthy() expect(view.queryByText('Chicken Breast')).toBeNull() }) it('APLIST-17: the Global filter keeps only global products', () => { const view = renderProducts() fireEvent.press(view.getByText('Global (1)')) expect(view.getByText('Global Paneer')).toBeTruthy() expect(view.queryByText('Chicken Breast')).toBeNull() expect(view.queryByText('Cucumber')).toBeNull() }) it('APLIST-18: filter counts match the products', () => { const view = renderProducts() expect(view.getByText('All (6)')).toBeTruthy() expect(view.getByText('In Stock (4)')).toBeTruthy() expect(view.getByText('Out of Stock (2)')).toBeTruthy() expect(view.getByText('Global (1)')).toBeTruthy() }) it('APLIST-19: Clear Filters clears the search and resets the filter to All', () => { const view = renderProducts() fireEvent.press(view.getByText('Out of Stock (2)')) fireEvent.changeText(search(view), 'zzz') fireEvent.press(view.getByText('Clear Filters')) expect(search(view).props.value).toBe('') expect(view.getByText('Chicken Breast')).toBeTruthy() expect(view.getByText('All (6)')).toBeTruthy() }) it('APLIST-20: View opens the product detail route', () => { const view = renderProducts() fireEvent.press(view.getAllByText('View')[0]) expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/products/detail/2') }) it('APLIST-21: Edit opens the edit route with the product id', () => { const view = renderProducts() fireEvent.press(view.getAllByText('Edit')[0]) expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/products/edit?id=2') }) it('APLIST-22: Stock opens the variant stock dialog for that product', () => { const view = renderProducts() fireEvent.press(view.getAllByText('Stock')[0]) expect(view.getByTestId('variant-stock-11')).toBeTruthy() expect(view.getByTestId('variant-stock-12')).toBeTruthy() }) it('APLIST-23: Add Product opens the create route', () => { const view = renderProducts() fireEvent.press(view.getByTestId('add-product-button')) expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/products/add') }) it('APLIST-24: pull-to-refresh is wired to the refresh path', async () => { const addListener = jest.spyOn(DeviceEventEmitter, 'addListener') const view = renderProducts() const refreshListener = addListener.mock.calls.find(([event]) => event === REFRESH_EVENT)?.[1] expect(typeof refreshListener).toBe('function') const refreshControl = view.UNSAFE_getByType(RefreshControl) await act(async () => { await refreshControl.props.onRefresh?.() }) expect(view.getByText('Chicken Breast')).toBeTruthy() addListener.mockRestore() }) it('APLIST-25: a focus refetches through the marked data fetcher', async () => { const view = renderProducts() // The screen registers a focus callback; the first focus skips the refetch // (mount already fetched), the next one invokes it. Refetch itself is an // inline jest.fn inside the harness query mock, so the assertion stops at // the focus wiring + a stable render after the callback runs. expect(navMock.__focusCallbacks.length).toBeGreaterThan(0) await act(async () => { navMock.__runFocus() }) expect(view.getByText('Chicken Breast')).toBeTruthy() }) it('APLIST-26: renders a 100+ product list without crashing', () => { const many = Array.from({ length: 120 }, (_, index) => ({ ...P_CHICKEN, id: 1000 + index, name: `Product ${index + 1}`, })) setProcedure('admin.product.getProducts', { data: { products: many, count: many.length } }) const view = renderProducts() // Virtualised: only the first window is mounted, but the list is stable. expect(view.getByText('Product 1')).toBeTruthy() expect(view.getAllByText(/^Product \d+$/).length).toBeGreaterThan(0) }) })