freshyo/apps/user-ui/tests/pages/me-layout.test.tsx
2026-10-18 21:51:01 +05:30

74 lines
3 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Me layout rows — APP-20..APP-23 (`tests/app/layouts.test.tsx` also covers
* these) plus the signed-out gate row ME-20.
*
* The layout is rendered directly: the `expo-router` mock turns `Stack` into a
* pass-through, so the gate's loading / login-wall / stack branches can be
* asserted through the mocked `AuthContext`.
*/
import React from 'react'
import MeLayout from '@/app/(drawer)/(tabs)/me/_layout'
import MyOrdersLayout from '@/app/(drawer)/(tabs)/me/my-orders/_layout'
import { renderWithProviders } from '../helpers/test-utils'
import { resetAllMocks } from '../helpers/reset'
import { Stack } from '../mocks/expo-router-mock'
import { __setAuthState } from '../mocks/auth-context'
jest.mock('@/src/contexts/AuthContext', () => require('../mocks/auth-context'))
function authState(next: Record<string, any>) {
__setAuthState({ isLoading: false, isAuthenticated: false, user: null, ...next })
}
describe('Me layout (APP-20..APP-23 / ME-20)', () => {
beforeEach(() => resetAllMocks())
it('APP-20: while loading the me layout shows the loading copy', () => {
authState({ isLoading: true })
const view = renderWithProviders(<MeLayout />, { auth: false })
expect(view.getByText('Loading...')).toBeTruthy()
expect(view.UNSAFE_queryByType(Stack)).toBeNull()
})
it('APP-21: signed out shows the inline login wall', () => {
authState({ isLoading: false, isAuthenticated: false })
const view = renderWithProviders(<MeLayout />, { auth: false })
expect(view.getByTestId('login-wall')).toBeTruthy()
expect(view.getByText('Log in to view your profile, orders and more.')).toBeTruthy()
})
it('APP-22: signed in renders the stack', () => {
authState({ isLoading: false, isAuthenticated: true })
const view = renderWithProviders(<MeLayout />, { auth: false })
expect(view.UNSAFE_getByType(Stack)).toBeTruthy()
expect(view.queryByTestId('login-wall')).toBeNull()
})
it('APP-23: the my-orders stack carries the My Orders and Order Details titles', () => {
const view = renderWithProviders(<MyOrdersLayout />, { auth: 'in' })
const stack = view.UNSAFE_getByType(Stack)
const detail = view.UNSAFE_getAllByType(Stack.Screen).find(
(screen: any) => screen.props.name === '[id]'
)
expect(stack.props.screenOptions.title).toBe('My Orders')
expect(detail.props.options.title).toBe('Order Details')
})
it('ME-20: signed out the me layout gates the hub behind the login wall', () => {
authState({ isLoading: false, isAuthenticated: false })
const view = renderWithProviders(<MeLayout />, { auth: false })
expect(view.getByTestId('login-wall')).toBeTruthy()
expect(view.getByText('Log in to view your profile, orders and more.')).toBeTruthy()
// The hub never mounts: no stack (and therefore no Me screen) is rendered.
expect(view.UNSAFE_queryByType(Stack)).toBeNull()
expect(view.queryByText('My Account')).toBeNull()
})
})