105 lines
5.6 KiB
TypeScript
105 lines
5.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||
import { render, screen, fireEvent, within } from '@testing-library/react'
|
||
|
||
const push = vi.fn()
|
||
vi.mock('next/navigation', () => ({ useRouter: () => ({ push }) }))
|
||
|
||
import { CapaWorkspace } from '@/components/capa/capa-workspace'
|
||
import type { CapaRow } from '@/lib/capa/list'
|
||
|
||
const row = (over: Partial<CapaRow>): CapaRow => ({
|
||
id: 'x', incident_id: 'i', description: 'Fit guard rail', department: 'Facilities', due_date: '2026-10-10',
|
||
priority: 'med', status: 'open', incident_ref: 'SC1-202607-0001', owner_name: 'Tan Mei Ling', ...over,
|
||
})
|
||
|
||
const capas: CapaRow[] = [
|
||
row({ id: '1', description: 'Anti-slip coating', due_date: '2026-09-12', priority: 'high', status: 'overdue', owner_name: 'Tan Mei Ling', department: 'Facilities' }),
|
||
row({ id: '2', description: 'Forklift refresher', due_date: '2026-10-20', priority: 'low', status: 'in_progress', owner_name: 'Ravi Kumar', department: 'Operations', incident_ref: 'SC3-202607-0002' }),
|
||
row({ id: '3', description: 'Guard rail repair', due_date: '2026-10-01', priority: 'med', status: 'pending_verification', owner_name: 'Tan Mei Ling', department: 'Facilities' }),
|
||
row({ id: '4', description: 'Signage', due_date: '2026-08-30', priority: 'low', status: 'closed', owner_name: null, department: 'Operations' }),
|
||
]
|
||
const today = '2026-09-27'
|
||
|
||
const desktopRows = () => screen.getAllByRole('row').slice(1)
|
||
|
||
beforeEach(() => push.mockClear())
|
||
|
||
describe('CapaWorkspace list', () => {
|
||
it('renders a TableCard with the action, incident, owner, tags and a late indicator', () => {
|
||
render(<CapaWorkspace capas={capas} today={today} />)
|
||
expect(screen.getAllByText('Anti-slip coating').length).toBeGreaterThan(0)
|
||
expect(screen.getAllByText('SC1-202607-0001').length).toBeGreaterThan(0)
|
||
expect(screen.getAllByText('Ravi Kumar').length).toBeGreaterThan(0)
|
||
expect(screen.getAllByText('Overdue').some(el => el.className.includes('bg-st-red-bg'))).toBe(true)
|
||
expect(screen.getAllByText('High').some(el => el.className.includes('bg-st-orange-bg'))).toBe(true)
|
||
expect(screen.getByText('15 days late')).toBeTruthy()
|
||
expect(screen.getByText('Showing 1–4 of 4')).toBeTruthy()
|
||
})
|
||
|
||
it('shows saved views with counts and filters by view', () => {
|
||
render(<CapaWorkspace capas={capas} today={today} />)
|
||
expect(screen.getByRole('button', { name: /All/ }).className).toContain('bg-rule-2')
|
||
fireEvent.click(screen.getByRole('button', { name: /Overdue/ }))
|
||
expect(screen.queryAllByText('Forklift refresher')).toHaveLength(0)
|
||
expect(screen.getAllByText('Anti-slip coating').length).toBeGreaterThan(0)
|
||
expect(screen.getByText('Showing 1–1 of 1')).toBeTruthy()
|
||
})
|
||
|
||
it('searches across action, incident and owner', () => {
|
||
render(<CapaWorkspace capas={capas} today={today} />)
|
||
fireEvent.change(screen.getByRole('searchbox', { name: 'Search actions' }), { target: { value: 'guard' } })
|
||
expect(screen.getAllByText('Guard rail repair').length).toBeGreaterThan(0)
|
||
expect(screen.queryAllByText('Signage')).toHaveLength(0)
|
||
})
|
||
|
||
it('filters by owner chip and clears the filters', () => {
|
||
render(<CapaWorkspace capas={capas} today={today} />)
|
||
fireEvent.change(screen.getByLabelText('Owner'), { target: { value: 'Ravi Kumar' } })
|
||
expect(screen.queryAllByText('Anti-slip coating')).toHaveLength(0)
|
||
expect(screen.getAllByText('Forklift refresher').length).toBeGreaterThan(0)
|
||
fireEvent.click(screen.getByRole('button', { name: 'Clear filters' }))
|
||
expect(screen.getAllByText('Anti-slip coating').length).toBeGreaterThan(0)
|
||
})
|
||
|
||
it('sorts by due date ascending by default and flips on the header', () => {
|
||
render(<CapaWorkspace capas={capas} today={today} />)
|
||
expect(desktopRows()[0].textContent).toContain('Signage')
|
||
fireEvent.click(screen.getByRole('button', { name: /Due/ }))
|
||
expect(desktopRows()[0].textContent).toContain('Forklift refresher')
|
||
})
|
||
|
||
it('opens a CAPA from its row', () => {
|
||
render(<CapaWorkspace capas={capas} today={today} />)
|
||
fireEvent.click(desktopRows().find(r => r.textContent?.includes('Anti-slip coating'))!)
|
||
expect(push).toHaveBeenCalledWith('/hse/capa/1')
|
||
})
|
||
|
||
it('shows an empty state when nothing matches', () => {
|
||
render(<CapaWorkspace capas={capas} today={today} />)
|
||
fireEvent.change(screen.getByRole('searchbox', { name: 'Search actions' }), { target: { value: 'zzzz' } })
|
||
expect(screen.getByText('No CAPA actions match')).toBeTruthy()
|
||
expect(screen.queryByText(/Showing/)).toBeNull()
|
||
})
|
||
})
|
||
|
||
describe('CapaWorkspace board', () => {
|
||
it('switches to a board of status columns with counts and card links', () => {
|
||
render(<CapaWorkspace capas={capas} today={today} />)
|
||
fireEvent.click(screen.getByRole('button', { name: 'Board' }))
|
||
expect(screen.queryByRole('table')).toBeNull()
|
||
const col = screen.getByRole('region', { name: 'in progress column' })
|
||
expect(within(col).getByText('1')).toBeTruthy()
|
||
expect(within(col).getByRole('link', { name: /Forklift refresher/ }).getAttribute('href')).toBe('/hse/capa/2')
|
||
expect(screen.getByRole('region', { name: 'reopened column' })).toBeTruthy()
|
||
expect(within(screen.getByRole('region', { name: 'reopened column' })).getByText('Empty')).toBeTruthy()
|
||
})
|
||
|
||
it('keeps the search and chip filters on the board', () => {
|
||
render(<CapaWorkspace capas={capas} today={today} />)
|
||
fireEvent.click(screen.getByRole('button', { name: 'Board' }))
|
||
fireEvent.change(screen.getByLabelText('Owner'), { target: { value: 'Ravi Kumar' } })
|
||
expect(screen.queryByRole('link', { name: /Anti-slip coating/ })).toBeNull()
|
||
expect(screen.getByRole('link', { name: /Forklift refresher/ })).toBeTruthy()
|
||
})
|
||
})
|