|
| 1 | +import {render} from './helpers/test-utils' |
| 2 | + |
| 3 | +describe('.toHaveDescription', () => { |
| 4 | + test('handles positive test cases', () => { |
| 5 | + const {queryByTestId} = render(` |
| 6 | + <div id="description">The description</div> |
| 7 | +
|
| 8 | + <div data-testid="single" aria-describedby="description"></div> |
| 9 | + <div data-testid="invalid_id" aria-describedby="invalid"></div> |
| 10 | + <div data-testid="without"></div> |
| 11 | + `) |
| 12 | + |
| 13 | + expect(queryByTestId('single')).toHaveDescription('The description') |
| 14 | + expect(queryByTestId('single')).toHaveDescription( |
| 15 | + expect.stringContaining('The'), |
| 16 | + ) |
| 17 | + expect(queryByTestId('single')).toHaveDescription(/The/) |
| 18 | + expect(queryByTestId('single')).toHaveDescription( |
| 19 | + expect.stringMatching(/The/), |
| 20 | + ) |
| 21 | + expect(queryByTestId('single')).toHaveDescription(/description/) |
| 22 | + expect(queryByTestId('single')).not.toHaveDescription('Something else') |
| 23 | + expect(queryByTestId('single')).not.toHaveDescription('The') |
| 24 | + |
| 25 | + expect(queryByTestId('invalid_id')).not.toHaveDescription() |
| 26 | + expect(queryByTestId('invalid_id')).toHaveDescription('') |
| 27 | + |
| 28 | + expect(queryByTestId('without')).not.toHaveDescription() |
| 29 | + expect(queryByTestId('without')).toHaveDescription('') |
| 30 | + }) |
| 31 | + |
| 32 | + test('handles multiple ids', () => { |
| 33 | + const {queryByTestId} = render(` |
| 34 | + <div id="first">First description</div> |
| 35 | + <div id="second">Second description</div> |
| 36 | + <div id="third">Third description</div> |
| 37 | +
|
| 38 | + <div data-testid="multiple" aria-describedby="first second third"></div> |
| 39 | + `) |
| 40 | + |
| 41 | + expect(queryByTestId('multiple')).toHaveDescription( |
| 42 | + 'First description Second description Third description', |
| 43 | + ) |
| 44 | + expect(queryByTestId('multiple')).toHaveDescription( |
| 45 | + /Second description Third/, |
| 46 | + ) |
| 47 | + expect(queryByTestId('multiple')).toHaveDescription( |
| 48 | + expect.stringContaining('Second description Third'), |
| 49 | + ) |
| 50 | + expect(queryByTestId('multiple')).toHaveDescription( |
| 51 | + expect.stringMatching(/Second description Third/), |
| 52 | + ) |
| 53 | + expect(queryByTestId('multiple')).not.toHaveDescription('Something else') |
| 54 | + expect(queryByTestId('multiple')).not.toHaveDescription('First') |
| 55 | + }) |
| 56 | + |
| 57 | + test('handles negative test cases', () => { |
| 58 | + const {queryByTestId} = render(` |
| 59 | + <div id="description">The description</div> |
| 60 | + <div data-testid="target" aria-describedby="description"></div> |
| 61 | + `) |
| 62 | + |
| 63 | + expect(() => |
| 64 | + expect(queryByTestId('other')).toHaveDescription('The description'), |
| 65 | + ).toThrowError() |
| 66 | + |
| 67 | + expect(() => |
| 68 | + expect(queryByTestId('target')).toHaveDescription('Something else'), |
| 69 | + ).toThrowError() |
| 70 | + |
| 71 | + expect(() => |
| 72 | + expect(queryByTestId('target')).not.toHaveDescription('The description'), |
| 73 | + ).toThrowError() |
| 74 | + }) |
| 75 | + |
| 76 | + test('normalizes whitespace', () => { |
| 77 | + const {queryByTestId} = render(` |
| 78 | + <div id="first"> |
| 79 | + Step |
| 80 | + 1 |
| 81 | + of |
| 82 | + 4 |
| 83 | + </div> |
| 84 | + <div id="second"> |
| 85 | + And |
| 86 | + extra |
| 87 | + description |
| 88 | + </div> |
| 89 | + <div data-testid="target" aria-describedby="first second"></div> |
| 90 | + `) |
| 91 | + |
| 92 | + expect(queryByTestId('target')).toHaveDescription( |
| 93 | + 'Step 1 of 4 And extra description', |
| 94 | + ) |
| 95 | + }) |
| 96 | + |
| 97 | + test('can handle multiple levels with content spread across decendants', () => { |
| 98 | + const {queryByTestId} = render(` |
| 99 | + <span id="description"> |
| 100 | + <span>Step</span> |
| 101 | + <span> 1</span> |
| 102 | + <span><span>of</span></span> |
| 103 | +
|
| 104 | +
|
| 105 | + 4</span> |
| 106 | + </span> |
| 107 | + <div data-testid="target" aria-describedby="description"></div> |
| 108 | + `) |
| 109 | + |
| 110 | + expect(queryByTestId('target')).toHaveDescription('Step 1 of 4') |
| 111 | + }) |
| 112 | + |
| 113 | + test('handles extra whitespace with multiple ids', () => { |
| 114 | + const {queryByTestId} = render(` |
| 115 | + <div id="first">First description</div> |
| 116 | + <div id="second">Second description</div> |
| 117 | + <div id="third">Third description</div> |
| 118 | +
|
| 119 | + <div data-testid="multiple" aria-describedby=" first |
| 120 | + second third |
| 121 | + "></div> |
| 122 | + `) |
| 123 | + |
| 124 | + expect(queryByTestId('multiple')).toHaveDescription( |
| 125 | + 'First description Second description Third description', |
| 126 | + ) |
| 127 | + }) |
| 128 | + |
| 129 | + test('is case-sensitive', () => { |
| 130 | + const {queryByTestId} = render(` |
| 131 | + <span id="description">Sensitive text</span> |
| 132 | + <div data-testid="target" aria-describedby="description"></div> |
| 133 | + `) |
| 134 | + |
| 135 | + expect(queryByTestId('target')).toHaveDescription('Sensitive text') |
| 136 | + expect(queryByTestId('target')).not.toHaveDescription('sensitive text') |
| 137 | + }) |
| 138 | +}) |
0 commit comments