freshyo/apps/admin-ui/tests/components/order-options-menu.test.tsx
2026-10-20 19:15:58 +05:30

206 lines
7.2 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Catalogue section 13.2 — OrderOptionsMenu (`tests/components/order-options-menu.test.tsx`).
*/
import React from 'react'
import { Alert, Modal } from 'react-native'
import { fireEvent } from '@testing-library/react-native'
jest.mock('expo-location', () => ({
requestForegroundPermissionsAsync: jest.fn(async () => ({ status: 'granted' })),
getCurrentPositionAsync: jest.fn(async () => ({
coords: { latitude: 12.34, longitude: 56.78 },
})),
Accuracy: { High: 6 },
}))
import { OrderOptionsMenu } from '@/components/OrderOptionsMenu'
import { renderWithProviders, flush } from '../helpers/test-utils'
import { resetAllMocks } from '../helpers/reset'
import { setProcedure, lastMutationInput } from '../mocks/trpc-client'
const ORDER = {
id: 11,
readableId: 101,
isPackaged: false,
isDelivered: false,
isFlashDelivery: false,
address: 'Home, 12 Main Road, Phone: 9876543210',
addressId: 11,
adminNotes: null as string | null,
userNotes: null as string | null,
latitude: 17,
longitude: 78,
status: 'pending',
isFirstOrder: false,
}
function renderMenu(overrides: Record<string, any> = {}) {
const props = {
open: true,
onClose: jest.fn(),
order: ORDER,
onViewDetails: jest.fn(),
onTogglePackaged: jest.fn(),
onToggleDelivered: jest.fn(),
onOpenAdminNotes: jest.fn(),
onCancelOrder: jest.fn(),
onAttachLocation: jest.fn(),
onWhatsApp: jest.fn(),
onDial: jest.fn(),
...overrides,
}
const view = renderWithProviders(<OrderOptionsMenu {...(props as any)} />, { auth: false })
return { view, props }
}
describe('OrderOptionsMenu (AOPT-01..07, AOO-01..03)', () => {
let alertSpy: jest.SpyInstance
beforeEach(() => {
resetAllMocks()
// The menu expects key/value rows from getRegionConstants (backend shape).
setProcedure('admin.region.getRegionConstants', {
data: [{ key: 'welcomeMessage', value: 'Welcome' }],
})
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined)
})
afterEach(() => alertSpy?.mockRestore())
it('AOPT-01: renders the order actions (current labels)', () => {
const { view } = renderMenu()
expect(view.getByText('Order #101')).toBeTruthy()
expect(view.getByText('Select an action to perform')).toBeTruthy()
expect(view.getByText('View Details')).toBeTruthy()
expect(view.getByText('Packaged')).toBeTruthy()
expect(view.getByText('Delivered')).toBeTruthy()
expect(view.getByText('Admin Notes')).toBeTruthy()
expect(view.getByText('Cancel Order')).toBeTruthy()
// Current menu has no complaint/“Edit Notes” entry.
expect(view.queryByText('Raise Complaint')).toBeNull()
expect(view.queryByText('Edit Notes')).toBeNull()
})
it('AOPT-02: View Details invokes onViewDetails and closes the menu', () => {
const { view, props } = renderMenu()
fireEvent.press(view.getByTestId('order-menu-view-details'))
expect(props.onViewDetails).toHaveBeenCalledTimes(1)
expect(props.onClose).toHaveBeenCalledTimes(1)
})
it('AOPT-03: Admin Notes invokes the notes callback and closes the menu', () => {
const { view, props } = renderMenu()
fireEvent.press(view.getByTestId('order-menu-admin-notes'))
expect(props.onOpenAdminNotes).toHaveBeenCalledTimes(1)
expect(props.onClose).toHaveBeenCalledTimes(1)
})
it('AOPT-04: there is no Raise Complaint action (current gap)', () => {
const { view } = renderMenu()
// The catalogue expects a complaint entry; today the menu only exposes the
// actions listed in AOPT-01, so there is no parent callback to trigger.
expect(view.queryByText('Raise Complaint')).toBeNull()
expect(view.getByTestId('order-menu-packaged')).toBeTruthy()
expect(view.getByTestId('order-menu-delivered')).toBeTruthy()
})
it('AOPT-05: Cancel Order invokes the cancel callback and closes the menu', () => {
const { view, props } = renderMenu()
fireEvent.press(view.getByTestId('order-menu-cancel'))
expect(props.onCancelOrder).toHaveBeenCalledTimes(1)
expect(props.onClose).toHaveBeenCalledTimes(1)
})
it('AOPT-06: a pending coordinate update disables Attach Location', async () => {
setProcedure('admin.order.updateAddressCoords', { mode: 'pending' })
const { view } = renderMenu()
fireEvent.press(view.getByTestId('order-menu-attach-location'))
await flush(2)
expect(
view.getByTestId('order-menu-attach-location').props.accessibilityState?.disabled,
).toBe(true)
})
it('AOPT-07: dismissing the menu invokes onClose (complaint close path absent)', () => {
const { view, props } = renderMenu()
fireEvent(view.UNSAFE_getByType(Modal), 'requestClose')
expect(props.onClose).toHaveBeenCalledTimes(1)
expect(lastMutationInput('admin.order.updateAddressCoords')).toBeUndefined()
})
/* ---------------------------------------------------------------------- */
/* Catalogue §17.1 — shared OrderOptionsMenu aliases */
/* ---------------------------------------------------------------------- */
it('AOO-01: renders the action rows', () => {
const { view } = renderMenu()
expect(view.getByText('View Details')).toBeTruthy()
expect(view.getByText('Packaged')).toBeTruthy()
expect(view.getByText('Delivered')).toBeTruthy()
expect(view.getByText('Admin Notes')).toBeTruthy()
expect(view.getByText('Cancel Order')).toBeTruthy()
expect(view.getByText('Attach Location')).toBeTruthy()
expect(view.getByText('Message On WhatsApp')).toBeTruthy()
expect(view.getByText('Dial Mobile Number')).toBeTruthy()
})
it('AOO-02: callback props are invoked by the rows', () => {
const { view, props } = renderMenu()
fireEvent.press(view.getByTestId('order-menu-view-details'))
fireEvent(view.getByTestId('order-menu-packaged'), 'responderEnd', {
stopPropagation: jest.fn(),
})
fireEvent(view.getByTestId('order-menu-delivered'), 'responderEnd', {
stopPropagation: jest.fn(),
})
fireEvent.press(view.getByTestId('order-menu-admin-notes'))
fireEvent.press(view.getByTestId('order-menu-cancel'))
expect(props.onViewDetails).toHaveBeenCalledTimes(1)
expect(props.onTogglePackaged).toHaveBeenCalledTimes(1)
expect(props.onToggleDelivered).toHaveBeenCalledTimes(1)
expect(props.onOpenAdminNotes).toHaveBeenCalledTimes(1)
expect(props.onCancelOrder).toHaveBeenCalledTimes(1)
})
it('AOO-03: a pending operation disables its row', async () => {
setProcedure('admin.order.updateAddressCoords', { mode: 'pending' })
const { view } = renderMenu()
fireEvent.press(view.getByTestId('order-menu-attach-location'))
await flush(2)
expect(
view.getByTestId('order-menu-attach-location').props.accessibilityState?.disabled,
).toBe(true)
})
it('shows Open in Maps when real coordinates are present', () => {
const { view } = renderMenu()
expect(view.getByText('Open in Maps')).toBeTruthy()
})
it('hides Open in Maps when the backend omits coordinates (seeded address)', () => {
const { view } = renderMenu({
order: { ...ORDER, latitude: null, longitude: null },
})
expect(view.queryByText('Open in Maps')).toBeNull()
})
})