/** * Order detail (me/my-orders/[id].tsx) — order.getOrderById, notes edit, * cancel and the complaint action. */ jest.mock('expo-router', () => require('../mocks/router').expoRouterMock()) import React from 'react' import { Alert } from 'react-native' import { fireEvent, screen, waitFor } from '@testing-library/react-native' import OrderDetails from '@/app/(drawer)/(tabs)/me/my-orders/[id]' import { resetStorage, setupApiMocks } from '../mocks/api' import { expectProcedureCalled, lastProcedureInput } from '../mocks/procedures' import { routerMock, searchParamsMock } from '../mocks/router' import { renderWithProviders } from '../helpers/render' const ORDER = { id: 42, orderId: 'ORD-42', orderDate: '2026-09-20T10:00:00.000Z', createdAt: '2026-09-20T10:00:00.000Z', orderStatus: 'confirmed', deliveryStatus: 'pending', deliveryDate: '2026-09-26T10:00:00.000Z', cancelReason: null, paymentMode: 'COD', paymentStatus: 'pending', orderAmount: 500, discountAmount: 0, deliveryCharge: 15, couponCode: null, isFlashDelivery: false, userNotes: null, items: [ { productName: 'Chicken Breast 500 g', quantity: 1, unitNotation: '500g', price: 215, amount: 215, image: null, }, ], } const setup = (order: Record = ORDER) => setupApiMocks({ procedures: { 'user.order.getOrderById': order, 'user.order.updateUserNotes': { success: true }, 'user.order.cancelOrder': { success: true }, }, }) describe('Order detail', () => { beforeEach(async () => { await resetStorage() searchParamsMock.current = { id: '42' } jest.spyOn(Alert, 'alert').mockImplementation(() => {}) }) it('renders the order, its items and the total', async () => { setup() renderWithProviders(, { centralStores: false }) await waitFor(() => expect(screen.getByText('Order #ORD-42')).toBeVisible()) expect(screen.getByText('Chicken Breast 500 g')).toBeVisible() expect(screen.getByText('Total Amount')).toBeVisible() expect(screen.getByText('₹500')).toBeVisible() }) it('shows the error state and goes back', async () => { setupApiMocks({ procedures: { 'user.order.getOrderById': () => { throw new Error('down') }, }, }) renderWithProviders(, { centralStores: false }) await waitFor(() => expect(screen.getByText('Failed to load')).toBeVisible()) fireEvent.press(screen.getByText('Go Back')) expect(routerMock.back).toHaveBeenCalled() }) it('saves special instructions via user.order.updateUserNotes', async () => { const api = setup() renderWithProviders(, { centralStores: false }) await waitFor(() => expect(screen.getByText('Order #ORD-42')).toBeVisible()) fireEvent.press(screen.getByText('Add')) fireEvent.changeText( screen.getByPlaceholderText('Add special delivery instructions...'), 'Ring the bell' ) fireEvent.press(screen.getByText('Save')) await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.order.updateUserNotes') ) expect(lastProcedureInput(api.fetchMock, 'user.order.updateUserNotes')).toEqual({ id: ORDER.id, userNotes: 'Ring the bell', }) }) it('requires a reason before cancelling', async () => { const api = setup() renderWithProviders(, { centralStores: false }) await waitFor(() => expect(screen.getByText('Cancel Order')).toBeVisible()) fireEvent.press(screen.getByText('Cancel Order')) await waitFor(() => expect(screen.getByTestId('cancel-reason')).toBeVisible()) fireEvent.press(screen.getByText('Confirm Cancellation')) await waitFor(() => expect(Alert.alert).toHaveBeenCalledWith( 'Error', 'Please enter a reason for cancellation' ) ) expect( api.fetchMock.mock.calls.some(([u]) => String(u).includes('cancelOrder')) ).toBe(false) }) it('cancels the order with a reason via user.order.cancelOrder', async () => { const api = setup() renderWithProviders(, { centralStores: false }) await waitFor(() => expect(screen.getByText('Cancel Order')).toBeVisible()) fireEvent.press(screen.getByText('Cancel Order')) await waitFor(() => expect(screen.getByTestId('cancel-reason')).toBeVisible()) fireEvent.changeText(screen.getByTestId('cancel-reason'), 'Changed my mind') fireEvent.press(screen.getByText('Confirm Cancellation')) await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.order.cancelOrder')) expect(lastProcedureInput(api.fetchMock, 'user.order.cancelOrder')).toEqual({ id: ORDER.id, reason: 'Changed my mind', }) }) it('opens the complaint form', async () => { setup() renderWithProviders(, { centralStores: false }) await waitFor(() => expect(screen.getByText('Raise Complaint')).toBeVisible()) fireEvent.press(screen.getByText('Raise Complaint')) await waitFor(() => expect(screen.getByPlaceholderText('Tell us what went wrong...')).toBeVisible() ) }) })