|
| 1 | +import {render} from './helpers/test-utils' |
| 2 | + |
| 3 | +describe('.toHaveRole', () => { |
| 4 | + it('matches implicit role', () => { |
| 5 | + const {queryByTestId} = render(` |
| 6 | + <div> |
| 7 | + <button data-testid="continue-button">Continue</button> |
| 8 | + </div> |
| 9 | + `) |
| 10 | + |
| 11 | + const continueButton = queryByTestId('continue-button') |
| 12 | + |
| 13 | + expect(continueButton).not.toHaveRole('listitem') |
| 14 | + expect(continueButton).toHaveRole('button') |
| 15 | + |
| 16 | + expect(() => { |
| 17 | + expect(continueButton).toHaveRole('listitem') |
| 18 | + }).toThrow(/expected element to have role/i) |
| 19 | + expect(() => { |
| 20 | + expect(continueButton).not.toHaveRole('button') |
| 21 | + }).toThrow(/expected element not to have role/i) |
| 22 | + }) |
| 23 | + |
| 24 | + it('matches explicit role', () => { |
| 25 | + const {queryByTestId} = render(` |
| 26 | + <div> |
| 27 | + <div role="button" data-testid="continue-button">Continue</div> |
| 28 | + </div> |
| 29 | + `) |
| 30 | + |
| 31 | + const continueButton = queryByTestId('continue-button') |
| 32 | + |
| 33 | + expect(continueButton).not.toHaveRole('listitem') |
| 34 | + expect(continueButton).toHaveRole('button') |
| 35 | + |
| 36 | + expect(() => { |
| 37 | + expect(continueButton).toHaveRole('listitem') |
| 38 | + }).toThrow(/expected element to have role/i) |
| 39 | + expect(() => { |
| 40 | + expect(continueButton).not.toHaveRole('button') |
| 41 | + }).toThrow(/expected element not to have role/i) |
| 42 | + }) |
| 43 | + |
| 44 | + it('matches multiple explicit roles', () => { |
| 45 | + const {queryByTestId} = render(` |
| 46 | + <div> |
| 47 | + <div role="button switch" data-testid="continue-button">Continue</div> |
| 48 | + </div> |
| 49 | + `) |
| 50 | + |
| 51 | + const continueButton = queryByTestId('continue-button') |
| 52 | + |
| 53 | + expect(continueButton).not.toHaveRole('listitem') |
| 54 | + expect(continueButton).toHaveRole('button') |
| 55 | + expect(continueButton).toHaveRole('switch') |
| 56 | + |
| 57 | + expect(() => { |
| 58 | + expect(continueButton).toHaveRole('listitem') |
| 59 | + }).toThrow(/expected element to have role/i) |
| 60 | + expect(() => { |
| 61 | + expect(continueButton).not.toHaveRole('button') |
| 62 | + }).toThrow(/expected element not to have role/i) |
| 63 | + expect(() => { |
| 64 | + expect(continueButton).not.toHaveRole('switch') |
| 65 | + }).toThrow(/expected element not to have role/i) |
| 66 | + }) |
| 67 | + |
| 68 | + // At this point, we might be testing the details of getImplicitAriaRoles, but |
| 69 | + // it's good to have a gut check |
| 70 | + it('handles implicit roles with multiple conditions', () => { |
| 71 | + const {queryByTestId} = render(` |
| 72 | + <div> |
| 73 | + <a href="/about" data-testid="link-valid">Actually a valid link</a> |
| 74 | + <a data-testid="link-invalid">Not a valid link (missing href)</a> |
| 75 | + </div> |
| 76 | + `) |
| 77 | + |
| 78 | + const validLink = queryByTestId('link-valid') |
| 79 | + const invalidLink = queryByTestId('link-invalid') |
| 80 | + |
| 81 | + // valid link has role 'link' |
| 82 | + expect(validLink).not.toHaveRole('listitem') |
| 83 | + expect(validLink).toHaveRole('link') |
| 84 | + |
| 85 | + expect(() => { |
| 86 | + expect(validLink).toHaveRole('listitem') |
| 87 | + }).toThrow(/expected element to have role/i) |
| 88 | + expect(() => { |
| 89 | + expect(validLink).not.toHaveRole('link') |
| 90 | + }).toThrow(/expected element not to have role/i) |
| 91 | + |
| 92 | + // invalid link has role 'generic' |
| 93 | + expect(invalidLink).not.toHaveRole('listitem') |
| 94 | + expect(invalidLink).not.toHaveRole('link') |
| 95 | + expect(invalidLink).toHaveRole('generic') |
| 96 | + |
| 97 | + expect(() => { |
| 98 | + expect(invalidLink).toHaveRole('listitem') |
| 99 | + }).toThrow(/expected element to have role/i) |
| 100 | + expect(() => { |
| 101 | + expect(invalidLink).toHaveRole('link') |
| 102 | + }).toThrow(/expected element to have role/i) |
| 103 | + expect(() => { |
| 104 | + expect(invalidLink).not.toHaveRole('generic') |
| 105 | + }).toThrow(/expected element not to have role/i) |
| 106 | + }) |
| 107 | +}) |
0 commit comments