freshyo/apps/user-ui/tests/pages/complaints.test.tsx
2026-10-20 11:19:25 +05:30

287 lines
8.8 KiB
TypeScript

/* 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 real tRPC client runs, so every `refetch()` sends a new `getAll`
* request; the two refresh rows count those recorded requests.
*/
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 { procedureQueryCalls, setProcedure } from '../mocks/trpc-client'
import { router } from '../mocks/expo-router-mock'
import { CONSTS, REGION_1 } from '../fixtures'
/** 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<string, any> = {}) {
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 procedureQueryCalls(path).length
}
async function load() {
await flush(2)
}
function renderPage(queryClient?: any) {
return renderWithProviders(<Complaints />, {
auth: false,
queryClient: queryClient ?? createTestQueryClient(),
})
}
/** Render and wait for the complaints query (and the header consts) to resolve. */
async function renderAndLoad(queryClient?: any) {
const view = renderPage(queryClient)
await load()
return view
}
async function emitFocus() {
await act(async () => {
Array.from(mockFocusCallbacks).forEach((callback) => callback())
})
}
describe('Complaints (CMP-01..CMP-18)', () => {
beforeEach(() => {
resetAllMocks()
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', async () => {
setProcedure(COMPLAINTS_PATH, { mode: 'error', error: new Error('offline') })
const view = await renderAndLoad()
expect(view.getByText('Oops!')).toBeTruthy()
expect(view.getByText('Failed to load complaints')).toBeTruthy()
})
it('CMP-03: an empty list shows No Complaints and routes home', async () => {
setComplaints([])
const view = await renderAndLoad()
expect(view.getByText('No Complaints')).toBeTruthy()
fireEvent.press(view.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 view = await renderAndLoad()
expect(view.getByText('Need Help?')).toBeTruthy()
// The support block reads `common.essentialConsts`, which resolves on its own tick.
expect(await view.findByText('8688182552')).toBeTruthy()
expect(await view.findByText('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 view = await renderAndLoad()
expect(await view.findByText('9999999999')).toBeTruthy()
expect(await view.findByText('help@freshyo.test')).toBeTruthy()
})
it('CMP-06: tapping Call Us opens the tel: link', async () => {
seedRegion(REGION_1, 7)
setComplaints([C1])
const view = await renderAndLoad()
await view.findByText('8688182552')
fireEvent.press(view.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 view = await renderAndLoad()
await view.findByText('support@freshyo.in')
fireEvent.press(view.getByTestId('complaints-email-button'))
expect(Linking.openURL).toHaveBeenCalledWith('mailto:support@freshyo.in')
})
it('CMP-08: an item without an order shows no order badge', async () => {
setComplaints([C2])
const view = await renderAndLoad()
expect(view.getByText('Complaint #2')).toBeTruthy()
expect(view.queryByText(/Order #/)).toBeNull()
})
it('CMP-09: an item with an order shows the Order # badge', async () => {
setComplaints([C1])
const view = await renderAndLoad()
expect(view.getByText('Order #ORD-1001')).toBeTruthy()
})
it('CMP-10: a resolved complaint shows the Resolved chip', async () => {
setComplaints([C2])
const view = await renderAndLoad()
expect(view.getByText('Resolved')).toBeTruthy()
})
it('CMP-11: a pending complaint shows Pending and the reviewing line', async () => {
setComplaints([C1])
const view = await renderAndLoad()
expect(view.getByText('Pending')).toBeTruthy()
expect(view.getByText('We are reviewing your complaint...')).toBeTruthy()
})
it('CMP-12: a response renders the Support Response block', async () => {
setComplaints([C2])
const view = await renderAndLoad()
expect(view.getByText('Support Response')).toBeTruthy()
expect(view.getByText('We fixed it')).toBeTruthy()
})
it('CMP-13: a pending complaint with a response hides the reviewing line', async () => {
setComplaints([C4])
const view = await renderAndLoad()
expect(view.getByText('Investigating')).toBeTruthy()
expect(view.queryByText('We are reviewing your complaint...')).toBeNull()
})
it('CMP-14: the created date uses MMM DD, YYYY • h:mm A', async () => {
setComplaints([C1])
const view = await renderAndLoad()
expect(view.getByText('Mar 05, 2026 • 2:30 PM')).toBeTruthy()
})
it('CMP-15: multiple complaints keep the API order', async () => {
setComplaints([C3, C1, C2])
const view = await renderAndLoad()
expect(
view.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', async () => {
setComplaints([])
const view = await renderAndLoad()
expect(view.getByText('Need Help?')).toBeTruthy()
})
})