/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 11.2 — Product group create (`tests/pages/product-grouping-create.test.tsx`). */ import React from 'react' import { Alert } from 'react-native' import { act, fireEvent, waitFor } from '@testing-library/react-native' import { BottomDropdown } from 'common-ui' import CreateProductGroup from '@/app/(drawer)/dashboard/product-groupings/create' import { renderAndFlush, 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' type View = Awaited>['view'] async function renderCreate() { const view = await renderAndFlush(, { auth: false }) return { view } } function nameInput(view: View) { return view.getByPlaceholderText('Enter group name') } async function submit(view: View) { fireEvent.press(view.getByText('Save')) await flush(3) } describe('Product group create (AGRPC-01..07)', () => { let alertSpy: jest.SpyInstance beforeEach(() => { resetAllMocks() alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) }) afterEach(() => alertSpy?.mockRestore()) it('AGRPC-01: renders Create Product Group', async () => { const { view } = await renderCreate() expect(view.getByText('Create Product Group')).toBeTruthy() expect(nameInput(view)).toBeTruthy() }) it('AGRPC-02: an empty name is a validation error', async () => { const { view } = await renderCreate() await submit(view) expect(view.getByText('Group name is required')).toBeTruthy() expect(procedureMutationCalls('admin.product.createGroup')).toHaveLength(0) }) it('AGRPC-03: creates the group and goes back', async () => { const { view } = await renderCreate() fireEvent.changeText(nameInput(view), 'Breakfast') fireEvent.changeText(view.getByPlaceholderText('Enter description (optional)'), 'Morning') await submit(view) expect(lastMutationInput('admin.product.createGroup')).toEqual({ group_name: 'Breakfast', description: 'Morning', product_ids: [], }) expect(router.back).toHaveBeenCalled() }) it('AGRPC-04: an empty description is allowed', async () => { const { view } = await renderCreate() fireEvent.changeText(nameInput(view), 'Only Name') await submit(view) expect(lastMutationInput('admin.product.createGroup')).toMatchObject({ group_name: 'Only Name', description: '', }) }) it('AGRPC-05: product selection is sent as product_ids', async () => { const { view } = await renderCreate() fireEvent.changeText(nameInput(view), 'Combo') const dropdown = view.UNSAFE_getAllByType(BottomDropdown)[0] act(() => { dropdown.props.onValueChange(['11', '13']) }) await submit(view) expect(lastMutationInput('admin.product.createGroup').product_ids).toEqual([11, 13]) }) it('AGRPC-06: a failed create shows an error alert', async () => { setProcedure('admin.product.createGroup', { mode: 'error', error: new Error('Bad group'), }) const { view } = await renderCreate() fireEvent.changeText(nameInput(view), 'Breakfast') await submit(view) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Error', 'Bad group')) }) it('AGRPC-07: a pending create shows Saving...', async () => { setProcedure('admin.product.createGroup', { mode: 'pending' }) const { view } = await renderCreate() fireEvent.changeText(nameInput(view), 'Breakfast') await submit(view) expect(lastMutationInput('admin.product.createGroup')).toBeTruthy() expect(view.getByText('Saving...')).toBeTruthy() }) })