|
1 | 1 | import React from 'react' |
2 | 2 | import { render } from '@testing-library/react' |
3 | | -import { SelectField } from '../' |
| 3 | +import userEvent from '@testing-library/user-event' |
| 4 | +import { Select, SelectField } from '../' |
4 | 5 | import { mockRef } from '../../test/utils' |
5 | 6 |
|
6 | | -const makeSelectFieldFixture = (props = {}) => <SelectField data-testid="select-field" label="SelectField" {...props} /> |
| 7 | +function makeSelectFixture(props = {}) { |
| 8 | + return ( |
| 9 | + <Select data-testid="select" defaultValue="foo" name="select" {...props}> |
| 10 | + <option value="foo">Foo</option> |
| 11 | + <option value="bar">Bar</option> |
| 12 | + </Select> |
| 13 | + ) |
| 14 | +} |
| 15 | + |
| 16 | +function makeSelectFieldFixture(props = {}) { |
| 17 | + return ( |
| 18 | + <SelectField data-testid="select" defaultValue="foo" label="Select" {...props}> |
| 19 | + <option value="foo">Foo</option> |
| 20 | + <option value="bar">Bar</option> |
| 21 | + </SelectField> |
| 22 | + ) |
| 23 | +} |
| 24 | + |
| 25 | +describe('Select', () => { |
| 26 | + it('Should render without crashing', () => { |
| 27 | + expect(() => render(makeSelectFixture())).not.toThrow() |
| 28 | + }) |
| 29 | + |
| 30 | + it('Should set an invalid state if `isInvalid` is `true`', () => { |
| 31 | + const { container } = render(makeSelectFixture({ isInvalid: true })) |
| 32 | + const input = container.querySelector('select') |
| 33 | + expect(input).toHaveAttribute('aria-invalid', 'true') |
| 34 | + }) |
| 35 | + |
| 36 | + it('Should not be interactive if `disabled` is passed in', () => { |
| 37 | + const { container } = render(makeSelectFixture({ disabled: true })) |
| 38 | + const select = container.querySelector('select') |
| 39 | + |
| 40 | + expect(document.body).toHaveFocus() |
| 41 | + userEvent.tab() |
| 42 | + expect(select).not.toHaveFocus() |
| 43 | + }) |
| 44 | +}) |
7 | 45 |
|
8 | 46 | describe('SelectField', () => { |
| 47 | + it('Should render without crashing', () => { |
| 48 | + expect(() => render(makeSelectFieldFixture())).not.toThrow() |
| 49 | + }) |
| 50 | + |
9 | 51 | it('should forward ref to underlying <select />', () => { |
10 | 52 | const ref = mockRef() |
11 | 53 |
|
12 | 54 | render(makeSelectFieldFixture({ ref })) |
13 | 55 |
|
14 | 56 | expect(ref.current).toBeInstanceOf(HTMLSelectElement) |
15 | 57 | }) |
| 58 | + |
| 59 | + it('Should have expected accessible name when `label` prop passed in', () => { |
| 60 | + const { container, getByLabelText } = render(makeSelectFieldFixture()) |
| 61 | + const select = container.querySelector('select') |
| 62 | + expect(getByLabelText('Select')).toBeInTheDocument() |
| 63 | + expect(select).toHaveAccessibleName('Select') |
| 64 | + }) |
| 65 | + |
| 66 | + it('Should add hint text to accessible description when `hint` prop provided', () => { |
| 67 | + const { container, getByText } = render(makeSelectFieldFixture({ hint: 'Some description.' })) |
| 68 | + expect(getByText('Some description.')).toBeInTheDocument() |
| 69 | + expect(container.querySelector('select')).toHaveAccessibleDescription('Some description.') |
| 70 | + }) |
| 71 | + |
| 72 | + it('Should render an astrix when `required` is passed in', () => { |
| 73 | + const { getByTitle } = render(makeSelectFieldFixture({ required: true })) |
| 74 | + expect(getByTitle('This field is required.')).toBeInTheDocument() |
| 75 | + }) |
| 76 | + |
| 77 | + it('Should render a `validationMessage` when passed in', () => { |
| 78 | + const { container, getByText } = render(makeSelectFieldFixture({ validationMessage: 'Please choose a value.' })) |
| 79 | + expect(getByText('Please choose a value.')).toBeInTheDocument() |
| 80 | + expect(container.querySelector('select')).toHaveAccessibleDescription('Please choose a value.') |
| 81 | + }) |
| 82 | + |
| 83 | + it('Should correctly compose an accessible description from multiple hints', () => { |
| 84 | + const { container } = render(makeSelectFieldFixture({ hint: 'Am hint.', validationMessage: 'Try again.' })) |
| 85 | + |
| 86 | + expect(container.querySelector('select')).toHaveAccessibleDescription('Try again. Am hint.') |
| 87 | + }) |
16 | 88 | }) |
0 commit comments