92 lines
3.3 KiB
TypeScript
92 lines
3.3 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 7.2 — Edit slot (`tests/pages/slot-edit.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { fireEvent, waitFor } from '@testing-library/react-native'
|
|
|
|
import EditSlot from '@/app/(drawer)/dashboard/slots/edit/[id]'
|
|
import { renderWithProviders, renderAndFlush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { setProcedure, lastMutationInput, procedureMutationCalls } from '../mocks/trpc-client'
|
|
import { __setParams, router } from '../mocks/expo-router-mock'
|
|
import { SLOT_DETAIL } from '../fixtures'
|
|
|
|
function renderEditSlot() {
|
|
return renderAndFlush(<EditSlot />, { auth: 'in' })
|
|
}
|
|
|
|
describe('Edit slot (ASLOTEDIT-01..05)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
__setParams({ id: '1849' })
|
|
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
})
|
|
|
|
afterEach(() => alertSpy?.mockRestore())
|
|
|
|
it('ASLOTEDIT-01: shows the loading copy', () => {
|
|
setProcedure('admin.slots.getSlotById', { mode: 'pending' })
|
|
const view = renderWithProviders(<EditSlot />, { auth: 'in' })
|
|
|
|
// Loading assertions stay synchronous: the query never settles.
|
|
expect(view.getByText('Loading slot...')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTEDIT-02: a missing slot shows "Slot not found"', async () => {
|
|
setProcedure('admin.slots.getSlotById', { data: undefined })
|
|
const view = await renderEditSlot()
|
|
|
|
expect(view.getByText('Slot not found')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTEDIT-03: prefills times, active state, products and groups', async () => {
|
|
const view = await renderEditSlot()
|
|
|
|
expect(view.getByText('Edit Slot')).toBeTruthy()
|
|
expect(
|
|
view.getAllByText(new Date(SLOT_DETAIL.deliveryTime).toLocaleDateString()).length
|
|
).toBeGreaterThanOrEqual(1)
|
|
expect(
|
|
view.getAllByText(new Date(SLOT_DETAIL.freezeTime).toLocaleDateString()).length
|
|
).toBeGreaterThanOrEqual(1)
|
|
// The group selection is restored from the server payload.
|
|
expect(await view.findByText('Breakfast')).toBeTruthy()
|
|
expect(view.getByText('Update Slot')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTEDIT-04: updating sends updateSlot with the restored values', async () => {
|
|
const view = await renderEditSlot()
|
|
|
|
fireEvent.press(view.getByTestId('create-slot-button'))
|
|
|
|
await waitFor(() =>
|
|
expect(lastMutationInput('admin.slots.updateSlot')).toEqual({
|
|
id: 1849,
|
|
deliveryTime: new Date(SLOT_DETAIL.deliveryTime).toISOString(),
|
|
freezeTime: new Date(SLOT_DETAIL.freezeTime).toISOString(),
|
|
isActive: true,
|
|
groupIds: [1],
|
|
skuIds: [2, 5],
|
|
})
|
|
)
|
|
await waitFor(() =>
|
|
expect(alertSpy).toHaveBeenCalledWith('Success', 'Slot updated successfully!')
|
|
)
|
|
await waitFor(() => expect(router.back).toHaveBeenCalled())
|
|
})
|
|
|
|
it('ASLOTEDIT-05: an update failure alerts and stays on the form', async () => {
|
|
setProcedure('admin.slots.updateSlot', { mode: 'error', error: new Error('boom') })
|
|
const view = await renderEditSlot()
|
|
|
|
fireEvent.press(view.getByTestId('create-slot-button'))
|
|
|
|
await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Error', 'boom'))
|
|
expect(procedureMutationCalls('admin.slots.updateSlot')).toHaveLength(1)
|
|
expect(router.back).not.toHaveBeenCalled()
|
|
})
|
|
})
|