Files

45 lines
1.9 KiB
TypeScript

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(<NotificationBell />)
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(<NotificationBell />)
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(<NotificationBell />)
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 }) })),
)
})
})