1
- import rule , { RULE_NAME } from '../../../lib/rules/no-node-access' ;
1
+ import type { TSESLint } from '@typescript-eslint/utils' ;
2
+
3
+ import rule , { RULE_NAME , Options } from '../../../lib/rules/no-node-access' ;
2
4
import { createRuleTester } from '../test-utils' ;
3
5
4
6
const ruleTester = createRuleTester ( ) ;
5
7
8
+ type ValidTestCase = TSESLint . ValidTestCase < Options > ;
9
+
6
10
const SUPPORTED_TESTING_FRAMEWORKS = [
7
11
'@testing-library/angular' ,
8
12
'@testing-library/react' ,
@@ -11,51 +15,52 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
11
15
] ;
12
16
13
17
ruleTester . run ( RULE_NAME , rule , {
14
- valid : SUPPORTED_TESTING_FRAMEWORKS . flatMap ( ( testingFramework ) => [
15
- {
16
- code : `
18
+ valid : SUPPORTED_TESTING_FRAMEWORKS . flatMap < ValidTestCase > (
19
+ ( testingFramework ) => [
20
+ {
21
+ code : `
17
22
import { screen } from '${ testingFramework } ';
18
23
19
24
const buttonText = screen.getByText('submit');
20
25
` ,
21
- } ,
22
- {
23
- code : `
26
+ } ,
27
+ {
28
+ code : `
24
29
import { screen } from '${ testingFramework } ';
25
30
26
31
const { getByText } = screen
27
32
const firstChild = getByText('submit');
28
33
expect(firstChild).toBeInTheDocument()
29
34
` ,
30
- } ,
31
- {
32
- code : `
35
+ } ,
36
+ {
37
+ code : `
33
38
import { screen } from '${ testingFramework } ';
34
39
35
40
const firstChild = screen.getByText('submit');
36
41
expect(firstChild).toBeInTheDocument()
37
42
` ,
38
- } ,
39
- {
40
- code : `
43
+ } ,
44
+ {
45
+ code : `
41
46
import { screen } from '${ testingFramework } ';
42
47
43
48
const { getByText } = screen;
44
49
const button = getByRole('button');
45
50
expect(button).toHaveTextContent('submit');
46
51
` ,
47
- } ,
48
- {
49
- code : `
52
+ } ,
53
+ {
54
+ code : `
50
55
import { render, within } from '${ testingFramework } ';
51
56
52
57
const { getByLabelText } = render(<MyComponent />);
53
58
const signInModal = getByLabelText('Sign In');
54
59
within(signInModal).getByPlaceholderText('Username');
55
60
` ,
56
- } ,
57
- {
58
- code : `
61
+ } ,
62
+ {
63
+ code : `
59
64
// case: code not related to testing library at all
60
65
ReactDOM.render(
61
66
<CommProvider useDsa={false}>
@@ -70,25 +75,36 @@ ruleTester.run(RULE_NAME, rule, {
70
75
document.getElementById('root')
71
76
);
72
77
` ,
73
- } ,
74
- {
75
- settings : {
76
- 'testing-library/utils-module' : 'test-utils' ,
77
78
} ,
78
- code : `
79
+ {
80
+ settings : {
81
+ 'testing-library/utils-module' : 'test-utils' ,
82
+ } ,
83
+ code : `
79
84
// case: custom module set but not imported (aggressive reporting limited)
80
85
const closestButton = document.getElementById('submit-btn').closest('button');
81
86
expect(closestButton).toBeInTheDocument();
82
87
` ,
83
- } ,
84
- {
85
- code : `
88
+ } ,
89
+ {
90
+ code : `
86
91
// case: without importing TL (aggressive reporting skipped)
87
92
const closestButton = document.getElementById('submit-btn')
88
93
expect(closestButton).toBeInTheDocument();
89
94
` ,
90
- } ,
91
- ] ) ,
95
+ } ,
96
+ {
97
+ options : [ { allowContainerFirstChild : true } ] ,
98
+ code : `
99
+ import { render } from '${ testingFramework } ';
100
+
101
+ const { container } = render(<MyComponent />)
102
+
103
+ expect(container.firstChild).toMatchSnapshot()
104
+ ` ,
105
+ } ,
106
+ ]
107
+ ) ,
92
108
invalid : SUPPORTED_TESTING_FRAMEWORKS . flatMap ( ( testingFramework ) => [
93
109
{
94
110
settings : {
@@ -291,5 +307,22 @@ ruleTester.run(RULE_NAME, rule, {
291
307
} ,
292
308
] ,
293
309
} ,
310
+ {
311
+ code : `
312
+ import { render } from '${ testingFramework } ';
313
+
314
+ const { container } = render(<MyComponent />)
315
+
316
+ expect(container.firstChild).toMatchSnapshot()
317
+ ` ,
318
+ errors : [
319
+ {
320
+ // error points to `firstChild`
321
+ line : 6 ,
322
+ column : 26 ,
323
+ messageId : 'noNodeAccess' ,
324
+ } ,
325
+ ] ,
326
+ } ,
294
327
] ) ,
295
328
} ) ;
0 commit comments