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