/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 3.1 — Root layout (`app/_layout.tsx`). * * The layout provides its own QueryClient/trpc/StaffAuth/Notification tree; we * mount it with the shared render helper (auth seeding is irrelevant here) so * the page can be rendered exactly as the app boots it. */ import React from 'react' import { LogBox } from 'react-native' import { act, waitFor } from '@testing-library/react-native' import * as Notifications from 'expo-notifications' import Toast from 'react-native-toast-message' import Layout from '@/app/_layout' import { renderWithProviders, flush } from '../helpers/test-utils' import { resetAllMocks } from '../helpers/reset' import { router } from '../mocks/expo-router-mock' import { STAFF } from '../fixtures' import { clearPendingHref, takePendingHref, } from '@/services/notif-service/pending-navigation' const notificationsMock = require('../mocks/expo-notifications-mock') jest.mock('@/components/context/staff-auth-context', () => require('../mocks/staff-auth-context')) // eslint-disable-next-line import/first import { __setStaffState } from '../mocks/staff-auth-context' function renderLayout() { return renderWithProviders(, { auth: false, withRefresh: false, withAuth: false }) } describe('Root layout (AROOT-01..09)', () => { beforeEach(() => { resetAllMocks() clearPendingHref() __setStaffState({ isLoading: false, isLoggedIn: true, staff: STAFF }) }) it('AROOT-01: renders the app root inside the provider tree', () => { const view = renderLayout() expect(view.getByTestId('app-root')).toBeTruthy() // The tRPC provider is mounted by the layout (its hooks resolve through the // mocked client), and the notification provider bootstraps permission. expect(notificationsMock.getPermissionsAsync).toHaveBeenCalled() }) it('AROOT-02: renders children via Stack inside RefreshProvider', async () => { const view = renderLayout() await flush() // Stack is mocked to render its children; an empty stack still leaves the // root safe-area view mounted. expect(view.getByTestId('app-root')).toBeTruthy() }) it('AROOT-03: mounts NotifChecker and the Toast host', async () => { const view = renderLayout() await flush() expect(view.getByTestId('toast-host')).toBeTruthy() // A received notification is routed through the mounted context to the toast. act(() => { notificationsMock.__emitNotification({ request: { content: { title: 'Heads up', body: 'New order', data: {} } }, }) }) expect(Toast.show).toHaveBeenCalledWith( expect.objectContaining({ type: 'info', text1: 'Heads up', text2: 'New order' }) ) // Tapping a notification response navigates through the mounted router. act(() => { notificationsMock.__emitResponse({ notification: { request: { content: { data: { orderId: 11 } } } }, }) }) expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/manage-orders/order-details/11') }) it('AROOT-04: ignores LogBox output in development', () => { const ignoreSpy = jest.spyOn(LogBox, 'ignoreAllLogs') jest.isolateModules(() => { // Re-evaluate the layout module so its module-level guard runs again. // eslint-disable-next-line @typescript-eslint/no-var-requires require('@/app/_layout') }) expect(ignoreSpy).toHaveBeenCalledWith(true) ignoreSpy.mockRestore() }) it('AROOT-05: removes both notification listeners on unmount', async () => { const view = renderLayout() await flush() expect(notificationsMock.__getListenerCounts()).toEqual({ received: 1, response: 1 }) view.unmount() expect(notificationsMock.__getListenerCounts()).toEqual({ received: 0, response: 0 }) }) it('AROOT-06: a tap that launched the app opens that order', async () => { notificationsMock.__setLastResponse({ notification: { request: { identifier: 'cold-1', content: { data: { orderId: 42 } } }, }, }) renderLayout() await flush() await waitFor(() => expect(router.push).toHaveBeenCalledWith( '/(drawer)/dashboard/manage-orders/order-details/42' ) ) expect(notificationsMock.clearLastNotificationResponseAsync).toHaveBeenCalled() }) it('AROOT-07: the launch response and a live delivery open the order once', async () => { const response = { notification: { request: { identifier: 'dup-1', content: { data: { orderId: 7 } } }, }, } notificationsMock.__setLastResponse(response) renderLayout() await flush() act(() => { notificationsMock.__emitResponse(response) }) await flush() expect(router.push).toHaveBeenCalledTimes(1) expect(router.push).toHaveBeenCalledWith( '/(drawer)/dashboard/manage-orders/order-details/7' ) }) it('AROOT-08: a launch tap while signed out waits for login', async () => { __setStaffState({ isLoading: false, isLoggedIn: false, staff: null }) notificationsMock.__setLastResponse({ notification: { request: { identifier: 'cold-2', content: { data: { orderId: 9 } } }, }, }) renderLayout() await flush() expect(router.push).not.toHaveBeenCalled() expect(takePendingHref()).toBe( '/(drawer)/dashboard/manage-orders/order-details/9' ) }) it('AROOT-09: a complaint push opens the complaints list', async () => { notificationsMock.__setLastResponse({ notification: { request: { identifier: 'complaint-1', content: { data: { complaintId: 5 } } }, }, }) renderLayout() await flush() await waitFor(() => expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/complaints') ) }) })