Files
faunosteo/apps/app/tests/unit/components.Notice.test.tsx
T

54 lines
2.0 KiB
TypeScript

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(<Notice tone="info" icon={icon}>x</Notice>)
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(<Notice tone="error">bad</Notice>)
expect(screen.getByRole('alert')).toHaveTextContent('bad')
expect(glyph(error)).toBe(alert)
cleanup()
const { container: success } = render(<Notice tone="success">ok</Notice>)
expect(screen.getByRole('status')).toHaveTextContent('ok')
expect(glyph(success)).toBe(check)
cleanup()
render(<Notice tone="info">note</Notice>)
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(<Notice tone={tone} live={false}>{tone}</Notice>)
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(<Notice tone="warning" icon="wifi-off">offline</Notice>)
expect(glyph(container)).toBe(wifiOff)
expect(screen.getByRole('status')).toHaveTextContent('offline')
})
})