|
| 1 | +import {render} from './helpers/test-utils' |
| 2 | + |
| 3 | +describe('.toBePartiallyChecked', () => { |
| 4 | + test('handles input checkbox with aria-checked', () => { |
| 5 | + const {queryByTestId} = render(` |
| 6 | + <input type="checkbox" aria-checked="mixed" data-testid="checkbox-mixed" /> |
| 7 | + <input type="checkbox" checked data-testid="checkbox-checked" /> |
| 8 | + <input type="checkbox" data-testid="checkbox-unchecked" /> |
| 9 | + `) |
| 10 | + |
| 11 | + expect(queryByTestId('checkbox-mixed')).toBePartiallyChecked() |
| 12 | + expect(queryByTestId('checkbox-checked')).not.toBePartiallyChecked() |
| 13 | + expect(queryByTestId('checkbox-unchecked')).not.toBePartiallyChecked() |
| 14 | + }) |
| 15 | + |
| 16 | + test('handles input checkbox set as indeterminate', () => { |
| 17 | + const {queryByTestId} = render(` |
| 18 | + <input type="checkbox" data-testid="checkbox-mixed" /> |
| 19 | + <input type="checkbox" checked data-testid="checkbox-checked" /> |
| 20 | + <input type="checkbox" data-testid="checkbox-unchecked" /> |
| 21 | + `) |
| 22 | + |
| 23 | + queryByTestId('checkbox-mixed').indeterminate = true |
| 24 | + |
| 25 | + expect(queryByTestId('checkbox-mixed')).toBePartiallyChecked() |
| 26 | + expect(queryByTestId('checkbox-checked')).not.toBePartiallyChecked() |
| 27 | + expect(queryByTestId('checkbox-unchecked')).not.toBePartiallyChecked() |
| 28 | + }) |
| 29 | + |
| 30 | + test('handles element with role="checkbox"', () => { |
| 31 | + const {queryByTestId} = render(` |
| 32 | + <div role="checkbox" aria-checked="mixed" data-testid="aria-checkbox-mixed" /> |
| 33 | + <div role="checkbox" aria-checked="true" data-testid="aria-checkbox-checked" /> |
| 34 | + <div role="checkbox" aria-checked="false" data-testid="aria-checkbox-unchecked" /> |
| 35 | + `) |
| 36 | + |
| 37 | + expect(queryByTestId('aria-checkbox-mixed')).toBePartiallyChecked() |
| 38 | + expect(queryByTestId('aria-checkbox-checked')).not.toBePartiallyChecked() |
| 39 | + expect(queryByTestId('aria-checkbox-unchecked')).not.toBePartiallyChecked() |
| 40 | + }) |
| 41 | + |
| 42 | + test('throws when input checkbox is mixed but expected not to be', () => { |
| 43 | + const {queryByTestId} = render( |
| 44 | + `<input type="checkbox" aria-checked="mixed" data-testid="checkbox-mixed" />`, |
| 45 | + ) |
| 46 | + |
| 47 | + expect(() => |
| 48 | + expect(queryByTestId('checkbox-mixed')).not.toBePartiallyChecked(), |
| 49 | + ).toThrowError() |
| 50 | + }) |
| 51 | + |
| 52 | + test('throws when input checkbox is indeterminate but expected not to be', () => { |
| 53 | + const {queryByTestId} = render( |
| 54 | + `<input type="checkbox" data-testid="checkbox-mixed" />`, |
| 55 | + ) |
| 56 | + |
| 57 | + queryByTestId('checkbox-mixed').indeterminate = true |
| 58 | + |
| 59 | + expect(() => |
| 60 | + expect(queryByTestId('input-mixed')).not.toBePartiallyChecked(), |
| 61 | + ).toThrowError() |
| 62 | + }) |
| 63 | + |
| 64 | + test('throws when input checkbox is not checked but expected to be', () => { |
| 65 | + const {queryByTestId} = render( |
| 66 | + `<input type="checkbox" data-testid="checkbox-empty" />`, |
| 67 | + ) |
| 68 | + |
| 69 | + expect(() => |
| 70 | + expect(queryByTestId('checkbox-empty')).toBePartiallyChecked(), |
| 71 | + ).toThrowError() |
| 72 | + }) |
| 73 | + |
| 74 | + test('throws when element with role="checkbox" is partially checked but expected not to be', () => { |
| 75 | + const {queryByTestId} = render( |
| 76 | + `<div role="checkbox" aria-checked="mixed" data-testid="aria-checkbox-mixed" />`, |
| 77 | + ) |
| 78 | + |
| 79 | + expect(() => |
| 80 | + expect(queryByTestId('aria-checkbox-mixed')).not.toBePartiallyChecked(), |
| 81 | + ).toThrowError() |
| 82 | + }) |
| 83 | + |
| 84 | + test('throws when element with role="checkbox" is checked but expected to be partially checked', () => { |
| 85 | + const {queryByTestId} = render( |
| 86 | + `<div role="checkbox" aria-checked="true" data-testid="aria-checkbox-checked" />`, |
| 87 | + ) |
| 88 | + |
| 89 | + expect(() => |
| 90 | + expect(queryByTestId('aria-checkbox-checked')).toBePartiallyChecked(), |
| 91 | + ).toThrowError() |
| 92 | + }) |
| 93 | + |
| 94 | + test('throws when element with role="checkbox" is not checked but expected to be', () => { |
| 95 | + const {queryByTestId} = render( |
| 96 | + `<div role="checkbox" aria-checked="false" data-testid="aria-checkbox" />`, |
| 97 | + ) |
| 98 | + |
| 99 | + expect(() => |
| 100 | + expect(queryByTestId('aria-checkbox')).toBePartiallyChecked(), |
| 101 | + ).toThrowError() |
| 102 | + }) |
| 103 | + |
| 104 | + test('throws when element with role="checkbox" has an invalid aria-checked attribute', () => { |
| 105 | + const {queryByTestId} = render( |
| 106 | + `<div role="checkbox" aria-checked="something" data-testid="aria-checkbox-invalid" />`, |
| 107 | + ) |
| 108 | + |
| 109 | + expect(() => |
| 110 | + expect(queryByTestId('aria-checkbox-invalid')).toBePartiallyChecked(), |
| 111 | + ).toThrowError() |
| 112 | + }) |
| 113 | + |
| 114 | + test('throws when the element is not a checkbox', () => { |
| 115 | + const {queryByTestId} = render(`<select data-testid="select"></select>`) |
| 116 | + expect(() => |
| 117 | + expect(queryByTestId('select')).toBePartiallyChecked(), |
| 118 | + ).toThrowError( |
| 119 | + 'only inputs with type="checkbox" or elements with role="checkbox" and a valid aria-checked attribute can be used with .toBePartiallyChecked(). Use .toHaveValue() instead', |
| 120 | + ) |
| 121 | + }) |
| 122 | +}) |
0 commit comments