/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 24 — `OrderMenu` (OM-01..10). * * The menu opens a BottomDialog; the Edit Notes and Cancel Order rows open their * dialogs behind a 300ms `setTimeout`, so those cases run on fake timers and * advance them in `act`. Mutations are asserted through the procedure registry * (`lastProcedureInput`, `procedureMutationCalls`, `procedureInvalidations`). */ import * as React from 'react' import { ActivityIndicator, Alert, TouchableOpacity } from 'react-native' import { act, fireEvent, waitFor } from '@testing-library/react-native' import OrderMenu from '@/components/OrderMenu' import { renderWithProviders } from '../helpers/test-utils' import { resetAllMocks } from '../helpers/reset' import { lastProcedureInput, procedureInvalidations, procedureMutationCalls, setProcedure, } from '../mocks/trpc-client' import { router } from '../mocks/expo-router-mock' import { O1 } from '../fixtures' const ORDER_ID = O1.id let alertSpy: jest.SpyInstance function setup({ fakeTimers = false }: { fakeTimers?: boolean } = {}) { if (fakeTimers) jest.useFakeTimers() const postActionHandler = jest.fn() const utils = renderWithProviders( , { auth: false } ) return { ...utils, postActionHandler } } function advance(ms = 300) { act(() => { jest.advanceTimersByTime(ms) }) } /** * React Query batches observer notifications on a `setTimeout(0)`; with fake * timers the pending state is only visible after that timer runs. */ function settlePendingState() { advance(1) } type GetByTestId = (testId: string) => any function openNotesDialog(getByTestId: GetByTestId) { fireEvent.press(getByTestId('order-menu-trigger')) fireEvent.press(getByTestId('order-menu-edit-notes')) advance() } function openCancelDialog(getByTestId: GetByTestId) { fireEvent.press(getByTestId('order-menu-trigger')) fireEvent.press(getByTestId('order-menu-cancel-order')) advance() } describe('OrderMenu', () => { beforeEach(() => { resetAllMocks() alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined) }) afterEach(() => { alertSpy.mockRestore() }) it('OM-01: the trigger opens the Order Options menu', () => { const { getByTestId, getByText } = setup() fireEvent.press(getByTestId('order-menu-trigger')) expect(getByText('Order Options')).toBeTruthy() expect(getByText(`Select an action for Order #${ORDER_ID}`)).toBeTruthy() }) it('OM-02: View Details pushes the order detail route', () => { const { getByTestId } = setup() fireEvent.press(getByTestId('order-menu-trigger')) fireEvent.press(getByTestId('order-menu-view-details')) expect(router.push).toHaveBeenCalledWith( `/(drawer)/(tabs)/me/my-orders/${ORDER_ID}` ) }) it('OM-03: Edit Notes opens only after 300ms, with an empty input', () => { const { getByTestId, queryByText } = setup({ fakeTimers: true }) fireEvent.press(getByTestId('order-menu-trigger')) fireEvent.press(getByTestId('order-menu-edit-notes')) // The menu closes immediately; the notes dialog is still pending. expect(queryByText('Order Options')).toBeNull() expect(queryByText('Edit Instructions')).toBeNull() advance(299) expect(queryByText('Edit Instructions')).toBeNull() advance(1) expect(queryByText('Edit Instructions')).toBeTruthy() expect(getByTestId('order-menu-notes-input').props.value).toBe('') jest.useRealTimers() }) it('OM-04: saving notes sends the trimmed payload, alerts and runs the post action', async () => { const { getByTestId, postActionHandler } = setup({ fakeTimers: true }) openNotesDialog(getByTestId) jest.useRealTimers() fireEvent.changeText(getByTestId('order-menu-notes-input'), ' Leave at the door ') fireEvent.press(getByTestId('order-menu-save-notes')) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Success', 'Notes updated successfully') ) expect(lastProcedureInput('user.order.updateUserNotes')).toEqual({ id: ORDER_ID, userNotes: 'Leave at the door', }) expect(postActionHandler).toHaveBeenCalledTimes(1) }) it('OM-05: a failed notes save alerts and leaves the dialog open', async () => { setProcedure('user.order.updateUserNotes', { mode: 'error', error: new Error('nope') }) const { getByTestId, getByText } = setup({ fakeTimers: true }) openNotesDialog(getByTestId) jest.useRealTimers() fireEvent.changeText(getByTestId('order-menu-notes-input'), 'note') fireEvent.press(getByTestId('order-menu-save-notes')) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Error', 'nope')) expect(getByText('Edit Instructions')).toBeTruthy() }) it('OM-06: a blank cancel reason alerts without calling the API', () => { const { getByTestId, queryByText } = setup({ fakeTimers: true }) openCancelDialog(getByTestId) expect(queryByText('Confirm Cancellation')).toBeTruthy() fireEvent.press(getByTestId('order-menu-confirm-cancel')) expect(alertSpy).toHaveBeenCalledWith('Error', 'Please enter a reason for cancellation') expect(procedureMutationCalls('user.order.cancelOrder')).toHaveLength(0) jest.useRealTimers() }) it('OM-07: cancelling invalidates orders, alerts and runs the post action', async () => { const { getByTestId, postActionHandler } = setup({ fakeTimers: true }) openCancelDialog(getByTestId) jest.useRealTimers() fireEvent.changeText(getByTestId('order-menu-cancel-reason-input'), 'Ordered by mistake') fireEvent.press(getByTestId('order-menu-confirm-cancel')) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Success', 'Order cancelled successfully') ) expect(lastProcedureInput('user.order.cancelOrder')).toEqual({ id: ORDER_ID, reason: 'Ordered by mistake', }) expect(procedureInvalidations('user.order.getOrders')).toHaveLength(1) expect(postActionHandler).toHaveBeenCalledTimes(1) }) it('OM-08: a failed cancel alerts with the server message', async () => { setProcedure('user.order.cancelOrder', { mode: 'error', error: new Error('cancel boom'), }) const { getByTestId } = setup({ fakeTimers: true }) openCancelDialog(getByTestId) jest.useRealTimers() fireEvent.changeText(getByTestId('order-menu-cancel-reason-input'), 'reason') fireEvent.press(getByTestId('order-menu-confirm-cancel')) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('Error', 'cancel boom')) }) it('OM-09: closing the complaint form runs the post action', () => { const { getByTestId, postActionHandler, UNSAFE_getAllByType } = setup() fireEvent.press(getByTestId('order-menu-trigger')) fireEvent.press(getByTestId('order-menu-raise-complaint')) // The complaint BottomDialog renders last; its backdrop closes the form. const backdrops = UNSAFE_getAllByType(TouchableOpacity).filter( (node) => node.props.activeOpacity === 1 ) expect(backdrops.length).toBeGreaterThan(0) fireEvent.press(backdrops[backdrops.length - 1]) expect(postActionHandler).toHaveBeenCalledTimes(1) }) it('OM-10: pending mutations disable their buttons and show a spinner', () => { setProcedure('user.order.updateUserNotes', { mode: 'pending' }) setProcedure('user.order.cancelOrder', { mode: 'pending' }) const { getByTestId, queryByText, UNSAFE_getAllByType } = setup({ fakeTimers: true }) // Edit notes pending. openNotesDialog(getByTestId) fireEvent.changeText(getByTestId('order-menu-notes-input'), 'note') fireEvent.press(getByTestId('order-menu-save-notes')) settlePendingState() expect(queryByText('Save Instructions')).toBeNull() expect(getByTestId('order-menu-save-notes').props.accessibilityState.disabled).toBe(true) expect(UNSAFE_getAllByType(ActivityIndicator).length).toBeGreaterThan(0) // Cancel order pending. openCancelDialog(getByTestId) fireEvent.changeText(getByTestId('order-menu-cancel-reason-input'), 'reason') fireEvent.press(getByTestId('order-menu-confirm-cancel')) settlePendingState() expect(queryByText('Confirm Cancellation')).toBeNull() expect(getByTestId('order-menu-confirm-cancel').props.accessibilityState.disabled).toBe(true) jest.useRealTimers() }) })