/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 11.1 — Product groupings list (`tests/pages/product-groupings.test.tsx`). */ import React from 'react' import { Alert, DeviceEventEmitter } from 'react-native' import { act, fireEvent } from '@testing-library/react-native' import { MyTouchableOpacity } from 'common-ui' import { MaterialIcons } from '@expo/vector-icons' import ProductGroupings from '@/app/(drawer)/dashboard/product-groupings/index' import { renderWithProviders, flush } from '../helpers/test-utils' import { resetAllMocks } from '../helpers/reset' import { setProcedure, lastMutationInput, procedureMutationCalls, } from '../mocks/trpc-client' import { router } from '../mocks/expo-router-mock' import { GROUP_1 } from '../fixtures' type View = ReturnType['view'] function renderGroups(groups: any[] = [GROUP_1]) { setProcedure('admin.product.getGroups', { data: { groups } }) const view = renderWithProviders(, { auth: false }) return { view } } function icon(view: View, name: string) { return view.UNSAFE_getAllByType(MaterialIcons).find((node: any) => node.props.name === name) } async function confirmDelete(alertSpy: jest.SpyInstance) { const buttons = alertSpy.mock.calls[0][2] await act(async () => { await buttons[1].onPress?.() }) await flush(2) } describe('Product groupings list (AGRP-01..11)', () => { let alertSpy: jest.SpyInstance beforeEach(() => { resetAllMocks() alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) }) afterEach(() => alertSpy?.mockRestore()) it('AGRP-01: shows the loading copy', () => { setProcedure('admin.product.getGroups', { mode: 'pending' }) const view = renderWithProviders(, { auth: false }) expect(view.getByText('Loading product groups...')).toBeTruthy() }) it('AGRP-02: shows the error copy', () => { setProcedure('admin.product.getGroups', { mode: 'error', error: new Error('no') }) const view = renderWithProviders(, { auth: false }) expect(view.getByText('Error loading groups')).toBeTruthy() }) it('AGRP-03: shows the empty state with the create action', () => { const { view } = renderGroups([]) expect(view.getByText('No Groups Yet')).toBeTruthy() expect( view.getByText('Start by creating your first product group using the button below.') ).toBeTruthy() expect(view.UNSAFE_getByType(MyTouchableOpacity)).toBeTruthy() }) it('AGRP-04: renders the group name, description and product count', () => { const { view } = renderGroups([GROUP_1]) expect(view.getByText('Breakfast')).toBeTruthy() expect(view.getByText('Morning items')).toBeTruthy() expect(view.getByText('2 Products')).toBeTruthy() expect(view.getByText('Group Name')).toBeTruthy() }) it('AGRP-05: View Products currently only sets local state', () => { const { view } = renderGroups([GROUP_1]) // The page imports BottomDialog but never renders it, so pressing the count // is inert beyond setViewProducts. fireEvent.press(view.getByText('2 Products')) expect(view.getByText('Breakfast')).toBeTruthy() expect(view.queryByText('Products in group')).toBeNull() }) it('AGRP-06: Edit opens the edit route', () => { const { view } = renderGroups([GROUP_1]) fireEvent.press(icon(view, 'edit')) expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/product-groupings/edit/1') }) it('AGRP-07: Delete asks for confirmation and Cancel makes no call', async () => { const { view } = renderGroups([GROUP_1]) fireEvent.press(icon(view, 'delete')) expect(alertSpy).toHaveBeenCalledWith( 'Delete Group', 'Are you sure you want to delete this group?', expect.any(Array) ) const buttons = alertSpy.mock.calls[0][2] await act(async () => { buttons[0].onPress?.() }) expect(procedureMutationCalls('admin.product.deleteGroup')).toHaveLength(0) }) it('AGRP-08: confirming Delete calls deleteGroup', async () => { const { view } = renderGroups([GROUP_1]) fireEvent.press(icon(view, 'delete')) await confirmDelete(alertSpy) expect(lastMutationInput('admin.product.deleteGroup')).toEqual({ id: 1 }) }) it('AGRP-09: a failed delete currently does not alert', async () => { setProcedure('admin.product.deleteGroup', { mode: 'error', error: new Error('no') }) const { view } = renderGroups([GROUP_1]) fireEvent.press(icon(view, 'delete')) await confirmDelete(alertSpy) // The mutation has no onError handler, so no alert is raised. expect(alertSpy.mock.calls.every((call) => call[0] !== 'Error')).toBe(true) }) it('AGRP-10: the create FAB opens the create route', () => { const { view } = renderGroups([]) fireEvent.press(view.UNSAFE_getByType(MyTouchableOpacity)) expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/product-groupings/create') }) it('AGRP-11: a refresh event keeps the list rendered', () => { const { view } = renderGroups([GROUP_1]) DeviceEventEmitter.emit('refresh_event') expect(view.getByText('Breakfast')).toBeTruthy() expect(view.getByText('2 Products')).toBeTruthy() }) })