/** * Product groupings list (app/(drawer)/dashboard/product-groupings/index.tsx) — * group rows, empty/loading/error states, create/edit navigation, delete via * the confirm alert and the manual-refresh hooks. */ jest.mock('expo-router', () => require('../mocks/router').expoRouterMock()) import React from 'react' import { Alert } from 'react-native' import dayjs from 'dayjs' import MaterialIcons from '@expo/vector-icons/MaterialIcons' import { act, fireEvent, screen, waitFor } from '@testing-library/react-native' import ProductGroupings from '@/app/(drawer)/dashboard/product-groupings/index' import { resetStorage, setupApiMocks } from '../mocks/api' import { expectProcedureCalled, lastProcedureInput, procCount, } from '../mocks/procedures' import { resetStaffAuthMock } from '../mocks/staff-auth' import { resetRouterMock, routerMock, searchParamsMock } from '../mocks/router' import { refreshHooks, resetRefreshHooks } from '../mocks/refresh' import { renderWithProviders } from '../helpers/render' const group = (overrides: Record = {}) => ({ id: 1, groupName: 'Meat Box', description: 'Protein bundle', createdAt: '2024-05-01T00:00:00.000Z', products: [{ id: 10 }, { id: 11 }], productCount: 2, ...overrides, }) const groupsResponse = (groups: unknown[]) => ({ groups }) const iconByName = (name: string) => screen.UNSAFE_getAllByType(MaterialIcons).find((i) => i.props.name === name) as | Parameters[0] | undefined const pressLastAlertButton = (label: string) => { const calls = (Alert.alert as jest.Mock).mock.calls const call = calls[calls.length - 1] const buttons = (call?.[2] ?? []) as { text: string; onPress?: () => void }[] act(() => buttons.find((b) => b.text === label)?.onPress?.()) } describe('Product groupings list', () => { beforeEach(async () => { resetStaffAuthMock() await resetStorage() resetRouterMock() resetRefreshHooks() searchParamsMock.current = {} setupApiMocks({ procedures: { 'admin.product.getGroups': groupsResponse([group()]) } }) jest.spyOn(Alert, 'alert').mockImplementation(() => {}) }) it('shows the loading state', () => { setupApiMocks({ procedures: { 'admin.product.getGroups': () => new Promise(() => {}) }, }) renderWithProviders() expect(screen.getByText('Loading product groups...')).toBeVisible() }) it('shows the error state when the query fails', async () => { const { fetchMock } = setupApiMocks() fetchMock.mockRejectedValue(new Error('network down')) renderWithProviders() expect(await screen.findByText('Error loading groups')).toBeVisible() }) it('shows the empty state', async () => { setupApiMocks({ procedures: { 'admin.product.getGroups': groupsResponse([]) }, }) renderWithProviders() expect(await screen.findByText('No Groups Yet')).toBeVisible() expect( screen.getByText('Start by creating your first product group using the button below.') ).toBeVisible() }) it('renders a group row with description, product count and date', async () => { setupApiMocks({ procedures: { 'admin.product.getGroups': groupsResponse([ group(), group({ id: 2, groupName: 'Fish Box', description: null, productCount: 1 }), ]), }, }) renderWithProviders() expect(await screen.findByText('Meat Box')).toBeVisible() expect(screen.getByText('Protein bundle')).toBeVisible() expect(screen.getByText('2 Products')).toBeVisible() expect(screen.getByText('Fish Box')).toBeVisible() expect(screen.getByText('1 Products')).toBeVisible() expect( screen.getAllByText(dayjs('2024-05-01T00:00:00.000Z').format('MMM DD, YYYY')) ).toHaveLength(2) }) it('navigates to create from the floating action button', async () => { renderWithProviders() await screen.findByText('Meat Box') fireEvent.press(iconByName('add')!) expect(routerMock.push).toHaveBeenCalledWith( '/(drawer)/dashboard/product-groupings/create' ) }) it('navigates to edit for the pressed group', async () => { renderWithProviders() await screen.findByText('Meat Box') fireEvent.press(iconByName('edit')!) expect(routerMock.push).toHaveBeenCalledWith( '/(drawer)/dashboard/product-groupings/edit/1' ) }) it('deletes a group after confirming the alert and refetches', async () => { const { fetchMock } = setupApiMocks({ procedures: { 'admin.product.getGroups': groupsResponse([group()]), 'admin.product.deleteGroup': { message: 'Group deleted successfully' }, }, }) renderWithProviders() await screen.findByText('Meat Box') fireEvent.press(iconByName('delete')!) expect(Alert.alert).toHaveBeenCalledWith( 'Delete Group', 'Are you sure you want to delete this group?', expect.any(Array) ) pressLastAlertButton('Delete') await waitFor(() => expectProcedureCalled(fetchMock, 'admin.product.deleteGroup') ) expect(lastProcedureInput(fetchMock, 'admin.product.deleteGroup')).toEqual({ id: 1, }) await waitFor(() => expect(procCount(fetchMock, 'admin.product.getGroups')).toBeGreaterThan(1) ) }) it('does not delete when the alert is cancelled', async () => { const { fetchMock } = setupApiMocks({ procedures: { 'admin.product.getGroups': groupsResponse([group()]), 'admin.product.deleteGroup': { message: 'Group deleted successfully' }, }, }) renderWithProviders() await screen.findByText('Meat Box') fireEvent.press(iconByName('delete')!) pressLastAlertButton('Cancel') expect(procCount(fetchMock, 'admin.product.deleteGroup')).toBe(0) }) it('refetches through the manual-refresh and mark-data-fetchers hooks', async () => { const { fetchMock } = setupApiMocks({ procedures: { 'admin.product.getGroups': groupsResponse([group()]) }, }) renderWithProviders() await screen.findByText('Meat Box') const before = procCount(fetchMock, 'admin.product.getGroups') expect(refreshHooks.manualRefresh).toBeTruthy() refreshHooks.manualRefresh?.() await waitFor(() => expect(procCount(fetchMock, 'admin.product.getGroups')).toBeGreaterThan(before) ) const afterManual = procCount(fetchMock, 'admin.product.getGroups') expect(refreshHooks.markDataFetchers).toBeTruthy() refreshHooks.markDataFetchers?.() await waitFor(() => expect(procCount(fetchMock, 'admin.product.getGroups')).toBeGreaterThan(afterManual) ) }) })