freshyo/apps/admin-ui/tests/components/order-notes-form.test.tsx
2026-10-20 00:31:05 +05:30

89 lines
3.4 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Catalogue section 13.2 — OrderNotesForm (`tests/components/order-notes-form.test.tsx`).
*/
import React from 'react'
import { Alert } from 'react-native'
import { fireEvent } from '@testing-library/react-native'
import { OrderNotesForm } from '@/components/OrderNotesForm'
import { renderWithProviders, flush } from '../helpers/test-utils'
import { resetAllMocks } from '../helpers/reset'
import { setProcedure, lastMutationInput, procedureMutationCalls } from '../mocks/trpc-client'
describe('OrderNotesForm (AONF-01..05)', () => {
let alertSpy: jest.SpyInstance
beforeEach(() => {
resetAllMocks()
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
})
afterEach(() => alertSpy?.mockRestore())
it('AONF-01: renders an empty notes input', () => {
const view = renderWithProviders(<OrderNotesForm orderId={11} />, { auth: false })
expect(view.getByText('Add Order Notes')).toBeTruthy()
const input = view.getByPlaceholderText('Enter notes for this order...')
expect(input.props.value).toBe('')
})
it('AONF-02: saving calls updateNotes with the trimmed notes and reports success', async () => {
const onSuccess = jest.fn()
const view = renderWithProviders(<OrderNotesForm orderId={11} onSuccess={onSuccess} />, {
auth: false,
})
fireEvent.changeText(view.getByPlaceholderText('Enter notes for this order...'), ' Ring the bell ')
fireEvent.press(view.getByText('Save Notes'))
await flush(2)
expect(lastMutationInput('admin.order.updateNotes')).toEqual({
orderId: 11,
adminNotes: 'Ring the bell',
})
expect(alertSpy).toHaveBeenCalledWith('Success', 'Notes updated successfully')
expect(onSuccess).toHaveBeenCalledTimes(1)
})
it('AONF-03: a failed save shows the error alert and does not call onSuccess', async () => {
setProcedure('admin.order.updateNotes', { mode: 'error', error: new Error('boom') })
const onSuccess = jest.fn()
const view = renderWithProviders(<OrderNotesForm orderId={11} onSuccess={onSuccess} />, {
auth: false,
})
fireEvent.changeText(view.getByPlaceholderText('Enter notes for this order...'), 'note')
fireEvent.press(view.getByText('Save Notes'))
await flush(2)
expect(alertSpy).toHaveBeenCalledWith('Error', 'Failed to update notes')
expect(onSuccess).not.toHaveBeenCalled()
})
it('AONF-04: Cancel invokes onCancel without saving', () => {
const onCancel = jest.fn()
const view = renderWithProviders(<OrderNotesForm orderId={11} onCancel={onCancel} />, {
auth: false,
})
fireEvent.press(view.getByText('Cancel'))
expect(onCancel).toHaveBeenCalledTimes(1)
expect(procedureMutationCalls('admin.order.updateNotes')).toHaveLength(0)
})
it('AONF-05: while saving the button reads "Saving..." and is disabled', async () => {
setProcedure('admin.order.updateNotes', { mode: 'pending' })
const view = renderWithProviders(<OrderNotesForm orderId={11} />, { auth: false })
fireEvent.changeText(view.getByPlaceholderText('Enter notes for this order...'), 'note')
fireEvent.press(view.getByText('Save Notes'))
await flush(2)
expect(view.getByText('Saving...')).toBeTruthy()
expect(view.queryByText('Save Notes')).toBeNull()
expect(procedureMutationCalls('admin.order.updateNotes')).toHaveLength(1)
})
})