UNPKG

@amsterdam/design-system-react

Version:

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

150 lines (149 loc) 6.43 kB
import { jsx as _jsx } from "react/jsx-runtime"; /** * @license EUPL-1.2+ * Copyright Gemeente Amsterdam */ import { StarIcon } from '@amsterdam/design-system-react-icons'; import { render, screen } from '@testing-library/react'; import { createRef } from 'react'; import { describe, expect, it, vi } from 'vitest'; import { Checkbox } from './Checkbox'; describe('Checkbox', () => { it('renders', () => { const { container } = render(_jsx(Checkbox, {})); const wrapper = container.querySelector(':only-child'); const input = screen.getByRole('checkbox'); const label = container.querySelector('label'); expect(wrapper).toBeInTheDocument(); expect(wrapper).toBeVisible(); expect(input).toBeInTheDocument(); expect(input).toBeVisible(); expect(label).toBeInTheDocument(); expect(label).toBeVisible(); }); it('renders a design system BEM class name', () => { const { container } = render(_jsx(Checkbox, {})); const wrapper = container.querySelector(':only-child'); const input = screen.getByRole('checkbox'); const label = container.querySelector('label'); expect(wrapper).toHaveClass('ams-checkbox'); expect(input).toHaveClass('ams-checkbox__input'); expect(label).toHaveClass('ams-checkbox__label'); }); it('renders an extra class name', () => { const { container } = render(_jsx(Checkbox, { className: "extra" })); const wrapper = container.querySelector(':only-child'); expect(wrapper).toHaveClass('ams-checkbox extra'); }); describe('Checked state', () => { it('is not checked by default', () => { render(_jsx(Checkbox, {})); const input = screen.getByRole('checkbox'); expect(input).not.toBeChecked(); }); it('can have a checked state', () => { const handleChange = () => { }; render(_jsx(Checkbox, { checked: true, onChange: handleChange })); const input = screen.getByRole('checkbox'); expect(input).toBeChecked(); }); }); describe('Indeterminate state', () => { it('is not indeterminate by default', () => { render(_jsx(Checkbox, {})); const input = screen.getByRole('checkbox'); expect(input).not.toBePartiallyChecked(); }); it('can have an indeterminate state', () => { render(_jsx(Checkbox, { indeterminate: true })); const input = screen.getByRole('checkbox'); expect(input).toBePartiallyChecked(); }); }); describe('Invalid state', () => { it('is not invalid by default', () => { render(_jsx(Checkbox, {})); const input = screen.getByRole('checkbox'); expect(input).not.toBeInvalid(); }); it('can have an invalid state', () => { render(_jsx(Checkbox, { invalid: true })); const input = screen.getByRole('checkbox'); expect(input).toHaveAttribute('aria-invalid'); expect(input).toBeInvalid(); }); it('omits non-essential invalid attributes when not invalid', () => { render(_jsx(Checkbox, { invalid: false })); const input = screen.getByRole('checkbox'); expect(input).not.toHaveAttribute('aria-invalid'); }); }); describe('Disabled state', () => { it('is not disabled by default', () => { render(_jsx(Checkbox, {})); const input = screen.getByRole('checkbox'); expect(input).not.toBeDisabled(); }); it('can have a disabled state', () => { render(_jsx(Checkbox, { disabled: true })); const input = screen.getByRole('checkbox'); expect(input).toBeDisabled(); }); }); describe('Disabled invalid state', () => { it('can have a disabled invalid state', () => { render(_jsx(Checkbox, { disabled: true, invalid: true })); const input = screen.getByRole('checkbox'); expect(input).toBeDisabled(); expect(input).toBeInvalid(); }); }); it('can trigger a change event', () => { const handleChange = vi.fn(); render(_jsx(Checkbox, { onChange: handleChange })); const input = screen.getByRole('checkbox'); input?.click(); expect(handleChange).toHaveBeenCalled(); }); it('does not trigger a change event when disabled', () => { const handleChange = vi.fn(); render(_jsx(Checkbox, { disabled: true, onChange: handleChange })); const input = screen.getByRole('checkbox'); input?.click(); expect(handleChange).not.toHaveBeenCalled(); }); it('can trigger a change event by clicking on label', () => { const handleChange = vi.fn(); const { container } = render(_jsx(Checkbox, { onChange: handleChange })); const label = container.querySelector('label'); label?.click(); expect(handleChange).toHaveBeenCalled(); }); it('can use a custom id', () => { const handleChange = vi.fn(); const { container } = render(_jsx(Checkbox, { id: "test-id", onChange: handleChange })); const input = screen.getByRole('checkbox'); expect(input).toHaveAttribute('id', 'test-id'); const label = container.querySelector('label'); label?.click(); expect(handleChange).toHaveBeenCalled(); }); it('shows a custom icon', () => { const { container } = render(_jsx(Checkbox, { icon: _jsx(StarIcon, { className: "test-class" }) })); const icon = container.querySelector('svg'); expect(icon).toHaveClass('test-class'); }); it('supports ForwardRef in React', () => { const ref = createRef(); render(_jsx(Checkbox, { ref: ref })); const input = screen.getByRole('checkbox'); expect(ref.current).toBe(input); }); it('passes additional props', () => { render(_jsx(Checkbox, { "aria-hidden": "false", "data-test": "data-test", id: "id" })); const component = screen.getByRole('checkbox'); expect(component).toHaveAttribute('aria-hidden', 'false'); expect(component).toHaveAttribute('id', 'id'); expect(component).toHaveAttribute('data-test', 'data-test'); }); });