|
| 1 | +import { TSESLint } from '@typescript-eslint/utils'; |
| 2 | + |
| 3 | +import rule, { |
| 4 | + RULE_NAME, |
| 5 | + MessageIds, |
| 6 | + Options, |
| 7 | +} from '../../../lib/rules/prefer-query-matchers'; |
| 8 | +import { ALL_QUERIES_METHODS } from '../../../lib/utils'; |
| 9 | +import { createRuleTester } from '../test-utils'; |
| 10 | + |
| 11 | +const ruleTester = createRuleTester(); |
| 12 | + |
| 13 | +const getByQueries = ALL_QUERIES_METHODS.map((method) => `get${method}`); |
| 14 | +const getAllByQueries = ALL_QUERIES_METHODS.map((method) => `getAll${method}`); |
| 15 | +const queryByQueries = ALL_QUERIES_METHODS.map((method) => `query${method}`); |
| 16 | +const queryAllByQueries = ALL_QUERIES_METHODS.map( |
| 17 | + (method) => `queryAll${method}` |
| 18 | +); |
| 19 | + |
| 20 | +type RuleValidTestCase = TSESLint.ValidTestCase<Options>; |
| 21 | +type RuleInvalidTestCase = TSESLint.InvalidTestCase<MessageIds, Options>; |
| 22 | + |
| 23 | +type AssertionFnParams = { |
| 24 | + query: string; |
| 25 | + matcher: string; |
| 26 | + options: Options; |
| 27 | +}; |
| 28 | + |
| 29 | +const wrapExpectInTest = (expectStatement: string) => ` |
| 30 | +import { render, screen } from '@testing-library/react' |
| 31 | +
|
| 32 | +test('a fake test', () => { |
| 33 | + render(<Component />) |
| 34 | +
|
| 35 | + ${expectStatement} |
| 36 | +})`; |
| 37 | + |
| 38 | +const getValidAssertions = ({ |
| 39 | + query, |
| 40 | + matcher, |
| 41 | + options, |
| 42 | +}: AssertionFnParams): RuleValidTestCase[] => { |
| 43 | + const expectStatement = `expect(${query}('Hello'))${matcher}`; |
| 44 | + const expectScreenStatement = `expect(screen.${query}('Hello'))${matcher}`; |
| 45 | + return [ |
| 46 | + { |
| 47 | + // name: `${expectStatement} with default options of empty validEntries`, |
| 48 | + code: wrapExpectInTest(expectStatement), |
| 49 | + }, |
| 50 | + { |
| 51 | + // name: `${expectStatement} with provided options`, |
| 52 | + code: wrapExpectInTest(expectStatement), |
| 53 | + options, |
| 54 | + }, |
| 55 | + { |
| 56 | + // name: `${expectScreenStatement} with default options of empty validEntries`, |
| 57 | + code: wrapExpectInTest(expectScreenStatement), |
| 58 | + }, |
| 59 | + { |
| 60 | + // name: `${expectScreenStatement} with provided options`, |
| 61 | + code: wrapExpectInTest(expectScreenStatement), |
| 62 | + options, |
| 63 | + }, |
| 64 | + ]; |
| 65 | +}; |
| 66 | + |
| 67 | +const getInvalidAssertions = ({ |
| 68 | + query, |
| 69 | + matcher, |
| 70 | + options, |
| 71 | +}: AssertionFnParams): RuleInvalidTestCase[] => { |
| 72 | + const expectStatement = `expect(${query}('Hello'))${matcher}`; |
| 73 | + const expectScreenStatement = `expect(screen.${query}('Hello'))${matcher}`; |
| 74 | + const messageId: MessageIds = 'wrongQueryForMatcher'; |
| 75 | + const [ |
| 76 | + { |
| 77 | + validEntries: [validEntry], |
| 78 | + }, |
| 79 | + ] = options; |
| 80 | + return [ |
| 81 | + { |
| 82 | + // name: `${expectStatement} with provided options`, |
| 83 | + code: wrapExpectInTest(expectStatement), |
| 84 | + options, |
| 85 | + errors: [ |
| 86 | + { |
| 87 | + messageId, |
| 88 | + line: 7, |
| 89 | + column: 10, |
| 90 | + data: { query: validEntry.query, matcher: validEntry.matcher }, |
| 91 | + }, |
| 92 | + ], |
| 93 | + }, |
| 94 | + { |
| 95 | + // name: `${expectScreenStatement} with provided options`, |
| 96 | + code: wrapExpectInTest(expectScreenStatement), |
| 97 | + options, |
| 98 | + errors: [ |
| 99 | + { |
| 100 | + messageId, |
| 101 | + line: 7, |
| 102 | + column: 17, |
| 103 | + data: { query: validEntry.query, matcher: validEntry.matcher }, |
| 104 | + }, |
| 105 | + ], |
| 106 | + }, |
| 107 | + ]; |
| 108 | +}; |
| 109 | + |
| 110 | +ruleTester.run(RULE_NAME, rule, { |
| 111 | + valid: [ |
| 112 | + // cases: methods not matching Testing Library queries pattern |
| 113 | + `expect(queryElement('foo')).toBeVisible()`, |
| 114 | + `expect(getElement('foo')).not.toBeVisible()`, |
| 115 | + // cases: asserting with a configured allowed `[screen.]getBy*` query |
| 116 | + ...getByQueries.reduce<RuleValidTestCase[]>( |
| 117 | + (validRules, queryName) => [ |
| 118 | + ...validRules, |
| 119 | + ...getValidAssertions({ |
| 120 | + query: queryName, |
| 121 | + matcher: '.toBeVisible()', |
| 122 | + options: [ |
| 123 | + { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }, |
| 124 | + ], |
| 125 | + }), |
| 126 | + ...getValidAssertions({ |
| 127 | + query: queryName, |
| 128 | + matcher: '.toBeHelloWorld()', |
| 129 | + options: [ |
| 130 | + { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }, |
| 131 | + ], |
| 132 | + }), |
| 133 | + ...getValidAssertions({ |
| 134 | + query: queryName, |
| 135 | + matcher: '.not.toBeVisible()', |
| 136 | + options: [ |
| 137 | + { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }, |
| 138 | + ], |
| 139 | + }), |
| 140 | + ...getValidAssertions({ |
| 141 | + query: queryName, |
| 142 | + matcher: '.not.toBeHelloWorld()', |
| 143 | + options: [ |
| 144 | + { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }, |
| 145 | + ], |
| 146 | + }), |
| 147 | + ...getValidAssertions({ |
| 148 | + query: queryName, |
| 149 | + matcher: '.toBeHelloWorld()', |
| 150 | + options: [ |
| 151 | + { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] }, |
| 152 | + ], |
| 153 | + }), |
| 154 | + ], |
| 155 | + [] |
| 156 | + ), |
| 157 | + // cases: asserting with a configured allowed `[screen.]getAllBy*` query |
| 158 | + ...getAllByQueries.reduce<RuleValidTestCase[]>( |
| 159 | + (validRules, queryName) => [ |
| 160 | + ...validRules, |
| 161 | + ...getValidAssertions({ |
| 162 | + query: queryName, |
| 163 | + matcher: '.toBeVisible()', |
| 164 | + options: [ |
| 165 | + { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }, |
| 166 | + ], |
| 167 | + }), |
| 168 | + ...getValidAssertions({ |
| 169 | + query: queryName, |
| 170 | + matcher: '.toBeHelloWorld()', |
| 171 | + options: [ |
| 172 | + { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }, |
| 173 | + ], |
| 174 | + }), |
| 175 | + ...getValidAssertions({ |
| 176 | + query: queryName, |
| 177 | + matcher: '.not.toBeVisible()', |
| 178 | + options: [ |
| 179 | + { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }, |
| 180 | + ], |
| 181 | + }), |
| 182 | + ...getValidAssertions({ |
| 183 | + query: queryName, |
| 184 | + matcher: '.not.toBeHelloWorld()', |
| 185 | + options: [ |
| 186 | + { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }, |
| 187 | + ], |
| 188 | + }), |
| 189 | + ...getValidAssertions({ |
| 190 | + query: queryName, |
| 191 | + matcher: '.toBeHelloWorld()', |
| 192 | + options: [ |
| 193 | + { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] }, |
| 194 | + ], |
| 195 | + }), |
| 196 | + ], |
| 197 | + [] |
| 198 | + ), |
| 199 | + // cases: asserting with a configured allowed `[screen.]queryBy*` query |
| 200 | + ...queryByQueries.reduce<RuleValidTestCase[]>( |
| 201 | + (validRules, queryName) => [ |
| 202 | + ...validRules, |
| 203 | + ...getValidAssertions({ |
| 204 | + query: queryName, |
| 205 | + matcher: '.toBeVisible()', |
| 206 | + options: [ |
| 207 | + { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] }, |
| 208 | + ], |
| 209 | + }), |
| 210 | + ...getValidAssertions({ |
| 211 | + query: queryName, |
| 212 | + matcher: '.toBeHelloWorld()', |
| 213 | + options: [ |
| 214 | + { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] }, |
| 215 | + ], |
| 216 | + }), |
| 217 | + ...getValidAssertions({ |
| 218 | + query: queryName, |
| 219 | + matcher: '.not.toBeVisible()', |
| 220 | + options: [ |
| 221 | + { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] }, |
| 222 | + ], |
| 223 | + }), |
| 224 | + ...getValidAssertions({ |
| 225 | + query: queryName, |
| 226 | + matcher: '.not.toBeHelloWorld()', |
| 227 | + options: [ |
| 228 | + { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] }, |
| 229 | + ], |
| 230 | + }), |
| 231 | + ...getValidAssertions({ |
| 232 | + query: queryName, |
| 233 | + matcher: '.toBeHelloWorld()', |
| 234 | + options: [ |
| 235 | + { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }, |
| 236 | + ], |
| 237 | + }), |
| 238 | + ], |
| 239 | + [] |
| 240 | + ), |
| 241 | + // cases: asserting with a configured allowed `[screen.]queryAllBy*` query |
| 242 | + ...queryAllByQueries.reduce<RuleValidTestCase[]>( |
| 243 | + (validRules, queryName) => [ |
| 244 | + ...validRules, |
| 245 | + ...getValidAssertions({ |
| 246 | + query: queryName, |
| 247 | + matcher: '.toBeVisible()', |
| 248 | + options: [ |
| 249 | + { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] }, |
| 250 | + ], |
| 251 | + }), |
| 252 | + ...getValidAssertions({ |
| 253 | + query: queryName, |
| 254 | + matcher: '.toBeHelloWorld()', |
| 255 | + options: [ |
| 256 | + { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] }, |
| 257 | + ], |
| 258 | + }), |
| 259 | + ...getValidAssertions({ |
| 260 | + query: queryName, |
| 261 | + matcher: '.not.toBeVisible()', |
| 262 | + options: [ |
| 263 | + { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] }, |
| 264 | + ], |
| 265 | + }), |
| 266 | + ...getValidAssertions({ |
| 267 | + query: queryName, |
| 268 | + matcher: '.not.toBeHelloWorld()', |
| 269 | + options: [ |
| 270 | + { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] }, |
| 271 | + ], |
| 272 | + }), |
| 273 | + ...getValidAssertions({ |
| 274 | + query: queryName, |
| 275 | + matcher: '.toBeHelloWorld()', |
| 276 | + options: [ |
| 277 | + { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }, |
| 278 | + ], |
| 279 | + }), |
| 280 | + ], |
| 281 | + [] |
| 282 | + ), |
| 283 | + // case: getting outside an expectation |
| 284 | + { |
| 285 | + code: 'const el = getByText("button")', |
| 286 | + }, |
| 287 | + // case: querying outside an expectation |
| 288 | + { |
| 289 | + code: 'const el = queryByText("button")', |
| 290 | + }, |
| 291 | + // case: finding outside an expectation |
| 292 | + { |
| 293 | + code: `async () => { |
| 294 | + const el = await findByText('button') |
| 295 | + expect(el).toBeVisible() |
| 296 | + }`, |
| 297 | + }, |
| 298 | + { |
| 299 | + code: `// case: query an element with getBy but then check its absence after doing |
| 300 | + // some action which makes it disappear. |
| 301 | +
|
| 302 | + // submit button exists |
| 303 | + const submitButton = screen.getByRole('button') |
| 304 | + fireEvent.click(submitButton) |
| 305 | +
|
| 306 | + // right after clicking submit button it disappears |
| 307 | + expect(submitButton).toBeHelloWorld() |
| 308 | + `, |
| 309 | + options: [ |
| 310 | + { validEntries: [{ query: 'query', matcher: 'toBeHelloWorld' }] }, |
| 311 | + ], |
| 312 | + }, |
| 313 | + ], |
| 314 | + invalid: [ |
| 315 | + // cases: asserting with a disallowed `[screen.]getBy*` query |
| 316 | + ...getByQueries.reduce<RuleInvalidTestCase[]>( |
| 317 | + (validRules, queryName) => [ |
| 318 | + ...validRules, |
| 319 | + ...getInvalidAssertions({ |
| 320 | + query: queryName, |
| 321 | + matcher: '.toBeHelloWorld()', |
| 322 | + options: [ |
| 323 | + { validEntries: [{ query: 'query', matcher: 'toBeHelloWorld' }] }, |
| 324 | + ], |
| 325 | + }), |
| 326 | + ], |
| 327 | + [] |
| 328 | + ), |
| 329 | + // cases: asserting with a disallowed `[screen.]getAllBy*` query |
| 330 | + ...getAllByQueries.reduce<RuleInvalidTestCase[]>( |
| 331 | + (validRules, queryName) => [ |
| 332 | + ...validRules, |
| 333 | + ...getInvalidAssertions({ |
| 334 | + query: queryName, |
| 335 | + matcher: '.toBeHelloWorld()', |
| 336 | + options: [ |
| 337 | + { validEntries: [{ query: 'query', matcher: 'toBeHelloWorld' }] }, |
| 338 | + ], |
| 339 | + }), |
| 340 | + ], |
| 341 | + [] |
| 342 | + ), |
| 343 | + // cases: asserting with a disallowed `[screen.]getBy*` query |
| 344 | + ...queryByQueries.reduce<RuleInvalidTestCase[]>( |
| 345 | + (invalidRules, queryName) => [ |
| 346 | + ...invalidRules, |
| 347 | + ...getInvalidAssertions({ |
| 348 | + query: queryName, |
| 349 | + matcher: '.toBeVisible()', |
| 350 | + options: [ |
| 351 | + { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }, |
| 352 | + ], |
| 353 | + }), |
| 354 | + ], |
| 355 | + [] |
| 356 | + ), |
| 357 | + // cases: asserting with a disallowed `[screen.]queryAllBy*` query |
| 358 | + ...queryAllByQueries.reduce<RuleInvalidTestCase[]>( |
| 359 | + (invalidRules, queryName) => [ |
| 360 | + ...invalidRules, |
| 361 | + ...getInvalidAssertions({ |
| 362 | + query: queryName, |
| 363 | + matcher: '.toBeVisible()', |
| 364 | + options: [ |
| 365 | + { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }, |
| 366 | + ], |
| 367 | + }), |
| 368 | + ], |
| 369 | + [] |
| 370 | + ), |
| 371 | + // cases: indexing into an `AllBy` result within the expectation |
| 372 | + { |
| 373 | + code: 'expect(screen.queryAllByText("button")[1]).toBeVisible()', |
| 374 | + options: [{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }], |
| 375 | + errors: [ |
| 376 | + { |
| 377 | + messageId: 'wrongQueryForMatcher', |
| 378 | + line: 1, |
| 379 | + column: 15, |
| 380 | + data: { query: 'get', matcher: 'toBeVisible' }, |
| 381 | + }, |
| 382 | + ], |
| 383 | + }, |
| 384 | + { |
| 385 | + code: ` |
| 386 | + // case: asserting presence incorrectly with custom queryBy* query |
| 387 | + expect(queryByCustomQuery("button")).toBeVisible() |
| 388 | + `, |
| 389 | + options: [{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }], |
| 390 | + errors: [ |
| 391 | + { |
| 392 | + messageId: 'wrongQueryForMatcher', |
| 393 | + line: 3, |
| 394 | + column: 12, |
| 395 | + data: { query: 'get', matcher: 'toBeVisible' }, |
| 396 | + }, |
| 397 | + ], |
| 398 | + }, |
| 399 | + ], |
| 400 | +}); |
0 commit comments