/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 19 — complaints (`tests/pages/complaints.test.tsx`, * CMP-01..18). * * The page reads `user.complaint.getAll` and the support contact block from * `common.essentialConsts`; links go through `expo-linking`'s `openURL`. * * The tRPC mock's `refetch` is a no-op, so a small wrapper around the mock * records refetch calls for the two rows that assert a refresh. */ import React from 'react' import { Linking, RefreshControl } from 'react-native' import { act, fireEvent } from '@testing-library/react-native' import Complaints from '@/app/(drawer)/(tabs)/me/complaints/index' import { createTestQueryClient, flush, renderWithProviders, seedRegion } from '../helpers/test-utils' import { resetAllMocks } from '../helpers/reset' import { setProcedure } from '../mocks/trpc-client' import { router } from '../mocks/expo-router-mock' import { CONSTS, REGION_1 } from '../fixtures' jest.mock('@/src/trpc-client', () => { const actual = jest.requireActual('../mocks/trpc-client') const state: Record = ((globalThis as any).__trpcRefetchProbe = {}) const wrap = (node: any, path: string): any => new Proxy( {}, { get(_target, prop) { if (typeof prop !== 'string') return undefined const next = path ? `${path}.${prop}` : prop const value = node[prop] if (prop === 'useQuery') { return (input?: any, options?: any) => { const result = value.call(node, input, options) return { ...result, refetch: (...args: any[]) => { state[path] = (state[path] ?? 0) + 1 return result.refetch(...args) }, } } } if (value == null) return value if (typeof value === 'function') return value return wrap(value, next) }, } ) return { ...actual, trpc: wrap(actual.trpc, ''), } }) /** Focus listeners registered through the (narrowed) navigation mock. */ const mockFocusCallbacks = new Set<() => void>() jest.mock('@react-navigation/native', () => { const React = require('react') const actual = jest.requireActual(__dirname + '/../mocks/react-navigation-mock') return { ...actual, useFocusEffect: (callback: () => void | (() => void)) => { React.useEffect(() => { mockFocusCallbacks.add(callback) const cleanup = callback() return () => { mockFocusCallbacks.delete(callback) if (typeof cleanup === 'function') cleanup() } // eslint-disable-next-line react-hooks/exhaustive-deps }, [callback]) }, } }) const COMPLAINTS_PATH = 'user.complaint.getAll' const DATE = new Date(2026, 2, 5, 14, 30, 0) function complaint(overrides: Record = {}) { return { id: 1, orderId: 'ORD-1001', complaintBody: 'Late delivery', complaintImages: [], response: null, isResolved: false, createdAt: DATE.toISOString(), ...overrides, } } const C1 = complaint() const C2 = complaint({ id: 2, orderId: null, complaintBody: 'App issue', response: 'We fixed it', isResolved: true, }) const C3 = complaint({ id: 3, orderId: null, complaintBody: 'Cold food', isResolved: false }) const C4 = complaint({ id: 4, orderId: null, complaintBody: 'Still waiting', response: 'Investigating', isResolved: false, }) function setComplaints(complaints: any[]) { setProcedure(COMPLAINTS_PATH, { data: { complaints } }) } function refetchCount(path = COMPLAINTS_PATH) { return ((globalThis as any).__trpcRefetchProbe?.[path] ?? 0) as number } function resetRefetchProbe() { const state = (globalThis as any).__trpcRefetchProbe if (state) Object.keys(state).forEach((key) => delete state[key]) } async function load() { await flush(2) } function renderPage(queryClient?: any) { return renderWithProviders(, { auth: false, queryClient: queryClient ?? createTestQueryClient(), }) } async function emitFocus() { await act(async () => { Array.from(mockFocusCallbacks).forEach((callback) => callback()) }) } describe('Complaints (CMP-01..CMP-18)', () => { beforeEach(() => { resetAllMocks() resetRefetchProbe() mockFocusCallbacks.clear() jest.spyOn(Linking, 'openURL').mockResolvedValue(true) }) it('CMP-01: shows loading copy while the complaints query is pending', () => { setProcedure(COMPLAINTS_PATH, { mode: 'pending' }) const { getByText } = renderPage() expect(getByText('Loading complaints...')).toBeTruthy() }) it('CMP-02: a failed query shows Oops! and the failure copy', () => { setProcedure(COMPLAINTS_PATH, { mode: 'error', error: new Error('offline') }) const { getByText } = renderPage() expect(getByText('Oops!')).toBeTruthy() expect(getByText('Failed to load complaints')).toBeTruthy() }) it('CMP-03: an empty list shows No Complaints and routes home', () => { setComplaints([]) const { getByText, getByTestId } = renderPage() expect(getByText('No Complaints')).toBeTruthy() fireEvent.press(getByTestId('complaints-continue-shopping')) expect(router.push).toHaveBeenCalledWith('/(drawer)/(tabs)/home') }) it('CMP-04: the header shows Need Help? with the support mobile and email', async () => { seedRegion(REGION_1, 7) setComplaints([C1]) const { getByText } = renderPage() await load() expect(getByText('Need Help?')).toBeTruthy() expect(getByText('8688182552')).toBeTruthy() expect(getByText('support@freshyo.in')).toBeTruthy() }) it('CMP-05: essential-consts support values override the defaults', async () => { seedRegion(REGION_1, 7) setProcedure('common.essentialConsts', { data: { ...CONSTS, supportMobile: '9999999999', supportEmail: 'help@freshyo.test' }, }) setComplaints([C1]) const { getByText } = renderPage() await load() expect(getByText('9999999999')).toBeTruthy() expect(getByText('help@freshyo.test')).toBeTruthy() }) it('CMP-06: tapping Call Us opens the tel: link', async () => { seedRegion(REGION_1, 7) setComplaints([C1]) const { getByTestId } = renderPage() await load() fireEvent.press(getByTestId('complaints-call-button')) expect(Linking.openURL).toHaveBeenCalledWith('tel:8688182552') }) it('CMP-07: tapping Email opens the mailto: link', async () => { seedRegion(REGION_1, 7) setComplaints([C1]) const { getByTestId } = renderPage() await load() fireEvent.press(getByTestId('complaints-email-button')) expect(Linking.openURL).toHaveBeenCalledWith('mailto:support@freshyo.in') }) it('CMP-08: an item without an order shows no order badge', () => { setComplaints([C2]) const { getByText, queryByText } = renderPage() expect(getByText('Complaint #2')).toBeTruthy() expect(queryByText(/Order #/)).toBeNull() }) it('CMP-09: an item with an order shows the Order # badge', () => { setComplaints([C1]) const { getByText } = renderPage() expect(getByText('Order #ORD-1001')).toBeTruthy() }) it('CMP-10: a resolved complaint shows the Resolved chip', () => { setComplaints([C2]) const { getByText } = renderPage() expect(getByText('Resolved')).toBeTruthy() }) it('CMP-11: a pending complaint shows Pending and the reviewing line', () => { setComplaints([C1]) const { getByText } = renderPage() expect(getByText('Pending')).toBeTruthy() expect(getByText('We are reviewing your complaint...')).toBeTruthy() }) it('CMP-12: a response renders the Support Response block', () => { setComplaints([C2]) const { getByText } = renderPage() expect(getByText('Support Response')).toBeTruthy() expect(getByText('We fixed it')).toBeTruthy() }) it('CMP-13: a pending complaint with a response hides the reviewing line', () => { setComplaints([C4]) const { getByText, queryByText } = renderPage() expect(getByText('Investigating')).toBeTruthy() expect(queryByText('We are reviewing your complaint...')).toBeNull() }) it('CMP-14: the created date uses MMM DD, YYYY • h:mm A', () => { setComplaints([C1]) const { getByText } = renderPage() expect(getByText('Mar 05, 2026 • 2:30 PM')).toBeTruthy() }) it('CMP-15: multiple complaints keep the API order', () => { setComplaints([C3, C1, C2]) const { getAllByText } = renderPage() expect( getAllByText(/Late delivery|App issue|Cold food/).map((node) => node.props.children) ).toEqual(['Cold food', 'Late delivery', 'App issue']) }) it('CMP-16: pull to refresh refetches the complaints query', async () => { setComplaints([C1]) const queryClient = createTestQueryClient() const clearSpy = jest.spyOn(queryClient, 'clear') const view = renderPage(queryClient) await load() const before = refetchCount() const [refreshControl] = view.UNSAFE_getAllByType(RefreshControl) await act(async () => { fireEvent(refreshControl, 'refresh') }) await flush(2) expect(clearSpy).toHaveBeenCalled() expect(refetchCount()).toBeGreaterThan(before) }) it('CMP-17: re-focusing through useMarkDataFetchers refetches complaints', async () => { setComplaints([C1]) renderPage() await load() const before = refetchCount() await emitFocus() await flush() expect(refetchCount()).toBeGreaterThan(before) }) it('CMP-18: the header still renders when the list is empty', () => { setComplaints([]) const { getByText } = renderPage() expect(getByText('Need Help?')).toBeTruthy() }) })