|
| 1 | +import {configure} from '../config' |
| 2 | +import {screen} from '..' |
| 3 | +import {renderIntoDocument} from './helpers/test-utils' |
| 4 | + |
| 5 | +beforeAll(() => { |
| 6 | + configure({throwSuggestions: true}) |
| 7 | +}) |
| 8 | + |
| 9 | +afterAll(() => { |
| 10 | + configure({throwSuggestions: false}) |
| 11 | +}) |
| 12 | + |
| 13 | +test('does not suggest when using getByRole', () => { |
| 14 | + renderIntoDocument(`<button data-testid="foo">submit</button>`) |
| 15 | + |
| 16 | + expect(() => screen.getByRole('button', {name: /submit/i})).not.toThrowError() |
| 17 | +}) |
| 18 | + |
| 19 | +test('should not suggest when nothing available', () => { |
| 20 | + renderIntoDocument(`<span data-testid="foo" />`) |
| 21 | + |
| 22 | + expect(() => screen.queryByTestId('foo')).not.toThrowError() |
| 23 | +}) |
| 24 | + |
| 25 | +test(`should not suggest if the suggestion would give different results`, () => { |
| 26 | + renderIntoDocument(` |
| 27 | + <input type="text" data-testid="foo" /><span data-testid="foo" /> |
| 28 | + `) |
| 29 | + |
| 30 | + expect(() => |
| 31 | + screen.getAllByTestId('foo', {suggest: false}), |
| 32 | + ).not.toThrowError() |
| 33 | +}) |
| 34 | + |
| 35 | +test('should not suggest if there would be mixed suggestions', () => { |
| 36 | + renderIntoDocument(` |
| 37 | + <button data-testid="foo">submit</button> |
| 38 | + <label for="foo">Username</label><input data-testid="foo" id="foo" />`) |
| 39 | + |
| 40 | + expect(() => screen.getAllByTestId('foo')).not.toThrowError() |
| 41 | +}) |
| 42 | + |
| 43 | +test('should not suggest when suggest is turned off for a query', () => { |
| 44 | + renderIntoDocument(` |
| 45 | + <button data-testid="foo">submit</button> |
| 46 | + <button data-testid="foot">another</button>`) |
| 47 | + |
| 48 | + expect(() => screen.getByTestId('foo', {suggest: false})).not.toThrowError() |
| 49 | + expect(() => |
| 50 | + screen.getAllByTestId(/foo/, {suggest: false}), |
| 51 | + ).not.toThrowError() |
| 52 | +}) |
| 53 | + |
| 54 | +test('should suggest getByRole when used with getBy', () => { |
| 55 | + renderIntoDocument(`<button data-testid="foo">submit</button>`) |
| 56 | + |
| 57 | + expect(() => screen.getByTestId('foo')).toThrowErrorMatchingInlineSnapshot(` |
| 58 | +"A better query is available, try this: |
| 59 | +getByRole("button", {name: /submit/i}) |
| 60 | +
|
| 61 | +
|
| 62 | +<body> |
| 63 | + <button |
| 64 | + data-testid="foo" |
| 65 | + > |
| 66 | + submit |
| 67 | + </button> |
| 68 | +</body>" |
| 69 | +`) |
| 70 | +}) |
| 71 | + |
| 72 | +test('should suggest getAllByRole when used with getAllByTestId', () => { |
| 73 | + renderIntoDocument(` |
| 74 | + <button data-testid="foo">submit</button> |
| 75 | + <button data-testid="foo">submit</button>`) |
| 76 | + |
| 77 | + expect(() => screen.getAllByTestId('foo')) |
| 78 | + .toThrowErrorMatchingInlineSnapshot(` |
| 79 | +"A better query is available, try this: |
| 80 | +getAllByRole("button", {name: /submit/i}) |
| 81 | +
|
| 82 | +
|
| 83 | +<body> |
| 84 | + |
| 85 | + |
| 86 | + <button |
| 87 | + data-testid="foo" |
| 88 | + > |
| 89 | + submit |
| 90 | + </button> |
| 91 | + |
| 92 | + |
| 93 | + <button |
| 94 | + data-testid="foo" |
| 95 | + > |
| 96 | + submit |
| 97 | + </button> |
| 98 | +</body>" |
| 99 | +`) |
| 100 | +}) |
| 101 | +test('should suggest findByRole when used with findByTestId', async () => { |
| 102 | + renderIntoDocument(` |
| 103 | + <button data-testid="foo">submit</button> |
| 104 | + <button data-testid="foot">submit</button> |
| 105 | + `) |
| 106 | + |
| 107 | + await expect(screen.findByTestId('foo')).rejects.toThrowError( |
| 108 | + /findByRole\("button", \{name: \/submit\/i\}\)/, |
| 109 | + ) |
| 110 | + await expect(screen.findAllByTestId(/foo/)).rejects.toThrowError( |
| 111 | + /findAllByRole\("button", \{name: \/submit\/i\}\)/, |
| 112 | + ) |
| 113 | +}) |
| 114 | + |
| 115 | +test('should suggest img role w/ alt text', () => { |
| 116 | + renderIntoDocument(`<img data-testid="img" alt="Incredibles 2 Poster" />`) |
| 117 | + |
| 118 | + expect(() => screen.getByAltText('Incredibles 2 Poster')).toThrowError( |
| 119 | + /getByRole\("img", \{name: \/incredibles 2 poster\/i\}\)/, |
| 120 | + ) |
| 121 | +}) |
| 122 | + |
| 123 | +test('should suggest getByLabelText when no role available', () => { |
| 124 | + renderIntoDocument( |
| 125 | + `<label for="foo">Username</label><input data-testid="foo" id="foo" />`, |
| 126 | + ) |
| 127 | + expect(() => screen.getByTestId('foo')).toThrowError( |
| 128 | + /getByLabelText\("Username"\)/, |
| 129 | + ) |
| 130 | +}) |
| 131 | + |
| 132 | +test(`should suggest getByLabel on non form elements`, () => { |
| 133 | + renderIntoDocument(` |
| 134 | + <div data-testid="foo" aria-labelledby="section-one-header"> |
| 135 | + <span id="section-one-header">Section One</span> |
| 136 | + <p>some content</p> |
| 137 | + </div> |
| 138 | + `) |
| 139 | + |
| 140 | + expect(() => screen.getByTestId('foo')).toThrowError( |
| 141 | + /getByLabelText\("Section One"\)/, |
| 142 | + ) |
| 143 | +}) |
| 144 | + |
| 145 | +test.each([ |
| 146 | + `<label id="username-label">Username</label><input aria-labelledby="username-label" type="text" />`, |
| 147 | + `<label><span>Username</span><input type="text" /></label>`, |
| 148 | + `<label for="foo">Username</label><input id="foo" type="text" />`, |
| 149 | +])('%s\nshould suggest getByRole over', async html => { |
| 150 | + renderIntoDocument(html) |
| 151 | + |
| 152 | + expect(() => screen.getByLabelText('Username')).toThrowError( |
| 153 | + /getByRole\("textbox", \{name: \/username\/i\}\)/, |
| 154 | + ) |
| 155 | + expect(() => screen.getAllByLabelText('Username')).toThrowError( |
| 156 | + /getAllByRole\("textbox", \{name: \/username\/i\}\)/, |
| 157 | + ) |
| 158 | + |
| 159 | + expect(() => screen.queryByLabelText('Username')).toThrowError( |
| 160 | + /queryByRole\("textbox", \{name: \/username\/i\}\)/, |
| 161 | + ) |
| 162 | + expect(() => screen.queryAllByLabelText('Username')).toThrowError( |
| 163 | + /queryAllByRole\("textbox", \{name: \/username\/i\}\)/, |
| 164 | + ) |
| 165 | + |
| 166 | + await expect(screen.findByLabelText('Username')).rejects.toThrowError( |
| 167 | + /findByRole\("textbox", \{name: \/username\/i\}\)/, |
| 168 | + ) |
| 169 | + await expect(screen.findAllByLabelText(/Username/)).rejects.toThrowError( |
| 170 | + /findAllByRole\("textbox", \{name: \/username\/i\}\)/, |
| 171 | + ) |
| 172 | +}) |
| 173 | + |
| 174 | +test(`should suggest label over placeholder text`, () => { |
| 175 | + renderIntoDocument( |
| 176 | + `<label for="foo">Username</label><input id="foo" data-testid="foo" placeholder="Username" />`, |
| 177 | + ) |
| 178 | + |
| 179 | + expect(() => screen.getByPlaceholderText('Username')).toThrowError( |
| 180 | + /getByLabelText\("Username"\)/, |
| 181 | + ) |
| 182 | +}) |
| 183 | + |
| 184 | +test(`should suggest getByPlaceholderText`, () => { |
| 185 | + renderIntoDocument(`<input data-testid="foo" placeholder="Username" />`) |
| 186 | + |
| 187 | + expect(() => screen.getByTestId('foo')).toThrowError( |
| 188 | + /getByPlaceholderText\("Username"\)/, |
| 189 | + ) |
| 190 | +}) |
| 191 | + |
| 192 | +test(`should suggest getByText for simple elements`, () => { |
| 193 | + renderIntoDocument(`<div data-testid="foo">hello there</div>`) |
| 194 | + |
| 195 | + expect(() => screen.getByTestId('foo')).toThrowError( |
| 196 | + /getByText\("hello there"\)/, |
| 197 | + ) |
| 198 | +}) |
| 199 | + |
| 200 | +test(`should suggest getByDisplayValue`, () => { |
| 201 | + renderIntoDocument(`<input id="lastName" data-testid="lastName" />`) |
| 202 | + |
| 203 | + document.getElementById('lastName').value = 'Prine' // RIP John Prine |
| 204 | + |
| 205 | + expect(() => screen.getByTestId('lastName')).toThrowError( |
| 206 | + /getByDisplayValue\("Prine"\)/, |
| 207 | + ) |
| 208 | +}) |
| 209 | + |
| 210 | +test(`should suggest getByAltText`, () => { |
| 211 | + renderIntoDocument(` |
| 212 | + <input data-testid="input" alt="last name" /> |
| 213 | + <map name="workmap"> |
| 214 | + <area data-testid="area" shape="rect" coords="34,44,270,350" alt="Computer"> |
| 215 | + </map> |
| 216 | + `) |
| 217 | + |
| 218 | + expect(() => screen.getByTestId('input')).toThrowError( |
| 219 | + /getByAltText\("last name"\)/, |
| 220 | + ) |
| 221 | + expect(() => screen.getByTestId('area')).toThrowError( |
| 222 | + /getByAltText\("Computer"\)/, |
| 223 | + ) |
| 224 | +}) |
| 225 | + |
| 226 | +test(`should suggest getByTitle`, () => { |
| 227 | + renderIntoDocument(` |
| 228 | + <span title="Delete" data-testid="delete"></span> |
| 229 | + <svg> |
| 230 | + <title data-testid="svg">Close</title> |
| 231 | + <g><path /></g> |
| 232 | + </svg>`) |
| 233 | + |
| 234 | + expect(() => screen.getByTestId('delete')).toThrowError( |
| 235 | + /getByTitle\("Delete"\)/, |
| 236 | + ) |
| 237 | + expect(() => screen.getAllByTestId('delete')).toThrowError( |
| 238 | + /getAllByTitle\("Delete"\)/, |
| 239 | + ) |
| 240 | + expect(() => screen.queryByTestId('delete')).toThrowError( |
| 241 | + /queryByTitle\("Delete"\)/, |
| 242 | + ) |
| 243 | + expect(() => screen.queryAllByTestId('delete')).toThrowError( |
| 244 | + /queryAllByTitle\("Delete"\)/, |
| 245 | + ) |
| 246 | + expect(() => screen.queryAllByTestId('delete')).toThrowError( |
| 247 | + /queryAllByTitle\("Delete"\)/, |
| 248 | + ) |
| 249 | + expect(() => screen.queryAllByTestId('delete')).toThrowError( |
| 250 | + /queryAllByTitle\("Delete"\)/, |
| 251 | + ) |
| 252 | + |
| 253 | + // Since `ByTitle` and `ByText` will both return the <title> element |
| 254 | + // `getByText` will always be the suggested query as it is higher up the list. |
| 255 | + expect(() => screen.getByTestId('svg')).toThrowError(/getByText\("Close"\)/) |
| 256 | +}) |
0 commit comments