|
| 1 | +import React from 'react' |
| 2 | +import {render, cleanup} from '@testing-library/react' |
| 3 | +import userEvent from '@testing-library/user-event' |
| 4 | +import '@testing-library/jest-dom' |
| 5 | +import {toHaveNoViolations} from 'jest-axe' |
| 6 | + |
| 7 | +import {Checkbox} from '../Checkbox' |
| 8 | + |
| 9 | +expect.extend(toHaveNoViolations) |
| 10 | + |
| 11 | +describe('Checkbox', () => { |
| 12 | + afterEach(cleanup) |
| 13 | + |
| 14 | + it('renders a checkbox correctly into the document', () => { |
| 15 | + const {getByRole} = render(<Checkbox value="one" defaultChecked />) |
| 16 | + |
| 17 | + const checkbox = getByRole('checkbox') |
| 18 | + expect(checkbox).toBeInTheDocument() |
| 19 | + expect(checkbox).toBeChecked() |
| 20 | + expect(checkbox).toHaveAttribute('value', 'one') |
| 21 | + }) |
| 22 | + |
| 23 | + it('toggles the checkbox when clicked', async () => { |
| 24 | + const user = userEvent.setup() |
| 25 | + |
| 26 | + const {getByRole} = render(<Checkbox value="one" defaultChecked />) |
| 27 | + |
| 28 | + const checkbox = getByRole('checkbox') |
| 29 | + |
| 30 | + expect(checkbox).toBeChecked() |
| 31 | + |
| 32 | + await user.click(checkbox) |
| 33 | + expect(checkbox).not.toBeChecked() |
| 34 | + |
| 35 | + await user.click(checkbox) |
| 36 | + expect(checkbox).toBeChecked() |
| 37 | + }) |
| 38 | + |
| 39 | + it('toggles the checkbox when the associated span is clicked', async () => { |
| 40 | + const user = userEvent.setup() |
| 41 | + |
| 42 | + const {getByRole, container} = render(<Checkbox value="one" defaultChecked />) |
| 43 | + |
| 44 | + const checkbox = getByRole('checkbox') |
| 45 | + const span = container.querySelector('span.Checkbox') as HTMLSpanElement |
| 46 | + |
| 47 | + expect(span).toBeInTheDocument() |
| 48 | + expect(checkbox).toBeChecked() |
| 49 | + |
| 50 | + await user.click(span) |
| 51 | + expect(checkbox).not.toBeChecked() |
| 52 | + |
| 53 | + await user.click(span) |
| 54 | + expect(checkbox).toBeChecked() |
| 55 | + }) |
| 56 | + |
| 57 | + it('toggles the checkbox when the associated span is clicked and a functional ref is passed', async () => { |
| 58 | + const user = userEvent.setup() |
| 59 | + const mockRef = jest.fn() |
| 60 | + |
| 61 | + const {getByRole, container} = render(<Checkbox value="one" defaultChecked ref={mockRef} />) |
| 62 | + |
| 63 | + const checkbox = getByRole('checkbox') |
| 64 | + const span = container.querySelector('span.Checkbox') as HTMLSpanElement |
| 65 | + |
| 66 | + expect(mockRef).toHaveBeenCalledWith(checkbox) |
| 67 | + expect(checkbox).toBeChecked() |
| 68 | + |
| 69 | + await user.click(span) |
| 70 | + expect(checkbox).not.toBeChecked() |
| 71 | + |
| 72 | + await user.click(span) |
| 73 | + expect(checkbox).toBeChecked() |
| 74 | + }) |
| 75 | + |
| 76 | + it('remains clickable when passing an arbitrary function as a ref', async () => { |
| 77 | + // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 78 | + const {getByRole} = render(<Checkbox value="test" ref={() => {}} />) |
| 79 | + |
| 80 | + const checkbox = getByRole('checkbox') |
| 81 | + expect(checkbox).not.toBeChecked() |
| 82 | + |
| 83 | + const user = userEvent.setup() |
| 84 | + await user.click(checkbox) |
| 85 | + expect(checkbox).toBeChecked() |
| 86 | + }) |
| 87 | + |
| 88 | + it('supports a standard RefObject', async () => { |
| 89 | + const user = userEvent.setup() |
| 90 | + const refObject = React.createRef<HTMLInputElement>() |
| 91 | + |
| 92 | + const {getByRole} = render(<Checkbox value="test" ref={refObject} />) |
| 93 | + |
| 94 | + const checkbox = getByRole('checkbox') |
| 95 | + |
| 96 | + expect(refObject.current).toBe(checkbox) |
| 97 | + expect(checkbox).not.toBeChecked() |
| 98 | + |
| 99 | + if (refObject.current) { |
| 100 | + await user.click(refObject.current) |
| 101 | + } |
| 102 | + |
| 103 | + expect(checkbox).toBeChecked() |
| 104 | + expect(refObject.current?.checked).toBe(true) |
| 105 | + }) |
| 106 | +}) |
0 commit comments