150 lines
4.6 KiB
TypeScript
150 lines
4.6 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Shared ProductGroupForm (`tests/components/product-group-form.test.tsx`).
|
|
*
|
|
* Section 11 of the catalogue covers the list/create/edit screens; this file
|
|
* exercises the shared form they all mount.
|
|
*/
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { act, fireEvent } from '@testing-library/react-native'
|
|
import { BottomDropdown } from 'common-ui'
|
|
|
|
import ProductGroupForm, { ProductGroup } from '@/components/ProductGroupForm'
|
|
import { renderWithProviders, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import {
|
|
setProcedure,
|
|
lastMutationInput,
|
|
procedureMutationCalls,
|
|
procedureInvalidations,
|
|
} from '../mocks/trpc-client'
|
|
import { GROUP_1 } from '../fixtures'
|
|
|
|
type View = ReturnType<typeof renderForm>['view']
|
|
|
|
function renderForm(group?: ProductGroup | null) {
|
|
const onClose = jest.fn()
|
|
const onSuccess = jest.fn()
|
|
const view = renderWithProviders(
|
|
<ProductGroupForm group={group} onClose={onClose} onSuccess={onSuccess} />,
|
|
{ auth: false }
|
|
)
|
|
return { view, onClose, onSuccess }
|
|
}
|
|
|
|
function nameInput(view: View) {
|
|
return view.getByPlaceholderText('Enter group name')
|
|
}
|
|
|
|
async function submit(view: View) {
|
|
fireEvent.press(view.getByText('Save'))
|
|
await flush(3)
|
|
}
|
|
|
|
describe('ProductGroupForm (AGRPF-01..08)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
})
|
|
|
|
afterEach(() => alertSpy?.mockRestore())
|
|
|
|
it('AGRPF-01: renders the create form', () => {
|
|
const { view } = renderForm()
|
|
|
|
expect(view.getByText('Create Product Group')).toBeTruthy()
|
|
expect(nameInput(view).props.value).toBe('')
|
|
expect(view.getByPlaceholderText('Enter description (optional)')).toBeTruthy()
|
|
expect(view.getByText('Select products')).toBeTruthy()
|
|
expect(view.getByText('Cancel')).toBeTruthy()
|
|
})
|
|
|
|
it('AGRPF-02: an empty group name blocks the create', async () => {
|
|
const { view } = renderForm()
|
|
|
|
await submit(view)
|
|
|
|
expect(view.getByText('Group name is required')).toBeTruthy()
|
|
expect(procedureMutationCalls('admin.product.createGroup')).toHaveLength(0)
|
|
})
|
|
|
|
it('AGRPF-03: creates a group and invalidates the list', async () => {
|
|
const { view, onSuccess } = renderForm()
|
|
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(procedureInvalidations('admin.product.getGroups').length).toBeGreaterThan(0)
|
|
expect(onSuccess).toHaveBeenCalled()
|
|
})
|
|
|
|
it('AGRPF-04: renders the edit heading and prefilled values', async () => {
|
|
const { view } = renderForm(GROUP_1 as ProductGroup)
|
|
await flush()
|
|
|
|
expect(view.getByText('Edit Product Group')).toBeTruthy()
|
|
expect(nameInput(view).props.value).toBe('Breakfast')
|
|
expect(view.getByPlaceholderText('Enter description (optional)').props.value).toBe(
|
|
'Morning items'
|
|
)
|
|
expect(view.getByText('Chicken Breast - 500g, Cucumber - 500g')).toBeTruthy()
|
|
})
|
|
|
|
it('AGRPF-05: product selection writes product_ids', async () => {
|
|
const { view } = renderForm()
|
|
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('AGRPF-06: editing updates the group by id', async () => {
|
|
const { view, onSuccess } = renderForm(GROUP_1 as ProductGroup)
|
|
fireEvent.changeText(nameInput(view), 'Breakfast Plus')
|
|
|
|
await submit(view)
|
|
|
|
expect(lastMutationInput('admin.product.updateGroup')).toEqual({
|
|
id: 1,
|
|
group_name: 'Breakfast Plus',
|
|
description: 'Morning items',
|
|
product_ids: [11, 13],
|
|
})
|
|
expect(onSuccess).toHaveBeenCalled()
|
|
})
|
|
|
|
it('AGRPF-07: a failed save shows the server message', async () => {
|
|
setProcedure('admin.product.createGroup', {
|
|
mode: 'error',
|
|
error: new Error('Duplicate group'),
|
|
})
|
|
const { view } = renderForm()
|
|
fireEvent.changeText(nameInput(view), 'Breakfast')
|
|
|
|
await submit(view)
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith('Error', 'Duplicate group')
|
|
})
|
|
|
|
it('AGRPF-08: Cancel calls onClose', () => {
|
|
const { view, onClose } = renderForm()
|
|
|
|
fireEvent.press(view.getByText('Cancel'))
|
|
|
|
expect(onClose).toHaveBeenCalled()
|
|
})
|
|
})
|