137 lines
4.8 KiB
TypeScript
137 lines
4.8 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 13.2 — CancelOrderDialog (`tests/components/cancel-order-dialog.test.tsx`).
|
|
*/
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { act, fireEvent } from '@testing-library/react-native'
|
|
|
|
import CancelOrderDialog from '@/components/CancelOrderDialog'
|
|
import { renderWithProviders, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { setProcedure, lastMutationInput, procedureMutationCalls } from '../mocks/trpc-client'
|
|
|
|
function renderDialog(overrides: Partial<React.ComponentProps<typeof CancelOrderDialog>> = {}) {
|
|
const props = {
|
|
orderId: 11,
|
|
open: true,
|
|
onClose: jest.fn(),
|
|
onSuccess: jest.fn(),
|
|
...overrides,
|
|
}
|
|
const view = renderWithProviders(<CancelOrderDialog {...props} />, { auth: false })
|
|
return { view, props }
|
|
}
|
|
|
|
function pressAlertButton(alertSpy: jest.SpyInstance, label: string) {
|
|
const call = alertSpy.mock.calls[alertSpy.mock.calls.length - 1]
|
|
const buttons = (call?.[2] ?? []) as Array<{ text?: string; onPress?: () => void }>
|
|
const button = buttons.find((candidate) => candidate.text === label)
|
|
if (!button?.onPress) throw new Error(`Alert button "${label}" not found`)
|
|
return button.onPress()
|
|
}
|
|
|
|
const REASON_PLACEHOLDER = 'Enter reason for cancellation...'
|
|
|
|
describe('CancelOrderDialog (ACANCEL-01..06)', () => {
|
|
let alertSpy: jest.SpyInstance
|
|
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
|
|
})
|
|
|
|
afterEach(() => alertSpy?.mockRestore())
|
|
|
|
it('ACANCEL-01: renders the COD warning copy with no refund wording', () => {
|
|
const { view } = renderDialog()
|
|
|
|
expect(view.getAllByText('Cancel Order').length).toBeGreaterThanOrEqual(1)
|
|
expect(
|
|
view.getByText('This will cancel the order and mark it as cancelled by admin.'),
|
|
).toBeTruthy()
|
|
expect(view.queryByText(/refund/i)).toBeNull()
|
|
})
|
|
|
|
it('ACANCEL-02: an empty reason cannot be submitted and calls no API', () => {
|
|
const { view } = renderDialog()
|
|
|
|
const buttons = view.getAllByText('Cancel Order')
|
|
fireEvent.press(buttons[buttons.length - 1])
|
|
|
|
expect(procedureMutationCalls('admin.order.cancelOrder')).toHaveLength(0)
|
|
expect(alertSpy).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('ACANCEL-03: a valid reason confirms and calls cancelOrder with the exact payload', async () => {
|
|
const { view, props } = renderDialog()
|
|
|
|
fireEvent.changeText(view.getByPlaceholderText(REASON_PLACEHOLDER), 'Damaged item')
|
|
const buttons = view.getAllByText('Cancel Order')
|
|
fireEvent.press(buttons[buttons.length - 1])
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith(
|
|
'Cancel Order',
|
|
'Are you sure you want to cancel this order? This action cannot be undone.',
|
|
expect.any(Array),
|
|
)
|
|
|
|
await act(async () => {
|
|
pressAlertButton(alertSpy, 'Yes, Cancel')
|
|
})
|
|
await flush(2)
|
|
|
|
expect(lastMutationInput('admin.order.cancelOrder')).toEqual({
|
|
orderId: 11,
|
|
reason: 'Damaged item',
|
|
})
|
|
expect(props.onClose).toHaveBeenCalledTimes(1)
|
|
expect(props.onSuccess).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('ACANCEL-04: a failed cancellation shows the error alert', async () => {
|
|
setProcedure('admin.order.cancelOrder', { mode: 'error', error: new Error('boom') })
|
|
const { view, props } = renderDialog()
|
|
|
|
fireEvent.changeText(view.getByPlaceholderText(REASON_PLACEHOLDER), 'Damaged item')
|
|
const buttons = view.getAllByText('Cancel Order')
|
|
fireEvent.press(buttons[buttons.length - 1])
|
|
|
|
await act(async () => {
|
|
pressAlertButton(alertSpy, 'Yes, Cancel')
|
|
})
|
|
await flush(2)
|
|
|
|
expect(alertSpy).toHaveBeenCalledWith('Error', 'boom')
|
|
expect(props.onClose).not.toHaveBeenCalled()
|
|
expect(props.onSuccess).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('ACANCEL-05: while pending the label is "Cancelling..." and the action is disabled', async () => {
|
|
setProcedure('admin.order.cancelOrder', { mode: 'pending' })
|
|
const { view } = renderDialog()
|
|
|
|
fireEvent.changeText(view.getByPlaceholderText(REASON_PLACEHOLDER), 'Damaged item')
|
|
const buttons = view.getAllByText('Cancel Order')
|
|
fireEvent.press(buttons[buttons.length - 1])
|
|
|
|
await act(async () => {
|
|
pressAlertButton(alertSpy, 'Yes, Cancel')
|
|
})
|
|
await flush(2)
|
|
|
|
expect(view.getByText('Cancelling...')).toBeTruthy()
|
|
fireEvent.press(view.getByText('Cancelling...'))
|
|
await flush()
|
|
expect(procedureMutationCalls('admin.order.cancelOrder')).toHaveLength(1)
|
|
})
|
|
|
|
it('ACANCEL-06: Keep Order closes the dialog without cancelling', () => {
|
|
const { view, props } = renderDialog()
|
|
|
|
fireEvent.press(view.getByText('Keep Order'))
|
|
|
|
expect(props.onClose).toHaveBeenCalledTimes(1)
|
|
expect(procedureMutationCalls('admin.order.cancelOrder')).toHaveLength(0)
|
|
})
|
|
})
|