243 lines
No EOL
8.2 KiB
TypeScript
243 lines
No EOL
8.2 KiB
TypeScript
/**
|
|
* ProductGroupForm (components/ProductGroupForm.tsx) — create/edit titles,
|
|
* name validation, createGroup/updateGroup payloads, product selection and the
|
|
* cancel/close handlers.
|
|
*
|
|
* NOTE: the embedded ProductsSelector emits SKU ids, but createGroup /
|
|
* updateGroup expect productInfo ids (db_helper_postgres product.ts:315/345).
|
|
* The payloads below are asserted as observed and reported as an app bug.
|
|
*/
|
|
|
|
import React from 'react'
|
|
import MaterialIcons from '@expo/vector-icons/MaterialIcons'
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
|
|
import ProductGroupForm, { ProductGroup } from '@/components/ProductGroupForm'
|
|
import { resetStorage, setupApiMocks } from '../mocks/api'
|
|
import {
|
|
expectProcedureCalled,
|
|
lastProcedureInput,
|
|
procCount,
|
|
} from '../mocks/procedures'
|
|
import { createQueryClient, renderWithProviders } from '../helpers/render'
|
|
|
|
const skusResponse = {
|
|
skus: [
|
|
{ id: 1, productId: 10, productName: 'Chicken', label: 'Chicken 1kg' },
|
|
{ id: 2, productId: 11, productName: 'Mutton', label: 'Mutton 1kg' },
|
|
],
|
|
}
|
|
|
|
const procedures = {
|
|
'common.product.getAllProductsSummary': { products: [] },
|
|
'common.product.getAllSkusSummary': skusResponse,
|
|
'admin.product.createGroup': {
|
|
group: { id: 1, groupName: 'Meat', description: null, createdAt: '2024-01-01' },
|
|
message: 'Group created successfully',
|
|
},
|
|
'admin.product.updateGroup': {
|
|
group: { id: 5, groupName: 'Meat', description: 'Bundle', createdAt: '2024-01-01' },
|
|
message: 'Group updated successfully',
|
|
},
|
|
}
|
|
|
|
const existingGroup: ProductGroup = {
|
|
id: 5,
|
|
groupName: 'Beef Box',
|
|
description: 'Bundle',
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
products: [{ id: 10 }, { id: 11 }],
|
|
productCount: 2,
|
|
}
|
|
|
|
describe('ProductGroupForm', () => {
|
|
beforeEach(async () => {
|
|
await resetStorage()
|
|
setupApiMocks({ procedures })
|
|
jest.spyOn(console, 'error').mockImplementation(() => {})
|
|
})
|
|
|
|
it('renders in create mode', () => {
|
|
renderWithProviders(
|
|
<ProductGroupForm onClose={jest.fn()} onSuccess={jest.fn()} />
|
|
)
|
|
|
|
expect(screen.getByText('Create Product Group')).toBeVisible()
|
|
expect(screen.getByText('Group Name *')).toBeVisible()
|
|
expect(screen.getByText('Description')).toBeVisible()
|
|
expect(screen.getByText('Save')).toBeVisible()
|
|
})
|
|
|
|
it('renders in edit mode with the group seeded', () => {
|
|
renderWithProviders(
|
|
<ProductGroupForm
|
|
group={existingGroup}
|
|
onClose={jest.fn()}
|
|
onSuccess={jest.fn()}
|
|
/>
|
|
)
|
|
|
|
expect(screen.getByText('Edit Product Group')).toBeVisible()
|
|
expect(screen.getByDisplayValue('Beef Box')).toBeVisible()
|
|
expect(screen.getByDisplayValue('Bundle')).toBeVisible()
|
|
})
|
|
|
|
it('blocks submission when the group name is empty', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
renderWithProviders(
|
|
<ProductGroupForm onClose={jest.fn()} onSuccess={jest.fn()} />
|
|
)
|
|
|
|
fireEvent.press(screen.getByText('Save'))
|
|
|
|
expect(await screen.findByText('Group name is required')).toBeVisible()
|
|
expect(procCount(fetchMock, 'admin.product.createGroup')).toBe(0)
|
|
})
|
|
|
|
it('creates a group with the entered values', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
const onSuccess = jest.fn()
|
|
renderWithProviders(
|
|
<ProductGroupForm onClose={jest.fn()} onSuccess={onSuccess} />
|
|
)
|
|
|
|
fireEvent.changeText(screen.getByPlaceholderText('Enter group name'), 'Meat')
|
|
fireEvent.changeText(
|
|
screen.getByPlaceholderText('Enter description (optional)'),
|
|
'Protein'
|
|
)
|
|
fireEvent.press(screen.getByText('Save'))
|
|
|
|
await waitFor(() => expectProcedureCalled(fetchMock, 'admin.product.createGroup'))
|
|
expect(lastProcedureInput(fetchMock, 'admin.product.createGroup')).toEqual({
|
|
group_name: 'Meat',
|
|
description: 'Protein',
|
|
product_ids: [],
|
|
})
|
|
await waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
|
|
})
|
|
|
|
it('creates a group carrying the selected SKU ids as product_ids', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
renderWithProviders(
|
|
<ProductGroupForm onClose={jest.fn()} onSuccess={jest.fn()} />
|
|
)
|
|
|
|
await waitFor(() =>
|
|
expectProcedureCalled(fetchMock, 'common.product.getAllSkusSummary')
|
|
)
|
|
fireEvent.changeText(screen.getByPlaceholderText('Enter group name'), 'Meat')
|
|
fireEvent.press(screen.getByText('Select products'))
|
|
fireEvent.press(await screen.findByLabelText('undefined-option-1'))
|
|
fireEvent.press(screen.getByText('Save'))
|
|
|
|
await waitFor(() => expectProcedureCalled(fetchMock, 'admin.product.createGroup'))
|
|
// Observed bug: product_ids contains the selected SKU id (1), not product id 10.
|
|
expect(lastProcedureInput(fetchMock, 'admin.product.createGroup')).toMatchObject({
|
|
group_name: 'Meat',
|
|
product_ids: [1],
|
|
})
|
|
})
|
|
|
|
it('updates the group with its seeded product ids', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
const onSuccess = jest.fn()
|
|
renderWithProviders(
|
|
<ProductGroupForm
|
|
group={existingGroup}
|
|
onClose={jest.fn()}
|
|
onSuccess={onSuccess}
|
|
/>
|
|
)
|
|
|
|
fireEvent.changeText(screen.getByDisplayValue('Beef Box'), 'Beef Combo')
|
|
fireEvent.press(screen.getByText('Save'))
|
|
|
|
await waitFor(() => expectProcedureCalled(fetchMock, 'admin.product.updateGroup'))
|
|
expect(lastProcedureInput(fetchMock, 'admin.product.updateGroup')).toEqual({
|
|
id: 5,
|
|
group_name: 'Beef Combo',
|
|
description: 'Bundle',
|
|
product_ids: [10, 11],
|
|
})
|
|
await waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
|
|
})
|
|
|
|
it('invalidates the getGroups query with the tRPC array key on create and update', async () => {
|
|
// Spy on an injected client: utils.admin.product.getGroups.invalidate() calls
|
|
// queryClient.invalidateQueries({ queryKey }), and getGroups is not mounted by
|
|
// this form, so nothing hits fetch — the invalidation itself is the observable.
|
|
const client = createQueryClient()
|
|
const invalidateSpy = jest.spyOn(client, 'invalidateQueries') // keeps calling through
|
|
|
|
// tRPC keys are [pathArray, ...]. The old bug passed a dotted string
|
|
// ('admin.product.getGroups'); only the array form is correct.
|
|
const pathOf = (call: unknown[]) => {
|
|
const key = (call[0] as { queryKey?: unknown[] } | undefined)?.queryKey
|
|
return Array.isArray(key) ? key[0] : null
|
|
}
|
|
|
|
// Create path — same props/flow as 'creates a group with the entered values'.
|
|
const created = renderWithProviders(
|
|
<ProductGroupForm onClose={jest.fn()} onSuccess={jest.fn()} />,
|
|
{ queryClient: client }
|
|
)
|
|
fireEvent.changeText(screen.getByPlaceholderText('Enter group name'), 'Meat')
|
|
fireEvent.press(screen.getByText('Save'))
|
|
|
|
await waitFor(() => expect(invalidateSpy).toHaveBeenCalled())
|
|
expect(invalidateSpy.mock.calls.map(pathOf)).toContainEqual([
|
|
'admin',
|
|
'product',
|
|
'getGroups',
|
|
])
|
|
|
|
created.unmount()
|
|
|
|
// Update path — same props/flow as 'updates the group with its seeded product ids'.
|
|
const callsAfterCreate = invalidateSpy.mock.calls.length
|
|
renderWithProviders(
|
|
<ProductGroupForm
|
|
group={existingGroup}
|
|
onClose={jest.fn()}
|
|
onSuccess={jest.fn()}
|
|
/>,
|
|
{ queryClient: client }
|
|
)
|
|
fireEvent.press(screen.getByText('Save'))
|
|
|
|
await waitFor(() =>
|
|
expect(invalidateSpy.mock.calls.length).toBeGreaterThan(callsAfterCreate)
|
|
)
|
|
expect(invalidateSpy.mock.calls.slice(callsAfterCreate).map(pathOf)).toContainEqual([
|
|
'admin',
|
|
'product',
|
|
'getGroups',
|
|
])
|
|
})
|
|
|
|
it('calls onClose from the Cancel button', () => {
|
|
const onClose = jest.fn()
|
|
renderWithProviders(
|
|
<ProductGroupForm onClose={onClose} onSuccess={jest.fn()} />
|
|
)
|
|
|
|
fireEvent.press(screen.getByText('Cancel'))
|
|
|
|
expect(onClose).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('calls onClose from the header close icon', () => {
|
|
const onClose = jest.fn()
|
|
renderWithProviders(
|
|
<ProductGroupForm onClose={onClose} onSuccess={jest.fn()} />
|
|
)
|
|
|
|
const closeIcon = screen
|
|
.UNSAFE_getAllByType(MaterialIcons)
|
|
.find((i) => i.props.name === 'close') as Parameters<typeof fireEvent.press>[0]
|
|
fireEvent.press(closeIcon)
|
|
|
|
expect(onClose).toHaveBeenCalledTimes(1)
|
|
})
|
|
}) |