/** * OrderOptionsMenu — action rows, responder toggles, location attach and * WhatsApp/Dial/Maps intents. */ import React from 'react' import { Alert, Linking } from 'react-native' import { fireEvent, screen, waitFor } from '@testing-library/react-native' import Ionicons from '@expo/vector-icons/Ionicons' import * as Location from 'expo-location' import { OrderOptionsMenu } from '@/components/OrderOptionsMenu' import { setupApiMocks, resetStorage } from '../mocks/api' import { resetStaffAuthMock } from '../mocks/staff-auth' import { expectProcedureCalled, lastProcedureInput, procCount } from '../mocks/procedures' import { renderWithProviders } from '../helpers/render' type OrderProp = React.ComponentProps['order'] const baseOrder: OrderProp = { id: 11, readableId: 11, isPackaged: false, isDelivered: false, address: '12 Main St, City, State - 500001, Phone: 9876543210', addressId: 3, adminNotes: null, latitude: null, longitude: null, status: 'pending', } function renderMenu(overrides: Partial = {}, handlers: Record = {}) { const props = { open: true, onClose: jest.fn(), order: { ...baseOrder, ...overrides }, onViewDetails: jest.fn(), onTogglePackaged: jest.fn(), onToggleDelivered: jest.fn(), onOpenAdminNotes: jest.fn(), onCancelOrder: jest.fn(), onAttachLocation: jest.fn(), onWhatsApp: jest.fn(), onDial: jest.fn(), ...handlers, } renderWithProviders() return props } describe('OrderOptionsMenu', () => { beforeEach(async () => { resetStaffAuthMock() await resetStorage() setupApiMocks({ procedures: { 'admin.order.updateAddressCoords': { success: true } } }) jest.spyOn(Alert, 'alert').mockImplementation(() => {}) jest.spyOn(Linking, 'openURL').mockResolvedValue(true) }) it('renders the header and action rows', () => { renderMenu() expect(screen.getByText('Order #11')).toBeVisible() expect(screen.getByText('Select an action to perform')).toBeVisible() expect(screen.getByText('View Details')).toBeVisible() expect(screen.getByText('Packaged')).toBeVisible() expect(screen.getByText('Delivered')).toBeVisible() expect(screen.getByText('Admin Notes')).toBeVisible() expect(screen.getByText('Cancel Order')).toBeVisible() expect(screen.getByText('Attach Location')).toBeVisible() expect(screen.getByText('Message On WhatsApp')).toBeVisible() expect(screen.getByText('Dial Mobile Number')).toBeVisible() }) it('describes packaged/delivered state in the row subtitles', () => { renderMenu({ isPackaged: true, isDelivered: true }) expect(screen.getByText('Mark as not packaged')).toBeVisible() expect(screen.getByText('Mark as not delivered')).toBeVisible() }) it('hides cancel when the order is already cancelled', () => { renderMenu({ status: 'cancelled' }) expect(screen.queryByText('Cancel Order')).toBeNull() }) it('invokes view details and closes', () => { const props = renderMenu() fireEvent.press(screen.getByText('View Details')) expect(props.onViewDetails).toHaveBeenCalled() expect(props.onClose).toHaveBeenCalled() }) it('invokes admin notes and closes', () => { const props = renderMenu() fireEvent.press(screen.getByText('Admin Notes')) expect(props.onOpenAdminNotes).toHaveBeenCalled() expect(props.onClose).toHaveBeenCalled() }) it('invokes cancel then closes', () => { const props = renderMenu() fireEvent.press(screen.getByText('Cancel Order')) expect(props.onCancelOrder).toHaveBeenCalled() expect(props.onClose).toHaveBeenCalled() }) it('toggles packaged through the responder', () => { const props = renderMenu({ isPackaged: false, isDelivered: true }) const icon = screen .UNSAFE_getAllByType(Ionicons) .find((node) => node.props.name === 'square-outline')! fireEvent(icon, 'responderEnd', { stopPropagation: jest.fn() }) expect(props.onTogglePackaged).toHaveBeenCalled() expect(props.onToggleDelivered).not.toHaveBeenCalled() }) it('toggles delivered through the responder', () => { const props = renderMenu({ isPackaged: true, isDelivered: false }) const icon = screen .UNSAFE_getAllByType(Ionicons) .find((node) => node.props.name === 'square-outline')! fireEvent(icon, 'responderEnd', { stopPropagation: jest.fn() }) expect(props.onToggleDelivered).toHaveBeenCalled() expect(props.onTogglePackaged).not.toHaveBeenCalled() }) it('attaches location coordinates and reports success', async () => { const { fetchMock } = setupApiMocks({ procedures: { 'admin.order.updateAddressCoords': { success: true } }, }) const props = renderMenu() fireEvent.press(screen.getByText('Attach Location')) await waitFor(() => expectProcedureCalled(fetchMock, 'admin.order.updateAddressCoords')) expect(lastProcedureInput(fetchMock, 'admin.order.updateAddressCoords')).toEqual({ addressId: 3, latitude: 16.75, longitude: 78.0, }) await waitFor(() => expect(Alert.alert).toHaveBeenCalledWith( 'Success', 'Location attached to address successfully.' ) ) expect(props.onAttachLocation).toHaveBeenCalled() }) it('warns when location permission is denied and skips the mutation', async () => { const { fetchMock } = setupApiMocks({ procedures: { 'admin.order.updateAddressCoords': { success: true } }, }) ;(Location.requestForegroundPermissionsAsync as jest.Mock).mockResolvedValueOnce({ status: 'denied', granted: false, }) renderMenu() fireEvent.press(screen.getByText('Attach Location')) await waitFor(() => expect(Alert.alert).toHaveBeenCalledWith( 'Permission Denied', 'Location permission is required to attach coordinates.' ) ) expect(procCount(fetchMock, 'admin.order.updateAddressCoords')).toBe(0) }) it('opens the coordinates in Google Maps only when present', () => { const props = renderMenu({ latitude: 16.75, longitude: 78.0 }) fireEvent.press(screen.getByText('Open in Maps')) expect(Linking.openURL).toHaveBeenCalledWith( 'https://www.google.com/maps/search/?api=1&query=16.75,78' ) expect(props.onClose).toHaveBeenCalled() }) it('alerts instead of opening maps when coordinates are missing', () => { renderMenu({ latitude: null, longitude: null }) expect(screen.queryByText('Open in Maps')).toBeNull() }) it('sends WhatsApp to the phone parsed from the address', () => { renderMenu() fireEvent.press(screen.getByText('Message On WhatsApp')) expect(Linking.openURL).toHaveBeenCalledWith('whatsapp://send?phone=+919876543210') }) it('shows the welcome message row only for a first order and opens WhatsApp', () => { renderMenu({ isFirstOrder: true }) expect(screen.getByText('Send Welcome Msg')).toBeVisible() fireEvent.press(screen.getByText('Send Welcome Msg')) expect(Linking.openURL).toHaveBeenCalledWith('whatsapp://send?phone=+919876543210') }) it('hides the welcome message row for repeat orders', () => { renderMenu({ isFirstOrder: false }) expect(screen.queryByText('Send Welcome Msg')).toBeNull() }) it('prefills the welcome message when opening the first-order WhatsApp', async () => { setupApiMocks({ procedures: { 'admin.order.updateAddressCoords': { success: true }, 'admin.const.getConstants': [{ key: 'welcomeMessage', value: 'Hello & welcome!' }], }, }) renderMenu({ isFirstOrder: true }) // The constant loads async; retry until the handler sees it. await waitFor(() => { fireEvent.press(screen.getByText('Send Welcome Msg')) expect(Linking.openURL).toHaveBeenCalledWith( 'whatsapp://send?phone=+919876543210&text=Hello%20%26%20welcome!' ) }) }) it('dials the phone parsed from the address', () => { renderMenu() fireEvent.press(screen.getByText('Dial Mobile Number')) expect(Linking.openURL).toHaveBeenCalledWith('tel:9876543210') }) it('alerts when the address has no phone number', () => { renderMenu({ address: '12 Main St, City' }) fireEvent.press(screen.getByText('Message On WhatsApp')) expect(Alert.alert).toHaveBeenCalledWith('No phone number found') expect(Linking.openURL).not.toHaveBeenCalled() }) })