Skip to content

Commit 53a950c

Browse files
committed
refactor: generate test cases for all query methods
1 parent e3e4231 commit 53a950c

File tree

1 file changed

+51
-57
lines changed

1 file changed

+51
-57
lines changed

tests/lib/rules/prefer-expect-query-by.js

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,61 @@
22

33
const RuleTester = require('eslint').RuleTester;
44
const rule = require('../../../lib/rules/prefer-expect-query-by');
5+
const { ALL_QUERIES_METHODS } = require('../../../lib/utils');
56

67
const ruleTester = new RuleTester({
78
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
89
});
910

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}`],
6715
],
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+
}, []),
6862
});

0 commit comments

Comments
 (0)