320 lines
11 KiB
TypeScript
320 lines
11 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 3.3 — Drawer layout (`app/(drawer)/_layout.tsx`).
|
|
*
|
|
* The production `Drawer` primitive needs a NavigationContainer, so this file
|
|
* supplies a tiny test-only drawer that renders `drawerContent`, the resolved
|
|
* header options and the screen children. `@react-navigation/drawer` is shimmed
|
|
* for the same reason (its real theme hook is not available outside a navigator).
|
|
*/
|
|
import React from 'react'
|
|
import { ActivityIndicator, DeviceEventEmitter } from 'react-native'
|
|
import { act, fireEvent } from '@testing-library/react-native'
|
|
|
|
import DrawerLayout from '@/app/(drawer)/_layout'
|
|
import { renderWithProviders, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { setProcedure } from '../mocks/trpc-client'
|
|
import { router } from '../mocks/expo-router-mock'
|
|
import { __snapshot } from '../mocks/secure-store-mock'
|
|
import { REFRESH_EVENT, FORCE_LOGOUT_EVENT } from 'common-ui/src/lib/const-strs'
|
|
import queryClient from '@/utils/queryClient'
|
|
|
|
jest.mock('expo-router/drawer', () => {
|
|
const React = require('react')
|
|
const { View, Text } = require('react-native')
|
|
const base = require('../mocks/expo-router-mock')
|
|
|
|
let navState: any = {
|
|
index: 0,
|
|
routes: [{ name: 'dashboard', state: { index: 0, routes: [{ name: 'index' }] } }],
|
|
}
|
|
const navigation = { getState: () => navState, openDrawer: jest.fn() }
|
|
let lastOptions: any = null
|
|
|
|
const withId = (node: any, testID: string) =>
|
|
node ? React.cloneElement(node, { testID }) : null
|
|
|
|
const Drawer = ({ children, drawerContent: DrawerContent, screenOptions }: any) => {
|
|
const options =
|
|
typeof screenOptions === 'function' ? screenOptions({ navigation }) : screenOptions || {}
|
|
lastOptions = options
|
|
return React.createElement(
|
|
View,
|
|
{ testID: 'drawer-mock' },
|
|
React.createElement(Text, { testID: 'drawer-header-title' }, options.headerTitle),
|
|
React.createElement(
|
|
View,
|
|
{ testID: 'drawer-header-left' },
|
|
withId(options.headerLeft && options.headerLeft(), 'drawer-header-left-button')
|
|
),
|
|
React.createElement(
|
|
View,
|
|
{ testID: 'drawer-header-right' },
|
|
withId(options.headerRight && options.headerRight(), 'drawer-header-right-button')
|
|
),
|
|
DrawerContent ? React.createElement(DrawerContent) : null,
|
|
children
|
|
)
|
|
}
|
|
Drawer.Screen = () => null
|
|
Drawer.Trigger = () => null
|
|
|
|
return {
|
|
...base,
|
|
Drawer,
|
|
__setDrawerNavState: (next: any) => {
|
|
navState = next
|
|
},
|
|
__getDrawerNavigation: () => navigation,
|
|
__getDrawerOptions: () => lastOptions,
|
|
}
|
|
})
|
|
|
|
jest.mock('@react-navigation/drawer', () => {
|
|
const React = require('react')
|
|
const { View, Text, TouchableOpacity } = require('react-native')
|
|
return {
|
|
DrawerContentScrollView: ({ children }: any) => React.createElement(View, null, children),
|
|
DrawerItem: ({ label, onPress }: any) =>
|
|
React.createElement(
|
|
TouchableOpacity,
|
|
{ onPress, testID: `drawer-item-${label}` },
|
|
React.createElement(Text, null, label)
|
|
),
|
|
}
|
|
})
|
|
|
|
// eslint-disable-next-line import/first
|
|
const drawerMock = require('expo-router/drawer') as {
|
|
__setDrawerNavState: (next: any) => void
|
|
__getDrawerNavigation: () => { openDrawer: jest.Mock }
|
|
__getDrawerOptions: () => any
|
|
}
|
|
|
|
const DEFAULT_NAV_STATE = {
|
|
index: 0,
|
|
routes: [{ name: 'dashboard', state: { index: 0, routes: [{ name: 'index' }] } }],
|
|
}
|
|
|
|
async function renderDrawer(auth: 'in' | 'out' = 'in') {
|
|
const view = renderWithProviders(<DrawerLayout />, { auth })
|
|
await flush()
|
|
return view
|
|
}
|
|
|
|
describe('Drawer layout (ADRAW-01..19)', () => {
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
drawerMock.__setDrawerNavState(DEFAULT_NAV_STATE)
|
|
})
|
|
|
|
it('ADRAW-01: shows a blue spinner while auth is loading', () => {
|
|
const view = renderWithProviders(<DrawerLayout />, { auth: 'out' })
|
|
|
|
const spinner = view.UNSAFE_getByType(ActivityIndicator)
|
|
expect(spinner.props.color).toBe('#3B82F6')
|
|
})
|
|
|
|
it('ADRAW-02: redirects signed-out staff to login', async () => {
|
|
await renderDrawer('out')
|
|
|
|
expect(router.replace).toHaveBeenCalledWith('/login')
|
|
})
|
|
|
|
it('ADRAW-03: renders the staff name and region from the me query', async () => {
|
|
const view = await renderDrawer()
|
|
|
|
expect(await view.findByText('Mahabubnagar')).toBeTruthy()
|
|
expect(view.getByText('Admin One')).toBeTruthy()
|
|
})
|
|
|
|
it('ADRAW-04: falls back to the stored staff when me is unavailable', async () => {
|
|
setProcedure('admin.staffUser.me', { mode: 'error', error: new Error('offline') })
|
|
const view = await renderDrawer()
|
|
|
|
expect(view.getByText('Admin One')).toBeTruthy()
|
|
expect(view.getByText('Region #1')).toBeTruthy()
|
|
})
|
|
|
|
it('ADRAW-05: regionId 0 renders "No region"', async () => {
|
|
setProcedure('admin.staffUser.me', { data: { name: 'Unassigned', regionId: 0 } })
|
|
const view = await renderDrawer()
|
|
|
|
expect(await view.findByText('No region')).toBeTruthy()
|
|
expect(view.getByText('Unassigned')).toBeTruthy()
|
|
})
|
|
|
|
it('ADRAW-06: a region without a name renders "Region #<id>"', async () => {
|
|
setProcedure('admin.staffUser.me', { data: { name: 'Admin Two', regionId: 2 } })
|
|
const view = await renderDrawer()
|
|
|
|
expect(await view.findByText('Region #2')).toBeTruthy()
|
|
})
|
|
|
|
it('ADRAW-07: Dashboard dismisses to the dashboard route', async () => {
|
|
const view = await renderDrawer()
|
|
|
|
fireEvent.press(view.getByTestId('drawer-item-Dashboard'))
|
|
|
|
expect(router.dismissTo).toHaveBeenCalledWith('/(drawer)/dashboard')
|
|
})
|
|
|
|
it('ADRAW-08: Products and Prices open their routes', async () => {
|
|
const view = await renderDrawer()
|
|
|
|
fireEvent.press(view.getByTestId('drawer-item-Products'))
|
|
fireEvent.press(view.getByTestId('drawer-item-Prices Overview'))
|
|
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/products')
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/prices-overview')
|
|
})
|
|
|
|
it('ADRAW-09: Groupings / Banners / Slots open their routes', async () => {
|
|
const view = await renderDrawer()
|
|
|
|
fireEvent.press(view.getByTestId('drawer-item-Product Groupings'))
|
|
fireEvent.press(view.getByTestId('drawer-item-Dashboard Banners'))
|
|
fireEvent.press(view.getByTestId('drawer-item-Slots'))
|
|
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/product-groupings')
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/dashboard-banners')
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/slots')
|
|
})
|
|
|
|
it('ADRAW-10: Complaints / Manage Orders / Coupons open their routes', async () => {
|
|
const view = await renderDrawer()
|
|
|
|
fireEvent.press(view.getByTestId('drawer-item-Complaints'))
|
|
fireEvent.press(view.getByTestId('drawer-item-Manage Orders'))
|
|
fireEvent.press(view.getByTestId('drawer-item-Coupons'))
|
|
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/complaints')
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/manage-orders')
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/coupons')
|
|
})
|
|
|
|
it('ADRAW-11: Vendor Snippets / Stores / Users / Notifications open their routes', async () => {
|
|
const view = await renderDrawer()
|
|
|
|
fireEvent.press(view.getByTestId('drawer-item-Vendor Snippets'))
|
|
fireEvent.press(view.getByTestId('drawer-item-Stores'))
|
|
fireEvent.press(view.getByTestId('drawer-item-User Management'))
|
|
fireEvent.press(view.getByTestId('drawer-item-Send Notifications'))
|
|
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/vendor-snippets')
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/stores')
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/user-management')
|
|
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/send-notifications')
|
|
})
|
|
|
|
it('ADRAW-12: Logout clears the session, the query cache and returns to login', async () => {
|
|
const clearSpy = jest.spyOn(queryClient, 'clear')
|
|
const view = await renderDrawer()
|
|
|
|
expect(__snapshot().jwt_token).toBeTruthy()
|
|
|
|
fireEvent.press(view.getByTestId('drawer-item-Logout'))
|
|
await flush()
|
|
|
|
const storage = __snapshot()
|
|
expect(storage.jwt_token).toBeUndefined()
|
|
expect(storage.staff_session).toBeUndefined()
|
|
expect(clearSpy).toHaveBeenCalled()
|
|
expect(router.replace).toHaveBeenCalledWith('/login')
|
|
clearSpy.mockRestore()
|
|
})
|
|
|
|
it('ADRAW-13: a force-logout event performs the logout', async () => {
|
|
const view = await renderDrawer()
|
|
expect(view.getByText('Admin One')).toBeTruthy()
|
|
|
|
await act(async () => {
|
|
DeviceEventEmitter.emit(FORCE_LOGOUT_EVENT)
|
|
})
|
|
await flush()
|
|
|
|
const storage = __snapshot()
|
|
expect(storage.jwt_token).toBeUndefined()
|
|
expect(storage.staff_session).toBeUndefined()
|
|
expect(router.replace).toHaveBeenCalledWith('/login')
|
|
})
|
|
|
|
it('ADRAW-14: the header back button calls router.back on a nested route', async () => {
|
|
drawerMock.__setDrawerNavState({
|
|
index: 0,
|
|
routes: [{ name: 'dashboard', state: { index: 1, routes: [{ name: 'index' }, { name: 'products' }] } }],
|
|
})
|
|
const view = await renderDrawer()
|
|
|
|
fireEvent.press(view.getByTestId('drawer-header-left-button'))
|
|
|
|
expect(router.back).toHaveBeenCalled()
|
|
})
|
|
|
|
it('ADRAW-15: the header menu button opens the drawer on the dashboard', async () => {
|
|
const view = await renderDrawer()
|
|
|
|
fireEvent.press(view.getByTestId('drawer-header-left-button'))
|
|
|
|
expect(drawerMock.__getDrawerNavigation().openDrawer).toHaveBeenCalled()
|
|
})
|
|
|
|
it('ADRAW-16: maps every child screen to its header title', async () => {
|
|
const table: Array<[string, string]> = [
|
|
['index', 'Dashboard'],
|
|
['products', 'Products'],
|
|
['prices-overview', 'Prices Overview'],
|
|
['product-groupings', 'Product Groupings'],
|
|
['dashboard-banners', 'Dashboard Banners'],
|
|
['slots', 'Slots'],
|
|
['complaints', 'Complaints'],
|
|
['manage-orders', 'Manage Orders'],
|
|
['coupons', 'Coupons'],
|
|
['vendor-snippets', 'Vendor Snippets'],
|
|
['stores', 'Stores'],
|
|
['user-management', 'User Management'],
|
|
['send-notifications', 'Send Notifications'],
|
|
['product-tags', 'Product Tags'],
|
|
['rebalance-orders', 'Rebalance Orders'],
|
|
['customize-app', 'Customize App'],
|
|
]
|
|
|
|
for (const [name, title] of table) {
|
|
drawerMock.__setDrawerNavState({
|
|
index: 0,
|
|
routes: [{ name: 'dashboard', state: { index: 0, routes: [{ name }] } }],
|
|
})
|
|
// eslint-disable-next-line no-await-in-loop
|
|
const view = await renderDrawer()
|
|
expect(view.getByTestId('drawer-header-title').props.children).toBe(title)
|
|
view.unmount()
|
|
}
|
|
})
|
|
|
|
it('ADRAW-17: the header refresh button emits the refresh event', async () => {
|
|
const emitSpy = jest.spyOn(DeviceEventEmitter, 'emit')
|
|
const view = await renderDrawer()
|
|
|
|
fireEvent.press(view.getByTestId('drawer-header-right-button'))
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith(REFRESH_EVENT)
|
|
emitSpy.mockRestore()
|
|
})
|
|
|
|
it('ADRAW-18: an unknown nested route title falls back to "Dashboard"', async () => {
|
|
drawerMock.__setDrawerNavState({
|
|
index: 0,
|
|
routes: [{ name: 'dashboard', state: { index: 0, routes: [{ name: 'not-a-screen' }] } }],
|
|
})
|
|
const view = await renderDrawer()
|
|
|
|
expect(view.getByTestId('drawer-header-title').props.children).toBe('Dashboard')
|
|
})
|
|
|
|
it('ADRAW-19: drawer swipe is disabled', async () => {
|
|
await renderDrawer()
|
|
|
|
expect(drawerMock.__getDrawerOptions().swipeEnabled).toBe(false)
|
|
})
|
|
})
|