Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: add test
  • Loading branch information
y-hsgw committed Jun 6, 2025
commit 47f1ac9e82af3febb55eab708b0580ffe9616a5c
41 changes: 33 additions & 8 deletions tests/lib/rules/no-node-access.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { type TSESLint } from '@typescript-eslint/utils';
import { InvalidTestCase, ValidTestCase } from '@typescript-eslint/rule-tester';

import rule, { RULE_NAME, Options } from '../../../lib/rules/no-node-access';
import rule, {
RULE_NAME,
Options,
MessageIds,
} from '../../../lib/rules/no-node-access';
import { EVENT_HANDLER_METHODS } from '../../../lib/utils';
import { createRuleTester } from '../test-utils';

const ruleTester = createRuleTester();

type ValidTestCase = TSESLint.ValidTestCase<Options>;
type RuleValidTestCase = ValidTestCase<Options>;
type RuleInvalidTestCase = InvalidTestCase<MessageIds, Options>;

const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/angular',
Expand All @@ -15,7 +21,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
];

ruleTester.run(RULE_NAME, rule, {
valid: SUPPORTED_TESTING_FRAMEWORKS.flatMap<ValidTestCase>(
valid: SUPPORTED_TESTING_FRAMEWORKS.flatMap<RuleValidTestCase>(
(testingFramework) => [
{
code: `
Expand Down Expand Up @@ -100,7 +106,7 @@ ruleTester.run(RULE_NAME, rule, {
code: `/* related to issue #386 fix
* now all node accessing properties (listed in lib/utils/index.ts, in PROPERTIES_RETURNING_NODES)
* will not be reported by this rule because anything props.something won't be reported.
*/
*/
import { screen } from '${testingFramework}';
function ComponentA(props) {
if (props.firstChild) {
Expand Down Expand Up @@ -142,17 +148,17 @@ ruleTester.run(RULE_NAME, rule, {
// Example from discussions in issue #386
code: `
import { render } from '${testingFramework}';

function Wrapper({ children }) {
// this should NOT be reported
if (children) {
// ...
}

// this should NOT be reported
return <div className="wrapper-class">{children}</div>
};

render(<Wrapper><SomeComponent /></Wrapper>);
expect(screen.getByText('SomeComponent')).toBeInTheDocument();
`,
Expand Down Expand Up @@ -395,5 +401,24 @@ ruleTester.run(RULE_NAME, rule, {
},
],
},
...EVENT_HANDLER_METHODS.map<RuleInvalidTestCase>((method) => ({
code: `
import { screen } from '${testingFramework}';

const button = document.getElementById('submit-btn').${method}();
`,
errors: [
{
line: 4,
column: 33,
messageId: 'noNodeAccess',
},
{
line: 4,
column: 62,
messageId: 'noNodeAccess',
},
],
})),
]),
});