227 lines
7.1 KiB
TypeScript
227 lines
7.1 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 7.1 — Slot list (`tests/pages/slots.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { fireEvent, waitFor } from '@testing-library/react-native'
|
|
import dayjs from 'dayjs'
|
|
|
|
import Slots from '@/app/(drawer)/dashboard/slots/index'
|
|
import { renderWithProviders, renderAndFlush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { setProcedure, lastMutationInput } from '../mocks/trpc-client'
|
|
import { router } from '../mocks/expo-router-mock'
|
|
import { SLOT_1 } from '../fixtures'
|
|
|
|
function renderSlots() {
|
|
return renderAndFlush(<Slots />, { auth: 'in' })
|
|
}
|
|
|
|
describe('Slot list (ASLOTUI-01..16)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
})
|
|
|
|
afterEach(() => alertSpy?.mockRestore())
|
|
|
|
it('ASLOTUI-01: shows the loading copy', () => {
|
|
setProcedure('admin.slots.getAll', { mode: 'pending' })
|
|
const view = renderWithProviders(<Slots />, { auth: 'in' })
|
|
|
|
// Loading assertions stay synchronous: the query never settles.
|
|
expect(view.getByText('Loading slots...')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTUI-02: an empty response renders no slot rows', async () => {
|
|
setProcedure('admin.slots.getAll', { data: { slots: [], count: 0 } })
|
|
const view = await renderSlots()
|
|
|
|
expect(view.queryByText(/^Slot #/)).toBeNull()
|
|
expect(view.getByTestId('add-slot-fab')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTUI-03: renders slot id, delivery/freeze times, status and products', async () => {
|
|
const view = await renderSlots()
|
|
|
|
expect(view.getByText('Slot #1849')).toBeTruthy()
|
|
expect(view.getByText('ID: 1849')).toBeTruthy()
|
|
expect(view.getByText(dayjs(SLOT_1.deliveryTime).format('DD MMM, h:mm A'))).toBeTruthy()
|
|
expect(view.getByText(dayjs(SLOT_1.freezeTime).format('DD MMM, h:mm A'))).toBeTruthy()
|
|
expect(view.getAllByText('Chicken Breast, Cucumber').length).toBeGreaterThanOrEqual(1)
|
|
expect(view.getAllByText('Active').length).toBeGreaterThanOrEqual(1)
|
|
})
|
|
|
|
it('ASLOTUI-04: an inactive slot shows the Inactive status', async () => {
|
|
const view = await renderSlots()
|
|
|
|
expect(view.getByText('Inactive')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTUI-05: a full-capacity slot shows the FULL badge', async () => {
|
|
const view = await renderSlots()
|
|
|
|
expect(view.getByText('FULL')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTUI-06: more than two products shows "+N more" and opens the dialog', async () => {
|
|
setProcedure('admin.slots.getAll', {
|
|
data: {
|
|
slots: [
|
|
{
|
|
...SLOT_1,
|
|
products: [
|
|
{ id: 2, name: 'Chicken Breast' },
|
|
{ id: 5, name: 'Cucumber' },
|
|
{ id: 7, name: 'Paneer' },
|
|
{ id: 9, name: 'Mutton' },
|
|
],
|
|
},
|
|
],
|
|
count: 1,
|
|
},
|
|
})
|
|
const view = await renderSlots()
|
|
|
|
expect(view.getByText('+2 more')).toBeTruthy()
|
|
fireEvent.press(view.getByText('+2 more'))
|
|
expect(view.getByText('All Products')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTUI-07: tapping a slot card opens the details route', async () => {
|
|
const view = await renderSlots()
|
|
|
|
fireEvent.press(view.getByText('Slot #1849'))
|
|
|
|
expect(router.push).toHaveBeenCalledWith(
|
|
'/(drawer)/dashboard/slots/slot-details?slotId=1849'
|
|
)
|
|
})
|
|
|
|
it('ASLOTUI-08: the edit chip opens the edit route', async () => {
|
|
const view = await renderSlots()
|
|
|
|
fireEvent.press(view.getByTestId('edit-slot-1849'))
|
|
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/slots/edit/1849')
|
|
})
|
|
|
|
it('ASLOTUI-09: the menu opens the actions dialog', async () => {
|
|
const view = await renderSlots()
|
|
|
|
fireEvent.press(view.getByTestId('slot-menu-1849'))
|
|
|
|
expect(view.getByText('Slot #1849 Actions')).toBeTruthy()
|
|
expect(view.getByText('Mark as Full Capacity')).toBeTruthy()
|
|
expect(view.getByText('Replicate Slot')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTUI-10: marking a slot full sends isCapacityFull true', async () => {
|
|
const view = await renderSlots()
|
|
|
|
fireEvent.press(view.getByTestId('slot-menu-1849'))
|
|
fireEvent.press(view.getByText('Mark as Full Capacity'))
|
|
|
|
await waitFor(() =>
|
|
expect(lastMutationInput('admin.slots.updateSlotCapacity')).toEqual({
|
|
slotId: 1849,
|
|
isCapacityFull: true,
|
|
})
|
|
)
|
|
await waitFor(() =>
|
|
expect(alertSpy).toHaveBeenCalledWith(
|
|
'Success',
|
|
'Slot marked as full capacity. It will be hidden from users.'
|
|
)
|
|
)
|
|
})
|
|
|
|
it('ASLOTUI-11: marking a full slot not-full sends isCapacityFull false', async () => {
|
|
const view = await renderSlots()
|
|
|
|
fireEvent.press(view.getByTestId('slot-menu-1850'))
|
|
fireEvent.press(view.getByText('Mark as Full Capacity'))
|
|
|
|
await waitFor(() =>
|
|
expect(lastMutationInput('admin.slots.updateSlotCapacity')).toEqual({
|
|
slotId: 1850,
|
|
isCapacityFull: false,
|
|
})
|
|
)
|
|
await waitFor(() =>
|
|
expect(alertSpy).toHaveBeenCalledWith(
|
|
'Success',
|
|
'Slot capacity reset. It will now be visible to users.'
|
|
)
|
|
)
|
|
})
|
|
|
|
it('ASLOTUI-12: a failed capacity update alerts the error', async () => {
|
|
setProcedure('admin.slots.updateSlotCapacity', {
|
|
mode: 'error',
|
|
error: new Error('nope'),
|
|
})
|
|
const view = await renderSlots()
|
|
|
|
fireEvent.press(view.getByTestId('slot-menu-1849'))
|
|
fireEvent.press(view.getByText('Mark as Full Capacity'))
|
|
|
|
await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Error', 'nope'))
|
|
})
|
|
|
|
it('ASLOTUI-13: replicate opens add with the base slot param', async () => {
|
|
const view = await renderSlots()
|
|
|
|
fireEvent.press(view.getByTestId('slot-menu-1849'))
|
|
fireEvent.press(view.getByText('Replicate Slot'))
|
|
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/slots/add?baseslot=1849')
|
|
})
|
|
|
|
it('ASLOTUI-14: the FAB opens the add route', async () => {
|
|
const view = await renderSlots()
|
|
|
|
fireEvent.press(view.getByTestId('add-slot-fab'))
|
|
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/slots/add')
|
|
})
|
|
|
|
it('ASLOTUI-15: the products dialog lists every product name', async () => {
|
|
setProcedure('admin.slots.getAll', {
|
|
data: {
|
|
slots: [
|
|
{
|
|
...SLOT_1,
|
|
products: [
|
|
{ id: 2, name: 'Chicken Breast' },
|
|
{ id: 5, name: 'Cucumber' },
|
|
{ id: 7, name: 'Paneer' },
|
|
],
|
|
},
|
|
],
|
|
count: 1,
|
|
},
|
|
})
|
|
const view = await renderSlots()
|
|
|
|
fireEvent.press(view.getByText('+1 more'))
|
|
|
|
expect(view.getByText('All Products')).toBeTruthy()
|
|
expect(view.getByText('Chicken Breast')).toBeTruthy()
|
|
expect(view.getByText('Cucumber')).toBeTruthy()
|
|
expect(view.getByText('Paneer')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTUI-16: a slot without products renders no product row', async () => {
|
|
setProcedure('admin.slots.getAll', {
|
|
data: { slots: [{ ...SLOT_1, products: [] }], count: 1 },
|
|
})
|
|
const view = await renderSlots()
|
|
|
|
expect(view.getByText('Slot #1849')).toBeTruthy()
|
|
expect(view.queryByText('Products')).toBeNull()
|
|
})
|
|
})
|