UNPKG

@amsterdam/design-system-react

Version:

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

85 lines (84 loc) 4.25 kB
import { jsx as _jsx } from "react/jsx-runtime"; /** * @license EUPL-1.2+ * Copyright Gemeente Amsterdam */ import { render, screen } from '@testing-library/react'; import { createRef } from 'react'; import { describe, expect, it } from 'vitest'; import { FieldSet } from './FieldSet'; describe('FieldSet', () => { it('renders', () => { render(_jsx(FieldSet, { legend: "Test" })); const component = screen.getByRole('group', { name: 'Test' }); expect(component).toBeInTheDocument(); expect(component).toBeVisible(); }); it('renders a design system BEM class name', () => { render(_jsx(FieldSet, { legend: "Test" })); const component = screen.getByRole('group', { name: 'Test' }); expect(component).toHaveClass('ams-field-set'); }); it('renders an extra class name', () => { render(_jsx(FieldSet, { className: "extra", legend: "Test" })); const component = screen.getByRole('group', { name: 'Test' }); expect(component).toHaveClass('ams-field-set extra'); }); it('renders the correct legend class name', () => { const { container } = render(_jsx(FieldSet, { legend: "Test" })); const component = container.querySelector('legend'); expect(component).toHaveClass('ams-field-set__legend'); }); it('renders the error class', () => { const { container } = render(_jsx(FieldSet, { invalid: true, legend: "Test" })); const component = container.querySelector(':only-child'); expect(component).toHaveClass('ams-field-set--invalid'); }); it('supports ForwardRef in React', () => { const ref = createRef(); render(_jsx(FieldSet, { legend: "Test", ref: ref })); const component = screen.getByRole('group', { name: 'Test' }); expect(ref.current).toBe(component); }); it('renders the provided hint text after the legend', () => { render(_jsx(FieldSet, { hint: "hint text", legend: "Legend" })); const component = screen.getByRole('group', { name: 'Legend (hint text)' }); expect(component).toBeInTheDocument(); }); it('renders the default hint text after the legend', () => { render(_jsx(FieldSet, { legend: "Legend", optional: true })); const component = screen.getByRole('group', { name: 'Legend (niet verplicht)' }); expect(component).toBeInTheDocument(); }); it('renders the provided hint text after the legend when both `optional` and `hint` props are used', () => { render(_jsx(FieldSet, { hint: "not required", legend: "Legend", optional: true })); const component = screen.getByRole('group', { name: 'Legend (not required)' }); expect(component).toBeInTheDocument(); }); it('renders the provided hint text after the legend while `optional` is set to `false`', () => { render(_jsx(FieldSet, { hint: "required", legend: "Legend", optional: false })); const component = screen.getByRole('group', { name: 'Legend (required)' }); expect(component).toBeInTheDocument(); }); it('renders a heading in the legend', () => { render(_jsx(FieldSet, { legend: "Legend", legendIsPageHeading: true })); const heading = screen.getByRole('heading', { name: 'Legend', }); expect(heading).toBeInTheDocument(); }); it('renders the correct class names when `inFieldSet` is true', () => { const { container } = render(_jsx(FieldSet, { inFieldSet: true, legend: "Test", optional: true })); const legend = container.querySelector('legend'); const hint = legend?.querySelector('.ams-hint'); expect(legend).toHaveClass('ams-field-set__legend--in-fieldset'); expect(hint).toHaveClass('ams-hint--in-fieldset'); }); it('passes additional props', () => { render(_jsx(FieldSet, { "aria-hidden": "false", "data-test": "data-test", id: "id", legend: "Test" })); const component = screen.getByRole('group', { name: 'Test' }); expect(component).toHaveAttribute('aria-hidden', 'false'); expect(component).toHaveAttribute('id', 'id'); expect(component).toHaveAttribute('data-test', 'data-test'); }); });