/** * UserIncidentsView (components/UserIncidentsView.tsx) — incident list, empty * state, add-incident dialog, validation and the addUserIncident mutation. * * HARNESS BUG WORKAROUND: tests/mocks/native.ts has a stray top-level `return` * (line 89 → "'return' outside of function"). Re-mock the expo modules that * `common-ui` eagerly imports locally. See report. */ jest.mock('expo-image', () => { const React = require('react') const { View } = require('react-native') const Image = (props: Record) => React.createElement(View, { testID: 'mock-image', ...props }) return { __esModule: true, Image, default: Image } }) jest.mock('expo-image-picker', () => { const asset = { uri: 'file:///tmp/pick.jpg', fileName: 'pick.jpg', mimeType: 'image/jpeg' } return { __esModule: true, MediaTypeOptions: { Images: 'Images', All: 'All', Videos: 'Videos' }, launchImageLibraryAsync: jest.fn(async () => ({ canceled: false, assets: [asset] })), launchCameraAsync: jest.fn(async () => ({ canceled: false, assets: [asset] })), requestMediaLibraryPermissionsAsync: jest.fn(async () => ({ granted: true, status: 'granted' })), requestCameraPermissionsAsync: jest.fn(async () => ({ granted: true, status: 'granted' })), } }) jest.mock('expo-document-picker', () => ({ __esModule: true, getDocumentAsync: jest.fn(async () => ({ canceled: false, assets: [{ uri: 'file:///tmp/doc.pdf', name: 'doc.pdf', mimeType: 'application/pdf' }], })), })) import React from 'react' import { Alert } from 'react-native' import { fireEvent, screen, waitFor } from '@testing-library/react-native' import { UserIncidentsView } from '@/components/UserIncidentsView' import { resetStorage, setupApiMocks } from '../mocks/api' import { expectProcedureCalled, lastProcedureInput, procCount } from '../mocks/procedures' import { resetStaffAuthMock } from '../mocks/staff-auth' import { renderWithProviders } from '../helpers/render' const incident = { id: 1, userId: 3, orderId: 7, dateAdded: '2026-01-01T10:00:00.000Z', adminComment: 'Abusive behaviour', addedBy: 'shafi', negativityScore: 5, orderStatus: 'cancelled', } const procedures = (overrides: Record = {}) => ({ 'admin.user.getUserIncidents': { incidents: [incident] }, 'admin.user.addUserIncident': { success: true, data: { id: 2 } }, ...overrides, }) const renderView = ( props: { userId: number; orderId: number | null } = { userId: 3, orderId: null }, overrides: Record = {} ) => { const { fetchMock } = setupApiMocks({ procedures: procedures(overrides) }) renderWithProviders() return { fetchMock } } const openDialog = async () => { fireEvent.press(screen.getByText('Add Incident')) await screen.findByText('Add User Incident') } const submit = () => { fireEvent.press(screen.getAllByText('Add Incident').pop()!) } describe('UserIncidentsView', () => { beforeEach(async () => { resetStaffAuthMock() await resetStorage() jest.spyOn(Alert, 'alert').mockImplementation(() => {}) }) it('renders the section header and add button', async () => { renderView() expect(screen.getByText('User Incidents')).toBeVisible() expect(screen.getByText('Add Incident')).toBeVisible() await screen.findByText('Abusive behaviour') }) it('queries incidents for the given user', async () => { const { fetchMock } = renderView() await waitFor(() => expectProcedureCalled(fetchMock, 'admin.user.getUserIncidents') ) expect(lastProcedureInput(fetchMock, 'admin.user.getUserIncidents')).toEqual({ userId: 3, }) }) it('does not query when there is no user id', () => { const { fetchMock } = renderView({ userId: 0, orderId: null }) expect(procCount(fetchMock, 'admin.user.getUserIncidents')).toBe(0) }) it('renders each incident with its score, author and order', async () => { renderView() expect(await screen.findByText('Abusive behaviour')).toBeVisible() expect(screen.getByText('Score: 5')).toBeVisible() expect(screen.getByText('Added by shafi')).toBeVisible() expect(screen.getByText('Order #7')).toBeVisible() expect(screen.getByText(/Jan 01, 2026/)).toBeVisible() }) it('renders the empty state when there are no incidents', async () => { renderView({ userId: 3, orderId: null }, { 'admin.user.getUserIncidents': { incidents: [] } }) expect( await screen.findByText('No incidents recorded for this user') ).toBeVisible() }) it('renders the empty state when the query returns null', async () => { renderView({ userId: 3, orderId: null }, { 'admin.user.getUserIncidents': null }) expect( await screen.findByText('No incidents recorded for this user') ).toBeVisible() }) it('rejects an empty incident without calling the mutation', async () => { const { fetchMock } = renderView() await openDialog() submit() expect(Alert.alert).toHaveBeenCalledWith( 'Error', 'Please enter a comment or negativity score' ) expect(procCount(fetchMock, 'admin.user.addUserIncident')).toBe(0) }) it('adds an incident with an admin comment', async () => { const { fetchMock } = renderView() await openDialog() fireEvent.changeText( screen.getByPlaceholderText('Enter details about the incident...'), 'Repeated cancellations' ) submit() await waitFor(() => expectProcedureCalled(fetchMock, 'admin.user.addUserIncident')) expect(lastProcedureInput(fetchMock, 'admin.user.addUserIncident')).toEqual({ userId: 3, adminComment: 'Repeated cancellations', }) await waitFor(() => expect(Alert.alert).toHaveBeenCalledWith('Success', 'Incident added successfully') ) }) it('adds an incident with only a negativity score', async () => { const { fetchMock } = renderView() await openDialog() fireEvent.changeText(screen.getByPlaceholderText('0'), '7') submit() await waitFor(() => expectProcedureCalled(fetchMock, 'admin.user.addUserIncident')) expect(lastProcedureInput(fetchMock, 'admin.user.addUserIncident')).toEqual({ userId: 3, negativityScore: 7, }) }) it('adds an incident with a comment and score including the order id', async () => { const { fetchMock } = renderView({ userId: 3, orderId: 7 }) await openDialog() fireEvent.changeText( screen.getByPlaceholderText('Enter details about the incident...'), 'Rude call' ) fireEvent.changeText(screen.getByPlaceholderText('0'), '3') submit() await waitFor(() => expectProcedureCalled(fetchMock, 'admin.user.addUserIncident')) expect(lastProcedureInput(fetchMock, 'admin.user.addUserIncident')).toEqual({ userId: 3, orderId: 7, adminComment: 'Rude call', negativityScore: 3, }) }) it('refetches the incidents and closes the dialog after adding', async () => { const { fetchMock } = renderView() await screen.findByText('Abusive behaviour') const before = procCount(fetchMock, 'admin.user.getUserIncidents') await openDialog() fireEvent.changeText( screen.getByPlaceholderText('Enter details about the incident...'), 'Late arrival' ) submit() await waitFor(() => expect(procCount(fetchMock, 'admin.user.getUserIncidents')).toBeGreaterThan(before) ) await waitFor(() => expect(screen.queryByText('Add User Incident')).toBeNull()) }) it('shows the error alert when adding fails and keeps the dialog open', async () => { const { fetchMock } = renderView( { userId: 3, orderId: null }, { 'admin.user.addUserIncident': () => { throw new Error('nope') }, } ) await openDialog() fireEvent.changeText( screen.getByPlaceholderText('Enter details about the incident...'), 'Something' ) submit() await waitFor(() => expect(Alert.alert).toHaveBeenCalledWith('Error', 'nope') ) expect(fetchMock).toBeTruthy() expect(screen.getByText('Add User Incident')).toBeVisible() }) })