183 lines
6.7 KiB
TypeScript
183 lines
6.7 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 15 — VendorSnippetForm
|
|
* (`tests/components/vendor-snippet-form.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { fireEvent } from '@testing-library/react-native'
|
|
|
|
jest.mock('@/components/context/staff-auth-context', () =>
|
|
require('../mocks/staff-auth-context')
|
|
)
|
|
|
|
import VendorSnippetForm from '@/components/VendorSnippetForm'
|
|
import { __setStaffState } from '../mocks/staff-auth-context'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { flush, renderWithProviders } from '../helpers/test-utils'
|
|
import { lastMutationInput, procedureMutationCalls } from '../mocks/trpc-client'
|
|
import { SLOT_1, SNIPPET_1, STAFF } from '../fixtures'
|
|
|
|
function renderForm(props: Partial<React.ComponentProps<typeof VendorSnippetForm>> = {}) {
|
|
return renderWithProviders(
|
|
<VendorSnippetForm onClose={jest.fn()} onSuccess={jest.fn()} {...props} />,
|
|
{ auth: 'in' }
|
|
)
|
|
}
|
|
|
|
async function selectProduct(view: ReturnType<typeof renderForm>, label: string) {
|
|
fireEvent.press(view.getByText('Select products'))
|
|
fireEvent.press(view.getByText(label))
|
|
fireEvent.press(view.getByText('Done'))
|
|
}
|
|
|
|
describe('VendorSnippetForm (AVFORM-01..08)', () => {
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
__setStaffState({ isLoggedIn: true, isLoading: false, staff: STAFF })
|
|
})
|
|
|
|
afterEach(() => {
|
|
jest.restoreAllMocks()
|
|
})
|
|
|
|
it('AVFORM-01: renders code / slot / products / validTill / permanent fields', () => {
|
|
const view = renderForm()
|
|
|
|
expect(view.getByText('Create Vendor Snippet')).toBeTruthy()
|
|
expect(view.getByText('Snippet Code')).toBeTruthy()
|
|
expect(view.getByText('Is Permanent?')).toBeTruthy()
|
|
expect(view.getByText('Delivery Slot')).toBeTruthy()
|
|
expect(view.getByText('Choose a delivery slot')).toBeTruthy()
|
|
expect(view.getByText('Select products')).toBeTruthy()
|
|
expect(view.getByText('Valid Till (Optional)')).toBeTruthy()
|
|
expect(view.getByText('Create Snippet')).toBeTruthy()
|
|
|
|
// A unique snippet code is generated on mount for new snippets.
|
|
const codeInput = view.getByPlaceholderText('Enter snippet code')
|
|
expect(String(codeInput.props.value)).toMatch(/^VS_\d+_[a-z0-9]+$/)
|
|
})
|
|
|
|
it('AVFORM-02: an empty code blocks submit with a validation message', async () => {
|
|
const view = renderForm()
|
|
|
|
fireEvent.changeText(view.getByPlaceholderText('Enter snippet code'), '')
|
|
fireEvent.press(view.getByText('Create Snippet'))
|
|
await flush()
|
|
|
|
expect(view.getByText('Snippet code is required')).toBeTruthy()
|
|
expect(procedureMutationCalls('admin.vendorSnippets.create')).toHaveLength(0)
|
|
})
|
|
|
|
it('AVFORM-03: submitting without products blocks the mutation', async () => {
|
|
const view = renderForm()
|
|
|
|
fireEvent.changeText(view.getByPlaceholderText('Enter snippet code'), 'CODE123')
|
|
fireEvent.press(view.getByText('Is Permanent?'))
|
|
fireEvent.press(view.getByText('Create Snippet'))
|
|
await flush()
|
|
|
|
expect(view.getByText('At least one product must be selected')).toBeTruthy()
|
|
expect(procedureMutationCalls('admin.vendorSnippets.create')).toHaveLength(0)
|
|
})
|
|
|
|
it('AVFORM-04: permanent snippets hide the delivery slot section', () => {
|
|
const view = renderForm()
|
|
|
|
expect(view.getByText('Delivery Slot')).toBeTruthy()
|
|
fireEvent.press(view.getByText('Is Permanent?'))
|
|
|
|
expect(view.queryByText('Delivery Slot')).toBeNull()
|
|
expect(view.queryByText('Select Slot')).toBeNull()
|
|
// validTill is not tied to the permanence toggle in the current form.
|
|
expect(view.getByText('Valid Till (Optional)')).toBeTruthy()
|
|
})
|
|
|
|
it('AVFORM-05: the product selector stores the selected SKU ids', async () => {
|
|
const view = renderForm()
|
|
await flush()
|
|
|
|
fireEvent.changeText(view.getByPlaceholderText('Enter snippet code'), 'CODE123')
|
|
fireEvent.press(view.getByText('Is Permanent?'))
|
|
|
|
fireEvent.press(view.getByText('Select products'))
|
|
fireEvent.press(view.getByText('Chicken Breast - 500g'))
|
|
fireEvent.press(view.getByText('Chicken Breast - 1kg'))
|
|
fireEvent.press(view.getByText('Done'))
|
|
|
|
expect(view.getByText('Chicken Breast - 500g, Chicken Breast - 1kg')).toBeTruthy()
|
|
|
|
fireEvent.press(view.getByText('Create Snippet'))
|
|
await flush()
|
|
|
|
expect(lastMutationInput('admin.vendorSnippets.create').skuIds).toEqual([11, 12])
|
|
})
|
|
|
|
it('AVFORM-06: create submits the exact payload and reports success', async () => {
|
|
const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
const onClose = jest.fn()
|
|
const onSuccess = jest.fn()
|
|
const view = renderForm({ onClose, onSuccess })
|
|
await flush()
|
|
|
|
fireEvent.changeText(view.getByPlaceholderText('Enter snippet code'), 'MYCODE')
|
|
fireEvent.press(view.getByText('Choose a delivery slot'))
|
|
fireEvent.press(view.getByText(new Date(SLOT_1.deliveryTime).toLocaleString()))
|
|
await selectProduct(view, 'Chicken Breast - 500g')
|
|
fireEvent.press(view.getByText('Create Snippet'))
|
|
await flush()
|
|
|
|
expect(lastMutationInput('admin.vendorSnippets.create')).toEqual({
|
|
snippetCode: 'MYCODE',
|
|
slotId: 1849,
|
|
isPermanent: false,
|
|
skuIds: [11],
|
|
validTill: undefined,
|
|
})
|
|
expect(alertSpy).toHaveBeenCalledWith('Success', 'Vendor snippet created successfully')
|
|
expect(onSuccess).toHaveBeenCalledTimes(1)
|
|
expect(onClose).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('AVFORM-07: editing prefills the form and submits an update payload', async () => {
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
const onSuccess = jest.fn()
|
|
const view = renderForm({ snippet: SNIPPET_1 as any, onSuccess })
|
|
await flush()
|
|
|
|
expect(view.getByText('Edit Vendor Snippet')).toBeTruthy()
|
|
expect(view.getByDisplayValue('ABC123')).toBeTruthy()
|
|
// The existing SKU is shown as the current selection.
|
|
expect(view.getByText('Chicken Breast - 500g')).toBeTruthy()
|
|
expect(view.getByText(`Delivery Slot`)).toBeTruthy()
|
|
expect(
|
|
view.getByText(new Date(SLOT_1.deliveryTime).toLocaleString())
|
|
).toBeTruthy()
|
|
|
|
fireEvent.press(view.getByText('Update Snippet'))
|
|
await flush()
|
|
|
|
expect(lastMutationInput('admin.vendorSnippets.update')).toEqual({
|
|
id: 1,
|
|
updates: {
|
|
snippetCode: 'ABC123',
|
|
slotId: 1849,
|
|
isPermanent: false,
|
|
skuIds: [11],
|
|
validTill: SNIPPET_1.validTill,
|
|
},
|
|
})
|
|
expect(onSuccess).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('AVFORM-08: the close button calls onClose without saving', async () => {
|
|
const onClose = jest.fn()
|
|
const view = renderForm({ onClose })
|
|
|
|
fireEvent.press(view.getByText('✕'))
|
|
await flush()
|
|
|
|
expect(onClose).toHaveBeenCalledTimes(1)
|
|
expect(procedureMutationCalls('admin.vendorSnippets.create')).toHaveLength(0)
|
|
})
|
|
})
|