|
2 | 2 |
|
3 | 3 | const RuleTester = require('eslint').RuleTester;
|
4 | 4 | const rule = require('../../../lib/rules/prefer-expect-query-by');
|
| 5 | +const { ALL_QUERIES_METHODS } = require('../../../lib/utils'); |
5 | 6 |
|
6 | 7 | const ruleTester = new RuleTester({
|
7 | 8 | parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
|
8 | 9 | });
|
9 | 10 |
|
10 |
| -ruleTester.run('prefer-expect-query-by', rule, { |
11 |
| - valid: [ |
12 |
| - { code: "expect(queryByText('Hello')).not.toBeInTheDocument()" }, |
13 |
| - { code: "expect(rendered.queryByText('Hello')).not.toBeInTheDocument()" }, |
14 |
| - { code: "expect(queryAllByText('Hello')).not.toBeInTheDocument()" }, |
15 |
| - { |
16 |
| - code: "expect(rendered.queryAllByText('Hello')).not.toBeInTheDocument()", |
17 |
| - }, |
18 |
| - { code: "expect(queryByText('Hello')).toBeInTheDocument()" }, |
19 |
| - { code: "expect(rendered.queryByText('Hello')).toBeInTheDocument()" }, |
20 |
| - { code: "expect(queryAllByText('Hello')).toBeInTheDocument()" }, |
21 |
| - { |
22 |
| - code: "expect(rendered.queryAllByText('Hello')).toBeInTheDocument()", |
23 |
| - }, |
24 |
| - ], |
25 |
| - invalid: [ |
26 |
| - { |
27 |
| - code: "expect(getByText('Hello')).not.toBeInTheDocument()", |
28 |
| - errors: [{ messageId: 'expectQueryBy' }], |
29 |
| - output: "expect(queryByText('Hello')).not.toBeInTheDocument()", |
30 |
| - }, |
31 |
| - { |
32 |
| - code: "expect(rendered.getByText('Hello')).not.toBeInTheDocument()", |
33 |
| - errors: [{ messageId: 'expectQueryBy' }], |
34 |
| - output: "expect(rendered.queryByText('Hello')).not.toBeInTheDocument()", |
35 |
| - }, |
36 |
| - { |
37 |
| - code: "expect(getAllByText('Hello')).not.toBeInTheDocument()", |
38 |
| - errors: [{ messageId: 'expectQueryBy' }], |
39 |
| - output: "expect(queryAllByText('Hello')).not.toBeInTheDocument()", |
40 |
| - }, |
41 |
| - { |
42 |
| - code: "expect(rendered.getAllByText('Hello')).not.toBeInTheDocument()", |
43 |
| - errors: [{ messageId: 'expectQueryBy' }], |
44 |
| - output: |
45 |
| - "expect(rendered.queryAllByText('Hello')).not.toBeInTheDocument()", |
46 |
| - }, |
47 |
| - { |
48 |
| - code: "expect(getByText('Hello')).toBeInTheDocument()", |
49 |
| - errors: [{ messageId: 'expectQueryBy' }], |
50 |
| - output: "expect(queryByText('Hello')).toBeInTheDocument()", |
51 |
| - }, |
52 |
| - { |
53 |
| - code: "expect(rendered.getByText('Hello')).toBeInTheDocument()", |
54 |
| - errors: [{ messageId: 'expectQueryBy' }], |
55 |
| - output: "expect(rendered.queryByText('Hello')).toBeInTheDocument()", |
56 |
| - }, |
57 |
| - { |
58 |
| - code: "expect(getAllByText('Hello')).toBeInTheDocument()", |
59 |
| - errors: [{ messageId: 'expectQueryBy' }], |
60 |
| - output: "expect(queryAllByText('Hello')).toBeInTheDocument()", |
61 |
| - }, |
62 |
| - { |
63 |
| - code: "expect(rendered.getAllByText('Hello')).toBeInTheDocument()", |
64 |
| - errors: [{ messageId: 'expectQueryBy' }], |
65 |
| - output: "expect(rendered.queryAllByText('Hello')).toBeInTheDocument()", |
66 |
| - }, |
| 11 | +const queryByVariants = ALL_QUERIES_METHODS.reduce( |
| 12 | + (variants, method) => [ |
| 13 | + ...variants, |
| 14 | + ...[`query${method}`, `queryAll${method}`], |
67 | 15 | ],
|
| 16 | + [] |
| 17 | +); |
| 18 | +const getByVariants = ALL_QUERIES_METHODS.reduce( |
| 19 | + (variants, method) => [...variants, ...[`get${method}`, `getAll${method}`]], |
| 20 | + [] |
| 21 | +); |
| 22 | + |
| 23 | +ruleTester.run('prefer-expect-query-by', rule, { |
| 24 | + valid: queryByVariants.reduce( |
| 25 | + (validRules, queryName) => [ |
| 26 | + ...validRules, |
| 27 | + { code: `expect(${queryName}('Hello')).toBeInTheDocument()` }, |
| 28 | + { code: `expect(rendered.${queryName}('Hello')).toBeInTheDocument()` }, |
| 29 | + { code: `expect(${queryName}('Hello')).not.toBeInTheDocument()` }, |
| 30 | + { |
| 31 | + code: `expect(rendered.${queryName}('Hello')).not.toBeInTheDocument()`, |
| 32 | + }, |
| 33 | + ], |
| 34 | + [] |
| 35 | + ), |
| 36 | + invalid: getByVariants.reduce((invalidRules, queryName) => { |
| 37 | + const fixedQueryName = queryName.replace('get', 'query'); |
| 38 | + return [ |
| 39 | + ...invalidRules, |
| 40 | + { |
| 41 | + code: `expect(${queryName}('Hello')).toBeInTheDocument()`, |
| 42 | + errors: [{ messageId: 'expectQueryBy' }], |
| 43 | + output: `expect(${fixedQueryName}('Hello')).toBeInTheDocument()`, |
| 44 | + }, |
| 45 | + { |
| 46 | + code: `expect(rendered.${queryName}('Hello')).toBeInTheDocument()`, |
| 47 | + errors: [{ messageId: 'expectQueryBy' }], |
| 48 | + output: `expect(rendered.${fixedQueryName}('Hello')).toBeInTheDocument()`, |
| 49 | + }, |
| 50 | + { |
| 51 | + code: `expect(${queryName}('Hello')).not.toBeInTheDocument()`, |
| 52 | + errors: [{ messageId: 'expectQueryBy' }], |
| 53 | + output: `expect(${fixedQueryName}('Hello')).not.toBeInTheDocument()`, |
| 54 | + }, |
| 55 | + { |
| 56 | + code: `expect(rendered.${queryName}('Hello')).not.toBeInTheDocument()`, |
| 57 | + errors: [{ messageId: 'expectQueryBy' }], |
| 58 | + output: `expect(rendered.${fixedQueryName}('Hello')).not.toBeInTheDocument()`, |
| 59 | + }, |
| 60 | + ]; |
| 61 | + }, []), |
68 | 62 | });
|
0 commit comments