freshyo/apps/admin-ui/tests/pages/slots.test.tsx
2026-10-20 00:31:05 +05:30

224 lines
6.9 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 } from '@testing-library/react-native'
import dayjs from 'dayjs'
import Slots from '@/app/(drawer)/dashboard/slots/index'
import { renderWithProviders, 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 { SLOT_1 } from '../fixtures'
const HOUR = 60 * 60 * 1000
const iso = (offset: number) => new Date(Date.now() + offset).toISOString()
function renderSlots() {
return renderWithProviders(<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 = renderSlots()
expect(view.getByText('Loading slots...')).toBeTruthy()
})
it('ASLOTUI-02: an empty response renders no slot rows', () => {
setProcedure('admin.slots.getAll', { data: { slots: [], count: 0 } })
const view = 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', () => {
const view = 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', () => {
const view = renderSlots()
expect(view.getByText('Inactive')).toBeTruthy()
})
it('ASLOTUI-05: a full-capacity slot shows the FULL badge', () => {
const view = renderSlots()
expect(view.getByText('FULL')).toBeTruthy()
})
it('ASLOTUI-06: more than two products shows "+N more" and opens the dialog', () => {
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 = 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', () => {
const view = 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', () => {
const view = 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', () => {
const view = 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 = renderSlots()
fireEvent.press(view.getByTestId('slot-menu-1849'))
fireEvent.press(view.getByText('Mark as Full Capacity'))
await flush()
expect(lastMutationInput('admin.slots.updateSlotCapacity')).toEqual({
slotId: 1849,
isCapacityFull: true,
})
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 = renderSlots()
fireEvent.press(view.getByTestId('slot-menu-1850'))
fireEvent.press(view.getByText('Mark as Full Capacity'))
await flush()
expect(lastMutationInput('admin.slots.updateSlotCapacity')).toEqual({
slotId: 1850,
isCapacityFull: false,
})
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 = renderSlots()
fireEvent.press(view.getByTestId('slot-menu-1849'))
fireEvent.press(view.getByText('Mark as Full Capacity'))
await flush()
expect(alertSpy).toHaveBeenCalledWith('Error', 'nope')
})
it('ASLOTUI-13: replicate opens add with the base slot param', () => {
const view = 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', () => {
const view = 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', () => {
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 = 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', () => {
setProcedure('admin.slots.getAll', {
data: { slots: [{ ...SLOT_1, products: [] }], count: 1 },
})
const view = renderSlots()
expect(view.getByText('Slot #1849')).toBeTruthy()
expect(view.queryByText('Products')).toBeNull()
})
})