|
| 1 | +import {render} from './helpers/test-utils' |
| 2 | + |
| 3 | +describe('.toBePartiallyPressed', () => { |
| 4 | + test('handles button element', () => { |
| 5 | + const {queryByTestId} = render(` |
| 6 | + <button data-testid="button-partially-pressed" aria-pressed="mixed" /> |
| 7 | + <button data-testid="button-not-pressed" aria-pressed="true" /> |
| 8 | + <button data-testid="button-pressed" aria-pressed="false" /> |
| 9 | + <button data-testid="button" /> |
| 10 | + `) |
| 11 | + |
| 12 | + expect(queryByTestId('button-partially-pressed')).toBePartiallyPressed() |
| 13 | + expect(queryByTestId('button-not-pressed')).not.toBePartiallyPressed() |
| 14 | + expect(queryByTestId('button-pressed')).not.toBePartiallyPressed() |
| 15 | + expect(queryByTestId('button')).not.toBePartiallyPressed() |
| 16 | + }) |
| 17 | + |
| 18 | + test('handles element with role="button"', () => { |
| 19 | + const {queryByTestId} = render(` |
| 20 | + <span role="button" aria-pressed="mixed" data-testid="button-partially-pressed" /> |
| 21 | + <span role="button" aria-pressed="true" data-testid="button-not-pressed" /> |
| 22 | + <span role="button" aria-pressed="false" data-testid="button-pressed" /> |
| 23 | + <span role="button" data-testid="button" /> |
| 24 | + `) |
| 25 | + |
| 26 | + expect(queryByTestId('button-partially-pressed')).toBePartiallyPressed() |
| 27 | + expect(queryByTestId('button-not-pressed')).not.toBePartiallyPressed() |
| 28 | + expect(queryByTestId('button-pressed')).not.toBePartiallyPressed() |
| 29 | + expect(queryByTestId('button')).not.toBePartiallyPressed() |
| 30 | + }) |
| 31 | + |
| 32 | + test('handles input with button type', () => { |
| 33 | + const {queryByTestId} = render(` |
| 34 | + <input type="button" aria-pressed="mixed" data-testid="button-partially-pressed" /> |
| 35 | + <input type="button" aria-pressed="true" data-testid="button-not-pressed" /> |
| 36 | + <input type="button" aria-pressed="false" data-testid="button-pressed" /> |
| 37 | + <input type="button" data-testid="button" /> |
| 38 | + `) |
| 39 | + |
| 40 | + expect(queryByTestId('button-partially-pressed')).toBePartiallyPressed() |
| 41 | + expect(queryByTestId('button-not-pressed')).not.toBePartiallyPressed() |
| 42 | + expect(queryByTestId('button-pressed')).not.toBePartiallyPressed() |
| 43 | + expect(queryByTestId('button')).not.toBePartiallyPressed() |
| 44 | + }) |
| 45 | + |
| 46 | + test('throw an error when pressable element is not partially pressed but expected to be', () => { |
| 47 | + const {queryByTestId} = render(` |
| 48 | + <button data-testid="button" aria-pressed="true" /> |
| 49 | + `) |
| 50 | + |
| 51 | + expect(() => expect(queryByTestId('button')).toBePartiallyPressed()) |
| 52 | + .toThrowErrorMatchingInlineSnapshot(` |
| 53 | + <dim>expect(</><red>element</><dim>).toBePartiallyPressed()</> |
| 54 | + |
| 55 | + Expected element to have: |
| 56 | + <green> aria-pressed="mixed"</> |
| 57 | + Received: |
| 58 | + <red> aria-pressed="true"</> |
| 59 | + `) |
| 60 | + }) |
| 61 | + |
| 62 | + test('throw an error when pressable element is partially pressed but expected not to be', () => { |
| 63 | + const {queryByTestId} = render(` |
| 64 | + <button data-testid="button" aria-pressed="mixed" /> |
| 65 | + `) |
| 66 | + |
| 67 | + expect(() => expect(queryByTestId('button')).not.toBePartiallyPressed()) |
| 68 | + .toThrowErrorMatchingInlineSnapshot(` |
| 69 | + <dim>expect(</><red>element</><dim>).not.toBePartiallyPressed()</> |
| 70 | +
|
| 71 | + Expected element not to have: |
| 72 | + <green> aria-pressed="mixed"</> |
| 73 | + Received: |
| 74 | + <red> aria-pressed="mixed"</> |
| 75 | + `) |
| 76 | + }) |
| 77 | + |
| 78 | + test('throw an error when pressable element has invalid aria-pressed attribute', () => { |
| 79 | + const {queryByTestId} = render(` |
| 80 | + <button data-testid="button" aria-pressed="invalid" /> |
| 81 | + `) |
| 82 | + |
| 83 | + expect(() => |
| 84 | + expect(queryByTestId('button')).toBePartiallyPressed(), |
| 85 | + ).toThrowErrorMatchingInlineSnapshot( |
| 86 | + `Only button or input with type="button" or element with role="button" and a valid aria-pressed attribute can be used with .toBePartiallyPressed()`, |
| 87 | + ) |
| 88 | + }) |
| 89 | + |
| 90 | + test('throws when element is not a button', () => { |
| 91 | + const {queryByTestId} = render(` |
| 92 | + <div data-testid="div" aria-pressed="mixed" /> |
| 93 | + `) |
| 94 | + |
| 95 | + expect(() => |
| 96 | + expect(queryByTestId('div')).toBePartiallyPressed(), |
| 97 | + ).toThrowErrorMatchingInlineSnapshot( |
| 98 | + `Only button or input with type="button" or element with role="button" and a valid aria-pressed attribute can be used with .toBePartiallyPressed()`, |
| 99 | + ) |
| 100 | + }) |
| 101 | + |
| 102 | + test('throws when element do not have aria-pressed attribute', () => { |
| 103 | + const {queryByTestId} = render(`<span data-testid="span" />`) |
| 104 | + |
| 105 | + expect(() => |
| 106 | + expect(queryByTestId('span')).toBePartiallyPressed(), |
| 107 | + ).toThrowErrorMatchingInlineSnapshot( |
| 108 | + `Only button or input with type="button" or element with role="button" and a valid aria-pressed attribute can be used with .toBePartiallyPressed()`, |
| 109 | + ) |
| 110 | + }) |
| 111 | +}) |
0 commit comments