214 lines
6.7 KiB
TypeScript
214 lines
6.7 KiB
TypeScript
/**
|
|
* SlotForm component (components/SlotForm.tsx) — create/update mutations,
|
|
* validation and vendor-snippet handling.
|
|
*/
|
|
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
|
|
import SlotForm from '@/components/SlotForm'
|
|
import { setupApiMocks, resetStorage } from '../mocks/api'
|
|
import { resetStaffAuthMock } from '../mocks/staff-auth'
|
|
import { expectProcedureCalled, lastProcedureInput, procCount } from '../mocks/procedures'
|
|
import { searchParamsMock } from '../mocks/router'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
const DELIVERY = new Date('2026-10-01T10:00:00.000Z')
|
|
const FREEZE = new Date('2026-10-01T06:00:00.000Z')
|
|
|
|
const procedures = {
|
|
'admin.product.getGroups': { groups: [] },
|
|
'common.product.getAllSkusSummary': { skus: [] },
|
|
'admin.slots.createSlot': {
|
|
slot: { id: 10 },
|
|
createdSnippets: [],
|
|
message: 'Slot created successfully',
|
|
},
|
|
'admin.slots.updateSlot': {
|
|
slot: { id: 7 },
|
|
createdSnippets: [],
|
|
message: 'Slot updated successfully',
|
|
},
|
|
}
|
|
|
|
describe('SlotForm', () => {
|
|
beforeEach(async () => {
|
|
resetStaffAuthMock()
|
|
await resetStorage()
|
|
searchParamsMock.current = {}
|
|
setupApiMocks({ procedures })
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
|
})
|
|
|
|
it('renders the create form', () => {
|
|
renderWithProviders(
|
|
<SlotForm initialDeliveryTime={DELIVERY} initialFreezeTime={FREEZE} />
|
|
)
|
|
|
|
expect(screen.getByText('Create New Slot')).toBeVisible()
|
|
expect(screen.getByText('Create Slot')).toBeVisible()
|
|
expect(screen.getByText('Vendor Snippets')).toBeVisible()
|
|
expect(screen.getByText('Add Vendor Snippet')).toBeVisible()
|
|
})
|
|
|
|
it('blocks submit when a time is missing', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
|
|
renderWithProviders(<SlotForm />)
|
|
fireEvent.press(screen.getByTestId('create-slot-button'))
|
|
|
|
await waitFor(() =>
|
|
expect(Alert.alert).toHaveBeenCalledWith('Error', 'Please fill all fields')
|
|
)
|
|
expect(procCount(fetchMock, 'admin.slots.createSlot')).toBe(0)
|
|
})
|
|
|
|
it('blocks submit when freeze time is after delivery time', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
|
|
renderWithProviders(
|
|
<SlotForm
|
|
initialDeliveryTime={FREEZE}
|
|
initialFreezeTime={DELIVERY}
|
|
/>
|
|
)
|
|
fireEvent.press(screen.getByTestId('create-slot-button'))
|
|
|
|
await waitFor(() =>
|
|
expect(Alert.alert).toHaveBeenCalledWith(
|
|
'Error',
|
|
'Freeze time must be before or equal to delivery time'
|
|
)
|
|
)
|
|
expect(procCount(fetchMock, 'admin.slots.createSlot')).toBe(0)
|
|
})
|
|
|
|
it('creates a slot with the mapped payload and calls onSlotAdded', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
const onSlotAdded = jest.fn()
|
|
|
|
renderWithProviders(
|
|
<SlotForm
|
|
initialDeliveryTime={DELIVERY}
|
|
initialFreezeTime={FREEZE}
|
|
initialProductIds={[11, 12]}
|
|
initialGroupIds={[3]}
|
|
onSlotAdded={onSlotAdded}
|
|
/>
|
|
)
|
|
|
|
fireEvent.press(screen.getByTestId('create-slot-button'))
|
|
|
|
await waitFor(() =>
|
|
expectProcedureCalled(fetchMock, 'admin.slots.createSlot')
|
|
)
|
|
|
|
expect(lastProcedureInput(fetchMock, 'admin.slots.createSlot')).toEqual({
|
|
deliveryTime: '2026-10-01T10:00:00.000Z',
|
|
freezeTime: '2026-10-01T06:00:00.000Z',
|
|
isActive: true,
|
|
groupIds: [3],
|
|
skuIds: [11, 12],
|
|
vendorSnippets: [],
|
|
})
|
|
await waitFor(() => expect(onSlotAdded).toHaveBeenCalled())
|
|
expect(Alert.alert).toHaveBeenCalledWith('Success', 'Slot created successfully!')
|
|
})
|
|
|
|
it('includes an added vendor snippet in the create payload', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
|
|
renderWithProviders(
|
|
<SlotForm initialDeliveryTime={DELIVERY} initialFreezeTime={FREEZE} />
|
|
)
|
|
|
|
fireEvent.press(screen.getByText('Add Vendor Snippet'))
|
|
fireEvent.changeText(
|
|
screen.getByPlaceholderText('Enter snippet name'),
|
|
'Diwali Promo'
|
|
)
|
|
fireEvent.press(screen.getByTestId('create-slot-button'))
|
|
|
|
await waitFor(() =>
|
|
expectProcedureCalled(fetchMock, 'admin.slots.createSlot')
|
|
)
|
|
|
|
expect(
|
|
(lastProcedureInput(fetchMock, 'admin.slots.createSlot') as any).vendorSnippets
|
|
).toEqual([{ name: 'Diwali Promo', skuIds: [], validTill: '' }])
|
|
})
|
|
|
|
it('updates an existing slot in edit mode', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
const onSlotAdded = jest.fn()
|
|
|
|
renderWithProviders(
|
|
<SlotForm
|
|
slotId={7}
|
|
initialDeliveryTime={DELIVERY}
|
|
initialFreezeTime={FREEZE}
|
|
initialProductIds={[11]}
|
|
onSlotAdded={onSlotAdded}
|
|
/>
|
|
)
|
|
|
|
expect(screen.getByText('Edit Slot')).toBeVisible()
|
|
fireEvent.press(screen.getByTestId('create-slot-button'))
|
|
|
|
await waitFor(() =>
|
|
expectProcedureCalled(fetchMock, 'admin.slots.updateSlot')
|
|
)
|
|
expectProcedureCalled(fetchMock, 'admin.slots.getSlotById')
|
|
|
|
expect(lastProcedureInput(fetchMock, 'admin.slots.updateSlot')).toEqual({
|
|
id: 7,
|
|
deliveryTime: '2026-10-01T10:00:00.000Z',
|
|
freezeTime: '2026-10-01T06:00:00.000Z',
|
|
isActive: true,
|
|
groupIds: [],
|
|
skuIds: [11],
|
|
vendorSnippets: [],
|
|
})
|
|
await waitFor(() => expect(onSlotAdded).toHaveBeenCalled())
|
|
expect(Alert.alert).toHaveBeenCalledWith('Success', 'Slot updated successfully!')
|
|
})
|
|
|
|
it('refetches the mounted slot detail after a successful update in edit mode', async () => {
|
|
const { fetchMock } = setupApiMocks({ procedures })
|
|
|
|
renderWithProviders(
|
|
<SlotForm
|
|
slotId={7}
|
|
initialDeliveryTime={DELIVERY}
|
|
initialFreezeTime={FREEZE}
|
|
initialProductIds={[11]}
|
|
/>
|
|
)
|
|
|
|
// Edit mode mounts `admin.slots.getSlotById` with enabled: true, so there is
|
|
// a live query under `admin.slots` for the invalidation to refetch.
|
|
await waitFor(() =>
|
|
expect(procCount(fetchMock, 'admin.slots.getSlotById')).toBeGreaterThan(0)
|
|
)
|
|
|
|
// Baseline captured immediately before the press: the tests share one
|
|
// module-singleton httpBatchLink client, so counts can leak across cases —
|
|
// assert a delta, never an absolute number.
|
|
const before = procCount(fetchMock, 'admin.slots.getSlotById')
|
|
|
|
fireEvent.press(screen.getByTestId('create-slot-button'))
|
|
|
|
await waitFor(() =>
|
|
expectProcedureCalled(fetchMock, 'admin.slots.updateSlot')
|
|
)
|
|
|
|
// `utils.admin.slots.invalidate()` (SlotForm.tsx update onSuccess) invalidates
|
|
// the whole `admin.slots` path, which refetches the mounted detail query.
|
|
await waitFor(() =>
|
|
expect(procCount(fetchMock, 'admin.slots.getSlotById')).toBeGreaterThan(
|
|
before
|
|
)
|
|
)
|
|
})
|
|
})
|