forked from testing-library/eslint-plugin-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefer-find-by.test.ts
90 lines (88 loc) · 3.01 KB
/
prefer-find-by.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { InvalidTestCase } from '@typescript-eslint/experimental-utils/dist/ts-eslint'
import { createRuleTester } from '../test-utils';
import { ASYNC_QUERIES_COMBINATIONS, SYNC_QUERIES_COMBINATIONS } from '../../../lib/utils';
import rule, { WAIT_METHODS, RULE_NAME } from '../../../lib/rules/prefer-find-by';
const ruleTester = createRuleTester({
ecmaFeatures: {
jsx: true,
},
});
ruleTester.run(RULE_NAME, rule, {
valid: [
...ASYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `const submitButton = await ${queryMethod}('foo')`
})),
...ASYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `const submitButton = await screen.${queryMethod}('foo')`
})),
...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `await waitForElementToBeRemoved(() => ${queryMethod}(baz))`
})),
...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `await waitFor(function() {
return ${queryMethod}('baz', { name: 'foo' })
})`
})),
{
code: `await waitFor(() => myCustomFunction())`
},
{
code: `await waitFor(customFunctionReference)`
},
{
code: `await waitForElementToBeRemoved(document.querySelector('foo'))`
},
...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `
await waitFor(() => {
foo()
return ${queryMethod}()
})
`
})),
...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `
await waitFor(() => expect(screen.${queryMethod}('baz')).toBeDisabled());
`
})),
...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `
await waitFor(() => expect(${queryMethod}('baz')).toBeInTheDocument());
`
}))
],
invalid: [
// using reduce + concat 'cause flatMap is not available in node10.x
...WAIT_METHODS.reduce((acc: InvalidTestCase<'preferFindBy', []>[], waitMethod) => acc
.concat(
SYNC_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
code: `
const submitButton = await ${waitMethod}(() => ${queryMethod}('foo', { name: 'baz' }))
`,
errors: [{
messageId: 'preferFindBy',
data: {
queryVariant: queryMethod.includes('All') ? 'findAllBy': 'findBy',
queryMethod: queryMethod.split('By')[1],
fullQuery: `${waitMethod}(() => ${queryMethod}('foo', { name: 'baz' }))`,
}
}]
}))
).concat(
SYNC_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
code: `
const submitButton = await ${waitMethod}(() => screen.${queryMethod}('foo', { name: 'baz' }))
`,
errors: [{
messageId: 'preferFindBy',
data: {
queryVariant: queryMethod.includes('All') ? 'findAllBy': 'findBy',
queryMethod: queryMethod.split('By')[1],
fullQuery: `${waitMethod}(() => screen.${queryMethod}('foo', { name: 'baz' }))`,
}
}]
}))
),
[])
],
})