98 lines
3.3 KiB
TypeScript
98 lines
3.3 KiB
TypeScript
/**
|
|
* CancelOrderDialog — reason gating, the cancelOrder mutation and callbacks.
|
|
*/
|
|
|
|
import React from 'react'
|
|
import { Alert } from 'react-native'
|
|
import { act, fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
|
|
import CancelOrderDialog from '@/components/CancelOrderDialog'
|
|
import { setupApiMocks, resetStorage } from '../mocks/api'
|
|
import { resetStaffAuthMock } from '../mocks/staff-auth'
|
|
import { expectProcedureCalled, lastProcedureInput } from '../mocks/procedures'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
describe('CancelOrderDialog', () => {
|
|
const onClose = jest.fn()
|
|
const onSuccess = jest.fn()
|
|
|
|
beforeEach(async () => {
|
|
onClose.mockClear()
|
|
onSuccess.mockClear()
|
|
resetStaffAuthMock()
|
|
await resetStorage()
|
|
setupApiMocks({ procedures: { 'admin.order.cancelOrder': { success: true, message: 'ok' } } })
|
|
jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
|
})
|
|
|
|
it('renders the dialog chrome and reason input', () => {
|
|
renderWithProviders(
|
|
<CancelOrderDialog orderId={7} open onClose={onClose} onSuccess={onSuccess} />
|
|
)
|
|
|
|
expect(screen.getAllByText('Cancel Order')).toHaveLength(2)
|
|
expect(screen.getByText(/refund record will be created/)).toBeVisible()
|
|
expect(screen.getByPlaceholderText('Enter reason for cancellation...')).toBeVisible()
|
|
expect(screen.getByText('Keep Order')).toBeVisible()
|
|
})
|
|
|
|
it('does not submit and does not confirm when the reason is empty', () => {
|
|
const { fetchMock } = setupApiMocks({
|
|
procedures: { 'admin.order.cancelOrder': { success: true, message: 'ok' } },
|
|
})
|
|
|
|
renderWithProviders(
|
|
<CancelOrderDialog orderId={7} open onClose={onClose} onSuccess={onSuccess} />
|
|
)
|
|
|
|
const confirm = screen.getAllByText('Cancel Order').pop()!
|
|
fireEvent.press(confirm)
|
|
|
|
expect(Alert.alert).not.toHaveBeenCalled()
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('asks for confirmation then cancels with the typed reason', async () => {
|
|
const { fetchMock } = setupApiMocks({
|
|
procedures: { 'admin.order.cancelOrder': { success: true, message: 'cancelled' } },
|
|
})
|
|
|
|
renderWithProviders(
|
|
<CancelOrderDialog orderId={7} open onClose={onClose} onSuccess={onSuccess} />
|
|
)
|
|
|
|
fireEvent.changeText(
|
|
screen.getByPlaceholderText('Enter reason for cancellation...'),
|
|
'Out of stock'
|
|
)
|
|
fireEvent.press(screen.getAllByText('Cancel Order').pop()!)
|
|
|
|
const call = (Alert.alert as jest.Mock).mock.calls.find((c) => c[0] === 'Cancel Order')
|
|
expect(call).toBeTruthy()
|
|
const buttons = call![2] as { text: string; onPress?: () => void }[]
|
|
const yes = buttons.find((b) => b.text === 'Yes, Cancel')!
|
|
|
|
await act(async () => {
|
|
yes.onPress?.()
|
|
})
|
|
|
|
await waitFor(() => expectProcedureCalled(fetchMock, 'admin.order.cancelOrder'))
|
|
expect(lastProcedureInput(fetchMock, 'admin.order.cancelOrder')).toEqual({
|
|
orderId: 7,
|
|
reason: 'Out of stock',
|
|
})
|
|
expect(onClose).toHaveBeenCalled()
|
|
expect(onSuccess).toHaveBeenCalled()
|
|
})
|
|
|
|
it('keeps the order via the secondary button', () => {
|
|
renderWithProviders(
|
|
<CancelOrderDialog orderId={7} open onClose={onClose} onSuccess={onSuccess} />
|
|
)
|
|
|
|
fireEvent.press(screen.getByText('Keep Order'))
|
|
|
|
expect(onClose).toHaveBeenCalled()
|
|
expect(onSuccess).not.toHaveBeenCalled()
|
|
})
|
|
})
|