freshyo/apps/admin-ui/tests/app/root-layout.test.tsx
2026-10-20 00:31:05 +05:30

103 lines
3.6 KiB
TypeScript

/* 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 } 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'
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(<Layout />, { auth: false, withRefresh: false, withAuth: false })
}
describe('Root layout (AROOT-01..05)', () => {
beforeEach(() => {
resetAllMocks()
__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(Notifications.removeNotificationSubscription).not.toHaveBeenCalled()
view.unmount()
expect(Notifications.removeNotificationSubscription).toHaveBeenCalledTimes(2)
})
})