112 lines
3.5 KiB
TypeScript
112 lines
3.5 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 11.2 — Product group edit (`tests/pages/product-grouping-edit.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { fireEvent } from '@testing-library/react-native'
|
|
|
|
import EditProductGroup from '@/app/(drawer)/dashboard/product-groupings/edit/[id]'
|
|
import { renderWithProviders, renderAndFlush, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import {
|
|
setProcedure,
|
|
lastMutationInput,
|
|
procedureInvalidations,
|
|
} from '../mocks/trpc-client'
|
|
import { router, __setParams } from '../mocks/expo-router-mock'
|
|
import { GROUP_1 } from '../fixtures'
|
|
|
|
type View = Awaited<ReturnType<typeof renderEdit>>['view']
|
|
|
|
async function renderEdit(id = '1', groups = [GROUP_1]) {
|
|
__setParams({ id })
|
|
setProcedure('admin.product.getGroups', { data: { groups } })
|
|
const view = await renderAndFlush(<EditProductGroup />, { auth: false })
|
|
await flush(2)
|
|
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 edit (AGRPE-01..08)', () => {
|
|
beforeEach(() => resetAllMocks())
|
|
|
|
it('AGRPE-01: loads the group into the edit form', async () => {
|
|
const { view } = await renderEdit()
|
|
|
|
expect(view.getByText('Edit Product Group')).toBeTruthy()
|
|
expect(nameInput(view).props.value).toBe('Breakfast')
|
|
})
|
|
|
|
it('AGRPE-02: shows loading while the groups query is pending', async () => {
|
|
setProcedure('admin.product.getGroups', { mode: 'pending' })
|
|
__setParams({ id: '1' })
|
|
const view = renderWithProviders(<EditProductGroup />, { auth: false })
|
|
|
|
expect(view.getByText('Loading group...')).toBeTruthy()
|
|
})
|
|
|
|
it('AGRPE-03: prefills name, description and products', async () => {
|
|
const { view } = await renderEdit()
|
|
|
|
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('AGRPE-04: saving calls updateGroup with the edited values', async () => {
|
|
const { view } = await renderEdit()
|
|
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],
|
|
})
|
|
})
|
|
|
|
it('AGRPE-05: an unknown group id shows Group not found', async () => {
|
|
const { view } = await renderEdit('999')
|
|
|
|
expect(view.getByText('Group not found')).toBeTruthy()
|
|
})
|
|
|
|
it('AGRPE-06: product ids are the group SKU ids end-to-end', async () => {
|
|
const { view } = await renderEdit()
|
|
expect(view.getByText('Chicken Breast - 500g, Cucumber - 500g')).toBeTruthy()
|
|
|
|
await submit(view)
|
|
|
|
expect(lastMutationInput('admin.product.updateGroup').product_ids).toEqual([11, 13])
|
|
})
|
|
|
|
it('AGRPE-07: success invalidates the groups list and goes back', async () => {
|
|
const { view } = await renderEdit()
|
|
fireEvent.changeText(nameInput(view), 'Updated')
|
|
|
|
await submit(view)
|
|
|
|
expect(procedureInvalidations('admin.product.getGroups').length).toBeGreaterThan(0)
|
|
expect(router.back).toHaveBeenCalled()
|
|
})
|
|
|
|
it('AGRPE-08: Cancel goes back', async () => {
|
|
const { view } = await renderEdit()
|
|
|
|
fireEvent.press(view.getByText('Cancel'))
|
|
|
|
expect(router.back).toHaveBeenCalled()
|
|
})
|
|
})
|