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' ;
24import { createRuleTester } from '../test-utils' ;
35
46const ruleTester = createRuleTester ( ) ;
57
8+ type ValidTestCase = TSESLint . ValidTestCase < Options > ;
9+
610const SUPPORTED_TESTING_FRAMEWORKS = [
711 '@testing-library/angular' ,
812 '@testing-library/react' ,
@@ -11,51 +15,52 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
1115] ;
1216
1317ruleTester . 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 : `
1722 import { screen } from '${ testingFramework } ';
1823
1924 const buttonText = screen.getByText('submit');
2025 ` ,
21- } ,
22- {
23- code : `
26+ } ,
27+ {
28+ code : `
2429 import { screen } from '${ testingFramework } ';
2530
2631 const { getByText } = screen
2732 const firstChild = getByText('submit');
2833 expect(firstChild).toBeInTheDocument()
2934 ` ,
30- } ,
31- {
32- code : `
35+ } ,
36+ {
37+ code : `
3338 import { screen } from '${ testingFramework } ';
3439
3540 const firstChild = screen.getByText('submit');
3641 expect(firstChild).toBeInTheDocument()
3742 ` ,
38- } ,
39- {
40- code : `
43+ } ,
44+ {
45+ code : `
4146 import { screen } from '${ testingFramework } ';
4247
4348 const { getByText } = screen;
4449 const button = getByRole('button');
4550 expect(button).toHaveTextContent('submit');
4651 ` ,
47- } ,
48- {
49- code : `
52+ } ,
53+ {
54+ code : `
5055 import { render, within } from '${ testingFramework } ';
5156
5257 const { getByLabelText } = render(<MyComponent />);
5358 const signInModal = getByLabelText('Sign In');
5459 within(signInModal).getByPlaceholderText('Username');
5560 ` ,
56- } ,
57- {
58- code : `
61+ } ,
62+ {
63+ code : `
5964 // case: code not related to testing library at all
6065 ReactDOM.render(
6166 <CommProvider useDsa={false}>
@@ -70,25 +75,36 @@ ruleTester.run(RULE_NAME, rule, {
7075 document.getElementById('root')
7176 );
7277 ` ,
73- } ,
74- {
75- settings : {
76- 'testing-library/utils-module' : 'test-utils' ,
7778 } ,
78- code : `
79+ {
80+ settings : {
81+ 'testing-library/utils-module' : 'test-utils' ,
82+ } ,
83+ code : `
7984 // case: custom module set but not imported (aggressive reporting limited)
8085 const closestButton = document.getElementById('submit-btn').closest('button');
8186 expect(closestButton).toBeInTheDocument();
8287 ` ,
83- } ,
84- {
85- code : `
88+ } ,
89+ {
90+ code : `
8691 // case: without importing TL (aggressive reporting skipped)
8792 const closestButton = document.getElementById('submit-btn')
8893 expect(closestButton).toBeInTheDocument();
8994 ` ,
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+ ) ,
92108 invalid : SUPPORTED_TESTING_FRAMEWORKS . flatMap ( ( testingFramework ) => [
93109 {
94110 settings : {
@@ -291,5 +307,22 @@ ruleTester.run(RULE_NAME, rule, {
291307 } ,
292308 ] ,
293309 } ,
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+ } ,
294327 ] ) ,
295328} ) ;
0 commit comments