freshyo/apps/admin-ui/tests/screens/drawer-layout.test.tsx
2026-10-01 00:33:03 +05:30

181 lines
No EOL
5.5 KiB
TypeScript

/**
* Drawer layout (app/(drawer)/_layout.tsx) — auth gate, the custom drawer
* content (staff name, nav items, logout) and the header buttons.
*
* expo-router/drawer and @react-navigation/drawer are stubbed so the custom
* drawer content and the screenOptions header callbacks are reachable.
*/
const mockRedirects: string[] = []
jest.mock('expo-router', () => {
const React = require('react')
const { Text } = require('react-native')
const base = require('../mocks/router').expoRouterMock()
return {
...base,
Redirect: ({ href }: { href: string }) => {
mockRedirects.push(href)
return React.createElement(Text, { testID: 'redirect' }, String(href))
},
}
})
const mockNavigation = {
getState: () => ({ routes: [], index: 0 }),
openDrawer: jest.fn(),
}
jest.mock('expo-router/drawer', () => {
const React = require('react')
const { View } = require('react-native')
const Drawer = ({
drawerContent: DrawerContent,
screenOptions,
children,
}: {
drawerContent?: React.ComponentType
screenOptions?: (args: { navigation: unknown }) => Record<string, any>
children?: React.ReactNode
}) => {
const options =
typeof screenOptions === 'function'
? screenOptions({ navigation: mockNavigation })
: {}
return React.createElement(
View,
null,
options.headerLeft ? options.headerLeft() : null,
options.headerRight ? options.headerRight() : null,
DrawerContent ? React.createElement(DrawerContent) : null,
children
)
}
Drawer.Screen = () => null
return { Drawer }
})
jest.mock('@react-navigation/drawer', () => {
const React = require('react')
const { View, TouchableOpacity, Text } = require('react-native')
return {
DrawerContentScrollView: ({ children }: { children?: React.ReactNode }) =>
React.createElement(View, null, children),
DrawerItem: ({
label,
onPress,
}: {
label: string
onPress?: () => void
}) =>
React.createElement(
TouchableOpacity,
{ onPress, accessibilityLabel: label },
React.createElement(Text, null, label)
),
}
})
import React from 'react'
import { ActivityIndicator, DeviceEventEmitter } from 'react-native'
import MaterialIcons from '@expo/vector-icons/MaterialIcons'
import { fireEvent, screen } from '@testing-library/react-native'
import { REFRESH_EVENT } from 'common-ui/src/lib/const-strs'
import Layout from '@/app/(drawer)/_layout'
import { resetStorage, setupApiMocks } from '../mocks/api'
import { resetStaffAuthMock, staffAuthMock } from '../mocks/staff-auth'
import { resetRouterMock, routerMock } from '../mocks/router'
import { renderWithProviders } from '../helpers/render'
const iconByName = (name: string) =>
screen.UNSAFE_getAllByType(MaterialIcons).find((i) => i.props.name === name) as
| Parameters<typeof fireEvent.press>[0]
| undefined
describe('Drawer layout', () => {
beforeEach(async () => {
resetStaffAuthMock()
mockRedirects.length = 0
mockNavigation.openDrawer.mockClear()
resetRouterMock()
await resetStorage()
setupApiMocks()
jest.spyOn(DeviceEventEmitter, 'emit')
})
it('shows a spinner while the session is loading', () => {
staffAuthMock.isLoading = true
renderWithProviders(<Layout />)
expect(screen.UNSAFE_getByType(ActivityIndicator)).toBeTruthy()
expect(screen.queryByText('Logout')).toBeNull()
})
it('redirects to login when logged out', () => {
staffAuthMock.isLoggedIn = false
staffAuthMock.isLoading = false
renderWithProviders(<Layout />)
expect(screen.getByTestId('redirect')).toHaveTextContent('/login')
expect(mockRedirects).toEqual(['/login'])
})
it('renders the staff name and navigation items', () => {
renderWithProviders(<Layout />)
expect(screen.getByText('shafi')).toBeVisible()
expect(screen.getByText('Staff Member')).toBeVisible()
expect(screen.getByText('Dashboard')).toBeVisible()
expect(screen.getByText('Products')).toBeVisible()
expect(screen.getByText('Prices Overview')).toBeVisible()
expect(screen.getByText('Product Groupings')).toBeVisible()
expect(screen.getByText('Manage Orders')).toBeVisible()
expect(screen.getByText('User Management')).toBeVisible()
expect(screen.getByText('Send Notifications')).toBeVisible()
expect(screen.getByText('Logout')).toBeVisible()
})
it('navigates from a drawer item', () => {
renderWithProviders(<Layout />)
fireEvent.press(screen.getByText('Prices Overview'))
expect(routerMock.push).toHaveBeenCalledWith(
'/(drawer)/dashboard/prices-overview'
)
})
it('dismisses to the dashboard from the Dashboard item', () => {
renderWithProviders(<Layout />)
fireEvent.press(screen.getByText('Dashboard'))
expect(routerMock.dismissTo).toHaveBeenCalledWith('/(drawer)/dashboard')
})
it('logs out from the Logout item', () => {
renderWithProviders(<Layout />)
fireEvent.press(screen.getByText('Logout'))
expect(staffAuthMock.logout).toHaveBeenCalledTimes(1)
})
it('emits the refresh event from the header refresh button', () => {
renderWithProviders(<Layout />)
fireEvent.press(iconByName('refresh')!)
expect(DeviceEventEmitter.emit).toHaveBeenCalledWith(REFRESH_EVENT)
})
it('opens the drawer from the header menu button when there is no back stack', () => {
renderWithProviders(<Layout />)
fireEvent.press(iconByName('menu')!)
expect(mockNavigation.openDrawer).toHaveBeenCalledTimes(1)
expect(routerMock.back).not.toHaveBeenCalled()
})
})