+ <>
- {open && (
-
-
- Notifications
- {unread > 0 && (
-
- )}
-
- {notifications.length === 0 ? (
-
No notifications
- ) : (
-
- {notifications.map(n => {
- const inner = (
- <>
-
- {n.title ?? 'Notification'}
-
-
- {new Date(n.sent_at).toLocaleString()}
-
- >
- )
- return (
- -
- {n.link ? (
- setOpen(false)}>
- {inner}
-
- ) : (
-
{inner}
- )}
-
- )
- })}
-
- )}
+
setOpen(false)}
+ title="Notifications"
+ actions={
+ unread > 0 ? (
+
+ ) : undefined
+ }
+ >
+
+ {(['all', 'unread'] as const).map(t => (
+
+ ))}
- )}
-
+
+ {groups.length === 0 ? (
+
No notifications
+ ) : (
+ groups.map(g => (
+
+ {g.label}
+
+ {g.items.map(n => {
+ const body = (
+ <>
+
+ {n.title ?? 'Notification'}
+
+ {new Date(n.sent_at).toLocaleString()}
+ >
+ )
+ return (
+ -
+ {n.link ? (
+ setOpen(false)}>
+ {body}
+
+ ) : (
+
{body}
+ )}
+
+ )
+ })}
+
+
+ ))
+ )}
+
+ >
)
}
diff --git a/tests/components/layout/app-shell.test.tsx b/tests/components/layout/app-shell.test.tsx
new file mode 100644
index 0000000..833c81a
--- /dev/null
+++ b/tests/components/layout/app-shell.test.tsx
@@ -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: () =>
}))
+
+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(
page body
)
+ 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(
)
+ 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(
)
+ 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(
)
+ 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(
)
+ expect(container.querySelector('header form')).toBeNull()
+ })
+
+ it('shows the user chip, version label, and logs out', async () => {
+ render(
)
+ 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(
)
+ 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(
)
+ expect(container.innerHTML).not.toMatch(/border-[a-z]?-?2\b/)
+ })
+})
diff --git a/tests/components/layout/nav-items.test.ts b/tests/components/layout/nav-items.test.ts
new file mode 100644
index 0000000..c5cdbe1
--- /dev/null
+++ b/tests/components/layout/nav-items.test.ts
@@ -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()
+ })
+})
diff --git a/tests/components/layout/sidebar.test.tsx b/tests/components/layout/sidebar.test.tsx
deleted file mode 100644
index f8e454c..0000000
--- a/tests/components/layout/sidebar.test.tsx
+++ /dev/null
@@ -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(
)
- 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(
)
- 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(
)
- 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(
)
- expect(screen.getByText('v1.2.2')).toBeTruthy()
- })
-})
diff --git a/tests/components/notifications/bell.test.tsx b/tests/components/notifications/bell.test.tsx
new file mode 100644
index 0000000..7c1a061
--- /dev/null
+++ b/tests/components/notifications/bell.test.tsx
@@ -0,0 +1,44 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest'
+import { render, screen, fireEvent, waitFor } from '@testing-library/react'
+import { NotificationBell } from '@/components/notifications/bell'
+
+const payload = {
+ unread: 2,
+ notifications: [
+ { id: 'n1', title: 'CAPA overdue', link: '/hse/capa/1', sent_at: new Date().toISOString(), read_at: null },
+ { id: 'n2', title: 'New incident', link: null, sent_at: '2020-01-01T08:00:00Z', read_at: '2020-01-01T09:00:00Z' },
+ ],
+}
+
+beforeEach(() => {
+ vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => payload }))
+})
+
+describe('NotificationBell', () => {
+ it('shows the unread count on the bell', async () => {
+ render(
)
+ await waitFor(() => expect(screen.getByRole('button', { name: 'Notifications (2 unread)' })).toBeTruthy())
+ expect(screen.getByText('2')).toBeTruthy()
+ })
+
+ it('opens a drawer with grouped notifications and an Unread tab', async () => {
+ render(
)
+ fireEvent.click(await screen.findByRole('button', { name: /Notifications/ }))
+ expect(screen.getByRole('dialog', { name: 'Notifications' })).toBeTruthy()
+ expect(screen.getByText('CAPA overdue')).toBeTruthy()
+ expect(screen.getByText('Today')).toBeTruthy()
+ expect(screen.getByText('Earlier')).toBeTruthy()
+ fireEvent.click(screen.getByRole('button', { name: /Unread/ }))
+ expect(screen.queryByText('New incident')).toBeNull()
+ expect(screen.getByText('CAPA overdue')).toBeTruthy()
+ })
+
+ it('marks everything read', async () => {
+ render(
)
+ fireEvent.click(await screen.findByRole('button', { name: /Notifications/ }))
+ fireEvent.click(screen.getByRole('button', { name: 'Mark all read' }))
+ await waitFor(() =>
+ expect(fetch).toHaveBeenCalledWith('/api/notifications', expect.objectContaining({ method: 'POST', body: JSON.stringify({ all: true }) })),
+ )
+ })
+})