|
| 1 | +import { withBrowser } from 'pleasantest'; |
| 2 | + |
| 3 | +test( |
| 4 | + 'toHaveAccessibleName', |
| 5 | + withBrowser(async ({ screen, utils }) => { |
| 6 | + await utils.injectHTML(` |
| 7 | + <img data-testid="img-alt" src="" alt="Test alt" /> |
| 8 | + <img data-testid="img-empty-alt" src="" alt="" /> |
| 9 | + <svg data-testid="svg-title"><title>Test title</title></svg> |
| 10 | + <button data-testid="button-img-alt"><img src="" alt="Test" /></button> |
| 11 | + <p><img data-testid="img-paragraph" src="" alt="" /> Test content</p> |
| 12 | + <button data-testid="svg-button"><svg><title>Test</title></svg></p> |
| 13 | + <div><svg data-testid="svg-without-title"></svg></div> |
| 14 | + <input data-testid="input-title" title="test" /> |
| 15 | + `); |
| 16 | + |
| 17 | + await expect(await screen.getByTestId('img-alt')).toHaveAccessibleName( |
| 18 | + 'Test alt', |
| 19 | + ); |
| 20 | + |
| 21 | + await expect(await screen.getByTestId('img-alt')).not.toHaveAccessibleName( |
| 22 | + 'not test alt', |
| 23 | + ); |
| 24 | + await expect( |
| 25 | + expect(await screen.getByTestId('img-alt')).toHaveAccessibleName( |
| 26 | + 'not test alt', |
| 27 | + ), |
| 28 | + ).rejects.toThrowErrorMatchingInlineSnapshot(` |
| 29 | + "[2mexpect([22m[31melement[39m[2m).toHaveAccessibleName()[22m |
| 30 | +
|
| 31 | + Expected element to have accessible name: |
| 32 | + [32m not test alt[39m |
| 33 | + Received: |
| 34 | + [31m Test alt[39m" |
| 35 | + `); |
| 36 | + |
| 37 | + await expect( |
| 38 | + expect(await screen.getByTestId('img-empty-alt')).toHaveAccessibleName(), |
| 39 | + ).rejects.toThrowErrorMatchingInlineSnapshot(` |
| 40 | + "[2mexpect([22m[31melement[39m[2m).toHaveAccessibleName()[22m |
| 41 | +
|
| 42 | + Expected element to have accessible name: |
| 43 | + [32m null[39m |
| 44 | + Received: |
| 45 | + " |
| 46 | + `); |
| 47 | + |
| 48 | + await expect( |
| 49 | + await screen.getByTestId('img-empty-alt'), |
| 50 | + ).not.toHaveAccessibleName(); |
| 51 | + await expect( |
| 52 | + expect(await screen.getByTestId('img-empty-alt')).toHaveAccessibleName(), |
| 53 | + ).rejects.toThrowErrorMatchingInlineSnapshot(` |
| 54 | + "[2mexpect([22m[31melement[39m[2m).toHaveAccessibleName()[22m |
| 55 | +
|
| 56 | + Expected element to have accessible name: |
| 57 | + [32m null[39m |
| 58 | + Received: |
| 59 | + " |
| 60 | + `); |
| 61 | + |
| 62 | + await expect(await screen.getByTestId('svg-title')).toHaveAccessibleName( |
| 63 | + 'Test title', |
| 64 | + ); |
| 65 | + |
| 66 | + await expect( |
| 67 | + await screen.getByTestId('button-img-alt'), |
| 68 | + ).toHaveAccessibleName(); |
| 69 | + |
| 70 | + await expect( |
| 71 | + await screen.getByTestId('img-paragraph'), |
| 72 | + ).not.toHaveAccessibleName(); |
| 73 | + |
| 74 | + await expect(await screen.getByTestId('svg-button')).toHaveAccessibleName(); |
| 75 | + |
| 76 | + await expect( |
| 77 | + await screen.getByTestId('svg-without-title'), |
| 78 | + ).not.toHaveAccessibleName(); |
| 79 | + |
| 80 | + await expect( |
| 81 | + await screen.getByTestId('input-title'), |
| 82 | + ).toHaveAccessibleName(); |
| 83 | + }), |
| 84 | +); |
0 commit comments