/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Catalogue section 3.4 — Dashboard hub (`tests/pages/dashboard.test.tsx`).
*/
import React from 'react'
import { Text } from 'react-native'
import { fireEvent } from '@testing-library/react-native'
import Dashboard from '@/app/(drawer)/dashboard/index'
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 { useNavigationTarget } from 'common-ui/hooks/useNavigationTarget'
function NavigationProbe() {
const { navigationTarget } = useNavigationTarget()
return {navigationTarget ?? 'none'}
}
function renderDashboard() {
return renderWithProviders(
<>
>,
{ auth: 'in' }
)
}
describe('Dashboard hub (ADASH-01..15)', () => {
beforeEach(() => resetAllMocks())
it('ADASH-01: renders Quick Actions and every category/menu item', () => {
const view = renderDashboard()
expect(view.getByText('Quick Actions')).toBeTruthy()
expect(view.getByText('Orders')).toBeTruthy()
expect(view.getByText('Products & Inventory')).toBeTruthy()
expect(view.getByText('Marketing & Promotions')).toBeTruthy()
expect(view.getAllByText('User Management').length).toBeGreaterThanOrEqual(1)
expect(view.getByText('Settings & Configuration')).toBeTruthy()
expect(view.getByText('Manage Orders')).toBeTruthy()
expect(view.getByText('Delivery Sequences')).toBeTruthy()
expect(view.getByText('Delivery Slots')).toBeTruthy()
expect(view.getByText('Add Product')).toBeTruthy()
expect(view.getByText('Complaints')).toBeTruthy()
expect(view.getByText('Products')).toBeTruthy()
expect(view.getByText('Prices Overview')).toBeTruthy()
expect(view.getByText('Product Tags')).toBeTruthy()
expect(view.getByText('Product Groupings')).toBeTruthy()
expect(view.getByText('Stores')).toBeTruthy()
expect(view.getByText('Coupons')).toBeTruthy()
expect(view.getByText('App Constants')).toBeTruthy()
expect(view.getByText('Send Notifications')).toBeTruthy()
})
it('ADASH-02: shows the unresolved-complaint badge count from essentials', async () => {
setProcedure('admin.user.getEssentials', { data: { unresolvedComplaints: 7 } })
const view = renderDashboard()
expect(await view.findByText('7')).toBeTruthy()
})
it('ADASH-03: no badge when there are zero unresolved complaints', async () => {
setProcedure('admin.user.getEssentials', { data: { unresolvedComplaints: 0 } })
const view = renderDashboard()
await flush()
expect(view.queryByText('0')).toBeNull()
})
it('ADASH-04: Manage Orders sets the navigation target then opens the hub', () => {
const view = renderDashboard()
fireEvent.press(view.getByText('Manage Orders'))
expect(view.getByTestId('nav-target').props.children).toBe('/(drawer)/dashboard/manage-orders/orders')
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/manage-orders')
})
it('ADASH-05: Delivery Sequences sets the navigation target then opens the hub', () => {
const view = renderDashboard()
fireEvent.press(view.getByText('Delivery Sequences'))
expect(view.getByTestId('nav-target').props.children).toBe('/(drawer)/dashboard/manage-orders/delivery-sequences')
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/manage-orders')
})
it('ADASH-06: Delivery Slots opens the slots route', () => {
const view = renderDashboard()
fireEvent.press(view.getByTestId('delivery-slots-menu-item'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/slots')
})
it('ADASH-07: Add Product opens the product form', () => {
const view = renderDashboard()
fireEvent.press(view.getByTestId('add-product-menu-item'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/products/add')
})
it('ADASH-08: Complaints opens the complaints list', () => {
const view = renderDashboard()
fireEvent.press(view.getByText('Complaints'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/complaints')
})
it('ADASH-09: Products / Prices / Tags / Groupings routes', () => {
const view = renderDashboard()
fireEvent.press(view.getByText('Products'))
fireEvent.press(view.getByText('Prices Overview'))
fireEvent.press(view.getByText('Product Tags'))
fireEvent.press(view.getByText('Product Groupings'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/products')
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/prices-overview')
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/product-tags')
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/product-groupings')
})
it('ADASH-10: Stores / Coupons / App Constants routes', () => {
const view = renderDashboard()
fireEvent.press(view.getByText('Stores'))
fireEvent.press(view.getByText('Coupons'))
fireEvent.press(view.getByText('App Constants'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/stores')
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/coupons')
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/customize-app')
})
it('ADASH-11: User Management / Send Notifications routes', () => {
const view = renderDashboard()
fireEvent.press(view.getAllByText('User Management')[1])
fireEvent.press(view.getByText('Send Notifications'))
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/user-management')
expect(router.push).toHaveBeenCalledWith('/(drawer)/dashboard/send-notifications')
})
it('ADASH-12: a category with no items is omitted', () => {
// Every current category has items; assert the render does not contain an
// empty section heading by checking the category titles present.
const view = renderDashboard()
const headings = ['Orders', 'Products & Inventory', 'Marketing & Promotions', 'Settings & Configuration']
headings.forEach((heading) => expect(view.getAllByText(heading)).toHaveLength(1))
expect(view.getAllByText('User Management').length).toBeGreaterThanOrEqual(1)
})
it('ADASH-13: long descriptions render without crashing', () => {
const view = renderDashboard()
expect(view.getByText('View and manage customer orders')).toBeTruthy()
})
it('ADASH-14: an essentials error still renders the menu, with no badge', async () => {
setProcedure('admin.user.getEssentials', { mode: 'error', error: new Error('boom') })
const view = renderDashboard()
await flush()
expect(view.getByText('Quick Actions')).toBeTruthy()
expect(view.getByText('Complaints')).toBeTruthy()
expect(view.queryByText('7')).toBeNull()
})
it('ADASH-15: menu entries expose accessibility labels', () => {
const view = renderDashboard()
expect(view.getByTestId('add-product-menu-item').props.accessibilityLabel).toBe('add-product-menu-item')
})
})