import { afterEach, describe, expect, it } from 'vitest' import { cleanup, render, screen } from '@testing-library/react' import Notice from '@/components/ui/Notice' afterEach(() => cleanup()) /** The path of the glyph a notice draws (its only svg path). */ function glyph(container: HTMLElement): string | null | undefined { return container.querySelector('svg path')?.getAttribute('d') } function glyphOf(icon: 'alert' | 'check' | 'wifi-off'): string | null | undefined { const { container, unmount } = render(x) const d = glyph(container) unmount() return d } describe('Notice', () => { it('keeps today\'s roles and glyphs by default', () => { const alert = glyphOf('alert') const check = glyphOf('check') const { container: error } = render(bad) expect(screen.getByRole('alert')).toHaveTextContent('bad') expect(glyph(error)).toBe(alert) cleanup() const { container: success } = render(ok) expect(screen.getByRole('status')).toHaveTextContent('ok') expect(glyph(success)).toBe(check) cleanup() render(note) expect(screen.getByRole('status')).toHaveTextContent('note') }) it('drops the live role when live is false, for every tone', () => { for (const tone of ['error', 'success', 'info', 'warning'] as const) { render({tone}) expect(screen.queryByRole('alert')).toBeNull() expect(screen.queryByRole('status')).toBeNull() expect(screen.getByText(tone)).toBeInTheDocument() cleanup() } }) it('draws the glyph it is given, the warning tone included', () => { const wifiOff = glyphOf('wifi-off') expect(wifiOff).toBeTruthy() expect(wifiOff).not.toBe(glyphOf('alert')) const { container } = render(offline) expect(glyph(container)).toBe(wifiOff) expect(screen.getByRole('status')).toHaveTextContent('offline') }) })