144 lines
4.8 KiB
TypeScript
144 lines
4.8 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 7.3 — SlotForm
|
|
* (`tests/components/slot-form.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { fireEvent } from '@testing-library/react-native'
|
|
|
|
import SlotForm from '@/components/SlotForm'
|
|
import { renderWithProviders, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { setProcedure, lastMutationInput, procedureMutationCalls } from '../mocks/trpc-client'
|
|
|
|
const HOUR = 60 * 60 * 1000
|
|
|
|
type View = ReturnType<typeof renderWithProviders>
|
|
|
|
function renderSlotForm(props: Partial<React.ComponentProps<typeof SlotForm>> = {}) {
|
|
return renderWithProviders(<SlotForm {...props} />, { auth: 'in' })
|
|
}
|
|
|
|
function chooseDate(view: View, field: 'delivery' | 'freeze', date: Date) {
|
|
fireEvent.press(view.getByTestId(`${field}-date-picker`))
|
|
fireEvent(view.getByTestId('dateTimePicker'), 'change', { type: 'set' }, date)
|
|
}
|
|
|
|
function setDates(view: View, deliveryOffset = 3, freezeOffset = 2) {
|
|
const delivery = new Date(Date.now() + deliveryOffset * HOUR)
|
|
const freeze = new Date(Date.now() + freezeOffset * HOUR)
|
|
chooseDate(view, 'delivery', delivery)
|
|
chooseDate(view, 'freeze', freeze)
|
|
return { delivery, freeze }
|
|
}
|
|
|
|
function selectSku(view: View, testId: string, skuId: number) {
|
|
fireEvent.press(view.getByTestId(testId))
|
|
fireEvent.press(view.getByTestId(`${testId}-option-${skuId}`))
|
|
fireEvent.press(view.getByTestId(`${testId}-done`))
|
|
}
|
|
|
|
describe('SlotForm (ASLOTF-01..10)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
})
|
|
|
|
afterEach(() => alertSpy?.mockRestore())
|
|
|
|
it('ASLOTF-01: renders the create fields', () => {
|
|
const view = renderSlotForm()
|
|
|
|
expect(view.getByText('Create New Slot')).toBeTruthy()
|
|
expect(view.getByText('Delivery Date & Time')).toBeTruthy()
|
|
expect(view.getByText('Freeze Date & Time')).toBeTruthy()
|
|
expect(view.getByText('Create Slot')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTF-02: missing delivery/freeze times are rejected', async () => {
|
|
const view = renderSlotForm()
|
|
|
|
fireEvent.press(view.getByTestId('create-slot-button'))
|
|
await flush()
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith('Error', 'Please fill all fields')
|
|
expect(procedureMutationCalls('admin.slots.createSlot')).toHaveLength(0)
|
|
})
|
|
|
|
it('ASLOTF-03: freeze after delivery is rejected', async () => {
|
|
const view = renderSlotForm()
|
|
|
|
// Freeze is later than delivery.
|
|
setDates(view, 1, 4)
|
|
fireEvent.press(view.getByTestId('create-slot-button'))
|
|
await flush()
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith(
|
|
'Error',
|
|
'Freeze time must be before or equal to delivery time'
|
|
)
|
|
expect(procedureMutationCalls('admin.slots.createSlot')).toHaveLength(0)
|
|
})
|
|
|
|
it('ASLOTF-04: selecting products sends the SKU ids', async () => {
|
|
const view = renderSlotForm()
|
|
await flush()
|
|
|
|
selectSku(view, 'slot-products-selector', 13)
|
|
const { delivery, freeze } = setDates(view)
|
|
fireEvent.press(view.getByTestId('create-slot-button'))
|
|
await flush(2)
|
|
|
|
expect(lastMutationInput('admin.slots.createSlot')).toEqual(
|
|
expect.objectContaining({
|
|
deliveryTime: delivery.toISOString(),
|
|
freezeTime: freeze.toISOString(),
|
|
skuIds: [13],
|
|
})
|
|
)
|
|
})
|
|
|
|
it('ASLOTF-08: group selection is sent as group ids', async () => {
|
|
const view = renderSlotForm()
|
|
await flush()
|
|
|
|
fireEvent.press(view.getByTestId('product-groups-dropdown'))
|
|
fireEvent.press(view.getByTestId('product-groups-dropdown-option-1'))
|
|
fireEvent.press(view.getByTestId('product-groups-dropdown-done'))
|
|
setDates(view)
|
|
fireEvent.press(view.getByTestId('create-slot-button'))
|
|
await flush(2)
|
|
|
|
expect(lastMutationInput('admin.slots.createSlot')).toEqual(
|
|
expect.objectContaining({ groupIds: [1] })
|
|
)
|
|
})
|
|
|
|
it('ASLOTF-09: create and edit titles/buttons', () => {
|
|
const create = renderSlotForm()
|
|
expect(create.getByText('Create New Slot')).toBeTruthy()
|
|
expect(create.getByText('Create Slot')).toBeTruthy()
|
|
create.unmount()
|
|
|
|
const edit = renderSlotForm({ slotId: 1849 })
|
|
expect(edit.getByText('Edit Slot')).toBeTruthy()
|
|
expect(edit.getByText('Update Slot')).toBeTruthy()
|
|
})
|
|
|
|
it('ASLOTF-10: a pending submit shows "Creating..." and ignores further presses', async () => {
|
|
setProcedure('admin.slots.createSlot', { mode: 'pending' })
|
|
const view = renderSlotForm()
|
|
|
|
setDates(view)
|
|
fireEvent.press(view.getByTestId('create-slot-button'))
|
|
await flush(2)
|
|
|
|
expect(view.getByText('Creating...')).toBeTruthy()
|
|
fireEvent.press(view.getByTestId('create-slot-button'))
|
|
await flush(2)
|
|
expect(procedureMutationCalls('admin.slots.createSlot')).toHaveLength(1)
|
|
})
|
|
})
|