|
| 1 | +/** |
| 2 | + * @fileoverview Prefer toBeInTheDocument over querying and asserting length. |
| 3 | + * @author Anton Niklasson |
| 4 | + */ |
| 5 | + |
| 6 | +//------------------------------------------------------------------------------ |
| 7 | +// Requirements |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | + |
| 10 | +import { RuleTester } from "eslint"; |
| 11 | +import { queries, queriesByVariant } from "../../../queries"; |
| 12 | +import * as rule from "../../../rules/prefer-in-document"; |
| 13 | + |
| 14 | +//------------------------------------------------------------------------------ |
| 15 | +// Tests |
| 16 | +//------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +function invalidCase(code, output) { |
| 19 | + return { |
| 20 | + code, |
| 21 | + output, |
| 22 | + errors: [ |
| 23 | + { |
| 24 | + messageId: "use-document", |
| 25 | + }, |
| 26 | + ], |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +const valid = [ |
| 31 | + ...queries.map((q) => [ |
| 32 | + `expect(screen.${q}('foo')).toBeInTheDocument()`, |
| 33 | + `expect(${q}('foo')).toBeInTheDocument()`, |
| 34 | + `expect(wrapper.${q}('foo')).toBeInTheDocument()`, |
| 35 | + ]), |
| 36 | + `expect(screen.notAQuery('foo-bar')).toHaveLength(1)`, |
| 37 | + `expect(screen.getByText('foo-bar')).toHaveLength(2)`, |
| 38 | +]; |
| 39 | +const invalid = [ |
| 40 | + // Invalid cases that applies to all variants |
| 41 | + ...queries.map((q) => [ |
| 42 | + invalidCase( |
| 43 | + `expect(screen.${q}('foo')).toHaveLength(1)`, |
| 44 | + `expect(screen.${q}('foo')).toBeInTheDocument()` |
| 45 | + ), |
| 46 | + invalidCase( |
| 47 | + `expect(${q}('foo')).toHaveLength(1)`, |
| 48 | + `expect(${q}('foo')).toBeInTheDocument()` |
| 49 | + ), |
| 50 | + invalidCase( |
| 51 | + `expect(wrapper.${q}('foo')).toHaveLength(1)`, |
| 52 | + `expect(wrapper.${q}('foo')).toBeInTheDocument()` |
| 53 | + ), |
| 54 | + ]), |
| 55 | + // Invalid cases that applies to queryBy* and queryAllBy* |
| 56 | + ...queriesByVariant.query.map((q) => [ |
| 57 | + invalidCase( |
| 58 | + `expect(${q}('foo')).toHaveLength(0)`, |
| 59 | + `expect(${q}('foo')).not.toBeInTheDocument()` |
| 60 | + ), |
| 61 | + invalidCase( |
| 62 | + `expect(${q}('foo')).toBeNull()`, |
| 63 | + `expect(${q}('foo')).not.toBeInTheDocument()` |
| 64 | + ), |
| 65 | + invalidCase( |
| 66 | + `expect(${q}('foo')).not.toBeNull()`, |
| 67 | + `expect(${q}('foo')).toBeInTheDocument()` |
| 68 | + ), |
| 69 | + invalidCase( |
| 70 | + `expect(${q}('foo')).toBeDefined()`, |
| 71 | + `expect(${q}('foo')).toBeInTheDocument()` |
| 72 | + ), |
| 73 | + invalidCase( |
| 74 | + `expect(${q}('foo')).not.toBeDefined()`, |
| 75 | + `expect(${q}('foo')).not.toBeInTheDocument()` |
| 76 | + ), |
| 77 | + ]), |
| 78 | +]; |
| 79 | + |
| 80 | +const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } }); |
| 81 | +ruleTester.run("prefer-in-document", rule, { |
| 82 | + valid: [].concat(...valid), |
| 83 | + invalid: [].concat(...invalid), |
| 84 | +}); |
0 commit comments