158 lines
5.3 KiB
TypeScript
158 lines
5.3 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 11.1 — Product groupings list (`tests/pages/product-groupings.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert, DeviceEventEmitter } from 'react-native'
|
|
import { act, fireEvent } from '@testing-library/react-native'
|
|
import { MyTouchableOpacity } from 'common-ui'
|
|
import { MaterialIcons } from '@expo/vector-icons'
|
|
|
|
import ProductGroupings from '@/app/(drawer)/dashboard/product-groupings/index'
|
|
import { renderWithProviders, 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'
|
|
import { GROUP_1 } from '../fixtures'
|
|
|
|
type View = Awaited<ReturnType<typeof renderGroups>>['view']
|
|
|
|
async function renderGroups(groups: any[] = [GROUP_1]) {
|
|
setProcedure('admin.product.getGroups', { data: { groups } })
|
|
const view = await renderAndFlush(<ProductGroupings />, { auth: false })
|
|
return { view }
|
|
}
|
|
|
|
function icon(view: View, name: string) {
|
|
return view.UNSAFE_getAllByType(MaterialIcons).find((node: any) => node.props.name === name)
|
|
}
|
|
|
|
async function confirmDelete(alertSpy: jest.SpyInstance) {
|
|
const buttons = alertSpy.mock.calls[0][2]
|
|
await act(async () => {
|
|
await buttons[1].onPress?.()
|
|
})
|
|
await flush(2)
|
|
}
|
|
|
|
describe('Product groupings list (AGRP-01..11)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
})
|
|
|
|
afterEach(() => alertSpy?.mockRestore())
|
|
|
|
it('AGRP-01: shows the loading copy', async () => {
|
|
setProcedure('admin.product.getGroups', { mode: 'pending' })
|
|
const view = renderWithProviders(<ProductGroupings />, { auth: false })
|
|
|
|
expect(view.getByText('Loading product groups...')).toBeTruthy()
|
|
})
|
|
|
|
it('AGRP-02: shows the error copy', async () => {
|
|
setProcedure('admin.product.getGroups', { mode: 'error', error: new Error('no') })
|
|
const view = renderWithProviders(<ProductGroupings />, { auth: false })
|
|
|
|
expect(await view.findByText('Error loading groups')).toBeTruthy()
|
|
})
|
|
|
|
it('AGRP-03: shows the empty state with the create action', async () => {
|
|
const { view } = await renderGroups([])
|
|
|
|
expect(view.getByText('No Groups Yet')).toBeTruthy()
|
|
expect(
|
|
view.getByText('Start by creating your first product group using the button below.')
|
|
).toBeTruthy()
|
|
expect(view.UNSAFE_getByType(MyTouchableOpacity)).toBeTruthy()
|
|
})
|
|
|
|
it('AGRP-04: renders the group name, description and product count', async () => {
|
|
const { view } = await renderGroups([GROUP_1])
|
|
|
|
expect(view.getByText('Breakfast')).toBeTruthy()
|
|
expect(view.getByText('Morning items')).toBeTruthy()
|
|
expect(view.getByText('2 Products')).toBeTruthy()
|
|
expect(view.getByText('Group Name')).toBeTruthy()
|
|
})
|
|
|
|
it('AGRP-05: View Products currently only sets local state', async () => {
|
|
const { view } = await renderGroups([GROUP_1])
|
|
|
|
// The page imports BottomDialog but never renders it, so pressing the count
|
|
// is inert beyond setViewProducts.
|
|
fireEvent.press(view.getByText('2 Products'))
|
|
|
|
expect(view.getByText('Breakfast')).toBeTruthy()
|
|
expect(view.queryByText('Products in group')).toBeNull()
|
|
})
|
|
|
|
it('AGRP-06: Edit opens the edit route', async () => {
|
|
const { view } = await renderGroups([GROUP_1])
|
|
|
|
fireEvent.press(icon(view, 'edit'))
|
|
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/product-groupings/edit/1')
|
|
})
|
|
|
|
it('AGRP-07: Delete asks for confirmation and Cancel makes no call', async () => {
|
|
const { view } = await renderGroups([GROUP_1])
|
|
|
|
fireEvent.press(icon(view, 'delete'))
|
|
expect(alertSpy).toHaveBeenCalledWith(
|
|
'Delete Group',
|
|
'Are you sure you want to delete this group?',
|
|
expect.any(Array)
|
|
)
|
|
const buttons = alertSpy.mock.calls[0][2]
|
|
await act(async () => {
|
|
buttons[0].onPress?.()
|
|
})
|
|
|
|
expect(procedureMutationCalls('admin.product.deleteGroup')).toHaveLength(0)
|
|
})
|
|
|
|
it('AGRP-08: confirming Delete calls deleteGroup', async () => {
|
|
const { view } = await renderGroups([GROUP_1])
|
|
|
|
fireEvent.press(icon(view, 'delete'))
|
|
await confirmDelete(alertSpy)
|
|
|
|
expect(lastMutationInput('admin.product.deleteGroup')).toEqual({ id: 1 })
|
|
})
|
|
|
|
it('AGRP-09: a failed delete currently does not alert', async () => {
|
|
setProcedure('admin.product.deleteGroup', { mode: 'error', error: new Error('no') })
|
|
const { view } = await renderGroups([GROUP_1])
|
|
|
|
fireEvent.press(icon(view, 'delete'))
|
|
await confirmDelete(alertSpy)
|
|
|
|
// The mutation has no onError handler, so no alert is raised.
|
|
expect(alertSpy.mock.calls.every((call) => call[0] !== 'Error')).toBe(true)
|
|
})
|
|
|
|
it('AGRP-10: the create FAB opens the create route', async () => {
|
|
const { view } = await renderGroups([])
|
|
|
|
fireEvent.press(view.UNSAFE_getByType(MyTouchableOpacity))
|
|
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/product-groupings/create')
|
|
})
|
|
|
|
it('AGRP-11: a refresh event keeps the list rendered', async () => {
|
|
const { view } = await renderGroups([GROUP_1])
|
|
|
|
DeviceEventEmitter.emit('refresh_event')
|
|
await flush()
|
|
|
|
expect(view.getByText('Breakfast')).toBeTruthy()
|
|
expect(view.getByText('2 Products')).toBeTruthy()
|
|
})
|
|
})
|