UNPKG

@amsterdam/design-system-react

Version:

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

93 lines (92 loc) 3.92 kB
import { jsx as _jsx } from "react/jsx-runtime"; /** * @license EUPL-1.2+ * Copyright Gemeente Amsterdam */ import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { createRef, useState } from 'react'; import { TextInput, textInputTypes } from './TextInput'; import '@testing-library/jest-dom'; describe('Text input', () => { it('renders', () => { render(_jsx(TextInput, {})); const component = screen.getByRole('textbox'); expect(component).toBeInTheDocument(); expect(component).toBeVisible(); }); it('renders a design system BEM class name', () => { render(_jsx(TextInput, {})); const component = screen.getByRole('textbox'); expect(component).toHaveClass('ams-text-input'); }); it('renders an extra class name', () => { render(_jsx(TextInput, { className: "extra" })); const component = screen.getByRole('textbox'); expect(component).toHaveClass('ams-text-input extra'); }); it('should be working in a controlled state', async () => { function ControlledComponent() { const [value, setValue] = useState('Hello'); return _jsx(TextInput, { onChange: (e) => setValue(e.target.value), value: value }); } render(_jsx(ControlledComponent, {})); const componentText = screen.getByDisplayValue('Hello'); expect(componentText).toBeInTheDocument(); const component = screen.getByRole('textbox'); if (component) { await userEvent.type(component, ', World!'); } const newComponentText = screen.getByDisplayValue('Hello, World!'); expect(newComponentText).toBeInTheDocument(); }); it('should not update the value when disabled', async () => { render(_jsx(TextInput, { defaultValue: "Hello", disabled: true })); const component = screen.getByRole('textbox'); if (component) { await userEvent.type(component, ', World!'); } expect(component).toHaveValue('Hello'); }); it('supports ForwardRef in React', () => { const ref = createRef(); render(_jsx(TextInput, { ref: ref })); const component = screen.getByRole('textbox'); expect(ref.current).toBe(component); }); it('renders bidirectional by default using `dir="auto"`', () => { render(_jsx(TextInput, {})); const component = screen.getByRole('textbox'); expect(component).toHaveAttribute('dir', 'auto'); }); it('renders left-to-right by using `dir="ltr"`', () => { render(_jsx(TextInput, { dir: "ltr" })); const component = screen.getByRole('textbox'); expect(component).toHaveAttribute('dir', 'ltr'); }); describe('Invalid state', () => { it('is not invalid by default', () => { render(_jsx(TextInput, {})); const component = screen.getByRole('textbox'); expect(component).not.toBeInvalid(); }); it('can have an invalid state', () => { render(_jsx(TextInput, { invalid: true })); const component = screen.getByRole('textbox'); expect(component).toHaveAttribute('aria-invalid'); expect(component).toBeInvalid(); }); it('omits non-essential invalid attributes when not invalid', () => { render(_jsx(TextInput, { invalid: false })); const component = screen.getByRole('textbox'); expect(component).not.toHaveAttribute('aria-invalid'); }); }); describe('Type', () => { textInputTypes.map((type) => it(`sets the ‘${type}’ type`, () => { render(_jsx(TextInput, { type: type })); const component = screen.getByRole('textbox'); expect(component).toHaveAttribute('type', type); })); }); });