UNPKG

@amsterdam/design-system-react

Version:

All React components from the Amsterdam Design System. Use it to compose pages in your website or application.

82 lines (81 loc) 4.21 kB
import { jsx as _jsx } from "react/jsx-runtime"; /** * @license EUPL-1.2+ * Copyright Gemeente Amsterdam */ import { fireEvent, render, screen } from '@testing-library/react'; import { createRef } from 'react'; import { describe, expect, it, vi } from 'vitest'; import { Alert } from './Alert'; describe('Alert', () => { it('renders', () => { render(_jsx(Alert, { heading: "Let op!", headingLevel: 2 })); const component = screen.getByRole('region', { name: 'Let op!' }); const icon = component?.querySelector('.ams-alert__severity-indicator > .ams-icon'); expect(component).toBeInTheDocument(); expect(component).toBeVisible(); expect(icon).toBeInTheDocument(); expect(icon).not.toBeVisible(); // The icon is hidden by default, and only shown when the CSS loads. }); it('renders a design system BEM class name', () => { render(_jsx(Alert, { heading: "Let op!", headingLevel: 2 })); const component = screen.getByRole('region', { name: 'Let op!' }); expect(component).toHaveClass('ams-alert'); }); it('renders an extra class name', () => { render(_jsx(Alert, { className: "extra", heading: "Let op!", headingLevel: 2 })); const component = screen.getByRole('region', { name: 'Let op!' }); expect(component).toHaveClass('ams-alert extra'); }); it('supports ForwardRef in React', () => { const ref = createRef(); render(_jsx(Alert, { heading: "Let op!", headingLevel: 2, ref: ref })); const component = screen.getByRole('region', { name: 'Let op!' }); expect(ref.current).toBe(component); }); it('renders a heading', () => { render(_jsx(Alert, { heading: "Test heading", headingLevel: 2 })); const heading = screen.getByRole('heading', { name: 'Test heading', }); expect(heading).toBeInTheDocument(); }); it('renders the close button', () => { render(_jsx(Alert, { closeable: true, heading: "Let op!", headingLevel: 2 })); const component = screen.getByRole('region', { name: 'Let op!' }); const closeButton = component?.querySelector('.ams-icon-button'); expect(closeButton).toBeInTheDocument(); expect(closeButton).toBeVisible(); }); it('renders the close button with a label', () => { render(_jsx(Alert, { closeable: true, closeButtonLabel: "Close", heading: "Let op!", headingLevel: 2 })); const closeButton = screen.getByRole('button', { name: 'Close' }); expect(closeButton).toBeInTheDocument(); }); it('fires the onClose event when the close button is clicked', () => { const onClose = vi.fn(); render(_jsx(Alert, { closeable: true, heading: "Let op!", headingLevel: 2, onClose: onClose })); const component = screen.getByRole('region', { name: 'Let op!' }); const closeButton = component?.querySelector('.ams-icon-button'); fireEvent.click(closeButton); expect(onClose).toHaveBeenCalled(); }); it('uses a custom headingId to label the Alert', () => { render(_jsx(Alert, { heading: "Let op!", headingId: "custom-heading-id", headingLevel: 2 })); const component = screen.getByRole('region', { name: 'Let op!' }); expect(component).toBeInTheDocument(); expect(component).toHaveAttribute('aria-labelledby', 'custom-heading-id'); }); it('does not label the Alert when headingId is set to null', () => { const { container } = render(_jsx(Alert, { heading: "Let op!", headingId: null, headingLevel: 2 })); const component = container.querySelector(':only-child'); expect(component).not.toHaveAttribute('aria-labelledby'); }); it('passes additional props', () => { render(_jsx(Alert, { "aria-hidden": false, "data-test": "data-test", heading: "Let op!", headingLevel: 2, id: "id" })); const component = screen.getByRole('region', { name: 'Let op!' }); expect(component).toHaveAttribute('aria-hidden', 'false'); expect(component).toHaveAttribute('id', 'id'); expect(component).toHaveAttribute('data-test', 'data-test'); }); });