feat(design): add AppShell, MobileShell and the notifications drawer

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-01 09:31:12 +08:00
co-authored by Claude Haiku 4.5
parent 32dd42ac79
commit 9ba708ecb7
10 changed files with 482 additions and 311 deletions
@@ -0,0 +1,77 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
const push = vi.fn()
const refresh = vi.fn()
vi.mock('next/navigation', () => ({
usePathname: () => '/hse/capa',
useRouter: () => ({ push, refresh }),
}))
vi.mock('@/components/notifications/bell', () => ({ NotificationBell: () => <button aria-label="Notifications" /> }))
import { AppShell } from '@/components/layout/app-shell'
beforeEach(() => {
push.mockClear()
refresh.mockClear()
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true }))
})
describe('AppShell', () => {
it('has a 56px dark top bar, a grey sidebar and the page content', () => {
const { container } = render(<AppShell role="hse" userName="Ada Lovelace"><p>page body</p></AppShell>)
expect(container.querySelector('header')?.className).toContain('h-14')
expect(container.querySelector('header')?.className).toContain('bg-topbar')
expect(container.querySelector('aside')?.className).toContain('bg-rule')
expect(container.querySelector('aside')?.className).toContain('w-60')
expect(screen.getByText('page body')).toBeTruthy()
expect(screen.getByText('Setia HSE')).toBeTruthy()
})
it('marks the active sidebar item with aria-current on a white ringed item', () => {
render(<AppShell role="hse" userName="Ada"><span /></AppShell>)
const active = screen.getAllByRole('link', { name: /CAPA/ }).find(a => a.getAttribute('aria-current') === 'page')!
expect(active.className).toContain('bg-surface')
expect(active.className).toContain('shadow-chip')
})
it('shows a GET search form that targets the incident list for HSE roles', () => {
const { container } = render(<AppShell role="hse" userName="Ada"><span /></AppShell>)
const form = container.querySelector('header form')!
expect(form.getAttribute('action')).toBe('/hse/incidents')
expect(form.getAttribute('method')).toBe('get')
expect((form.querySelector('input') as HTMLInputElement).name).toBe('q')
})
it('focuses the search box on Cmd-K', () => {
render(<AppShell role="hse" userName="Ada"><span /></AppShell>)
fireEvent.keyDown(document, { key: 'k', metaKey: true })
expect(document.activeElement?.getAttribute('name')).toBe('q')
})
it('hides the search box for roles without an incident list', () => {
const { container } = render(<AppShell role="reporter" userName="Ada"><span /></AppShell>)
expect(container.querySelector('header form')).toBeNull()
})
it('shows the user chip, version label, and logs out', async () => {
render(<AppShell role="hse" userName="Ada Lovelace"><span /></AppShell>)
expect(screen.getAllByText('AL').length).toBeGreaterThan(0)
expect(screen.getByText('v1.2.2')).toBeTruthy()
fireEvent.click(screen.getAllByRole('button', { name: /Log ?out/i })[0])
await Promise.resolve()
expect(fetch).toHaveBeenCalledWith('/api/auth/logout', { method: 'POST' })
})
it('renders a mobile tab bar that is hidden from md up', () => {
const { container } = render(<AppShell role="hse" userName="Ada"><span /></AppShell>)
const nav = container.querySelector('nav[aria-label="Mobile"]')!
expect(nav.className).toContain('md:hidden')
expect(nav.className).toContain('border-t')
})
it('has no 2px rules', () => {
const { container } = render(<AppShell role="admin" userName="Ada"><span /></AppShell>)
expect(container.innerHTML).not.toMatch(/border-[a-z]?-?2\b/)
})
})
+24
View File
@@ -0,0 +1,24 @@
import { describe, it, expect } from 'vitest'
import { navItemsFor, isActivePath, searchHrefFor } from '@/components/layout/nav-items'
describe('nav items', () => {
it('returns the role menu and falls back to reporter', () => {
expect(navItemsFor('hse').map(i => i.label)).toEqual(['Dashboard', 'Incidents', 'CAPA', 'Settings'])
expect(navItemsFor('admin')[0].label).toBe('Admin')
expect(navItemsFor('nobody')).toEqual(navItemsFor('reporter'))
})
it('matches root pages exactly and nested pages by prefix', () => {
expect(isActivePath('/reporter', '/reporter')).toBe(true)
expect(isActivePath('/reporter', '/reporter/incidents/1')).toBe(false)
expect(isActivePath('/hse/incidents', '/hse/incidents/abc')).toBe(true)
})
it('offers the search box only to roles that have an incident list', () => {
expect(searchHrefFor('hse')).toBe('/hse/incidents')
expect(searchHrefFor('admin')).toBe('/hse/incidents')
expect(searchHrefFor('supervisor')).toBe('/supervisor/incidents')
expect(searchHrefFor('reporter')).toBeNull()
expect(searchHrefFor('capa_owner')).toBeNull()
})
})
-41
View File
@@ -1,41 +0,0 @@
import { describe, it, expect, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
vi.mock('next/navigation', () => ({
usePathname: () => '/hse/capa',
useRouter: () => ({ push: vi.fn(), refresh: vi.fn() }),
}))
vi.mock('@/components/notifications/bell', () => ({ NotificationBell: () => null }))
import { Sidebar } from '@/components/layout/sidebar'
describe('Sidebar (Modernist shell)', () => {
it('renders the role nav with Lucide icons instead of inline paths', () => {
const { container } = render(<Sidebar role="hse" userName="Ada Lovelace" userEmail="" />)
expect(screen.getAllByText('Dashboard').length).toBeGreaterThan(0)
expect(screen.getAllByText('CAPA').length).toBeGreaterThan(0)
expect(container.querySelectorAll('svg.lucide').length).toBeGreaterThan(4)
})
it('marks the active link in the accent with an aria-current', () => {
render(<Sidebar role="hse" userName="Ada" userEmail="" />)
const active = screen.getAllByRole('link', { name: /CAPA/ }).find(a => a.getAttribute('aria-current') === 'page')
expect(active).toBeTruthy()
expect(active?.className).toContain('text-accent-700')
expect(active?.className).toContain('border-accent')
})
it('is flat: a 2px divider rule, ground fill and no rounded corners except the avatar', () => {
const { container } = render(<Sidebar role="hse" userName="Ada" userEmail="" />)
const aside = container.querySelector('aside')
expect(aside?.className).toContain('border-r')
expect(aside?.className).toContain('bg-ground')
const rounded = container.innerHTML.match(/rounded[\w-]*/g) ?? []
expect(rounded.every(r => r === 'rounded-full')).toBe(true)
})
it('keeps the version label', () => {
render(<Sidebar role="hse" userName="Ada" userEmail="" />)
expect(screen.getByText('v1.2.2')).toBeTruthy()
})
})