Skip to content

Commit

Permalink
fix(no-node-access): false positives with props.children (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjarva authored Oct 2, 2022
1 parent 0ae1f25 commit 5f4287f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/rules/no-node-access.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ const signinModal = getByLabelText('Sign In');
within(signinModal).getByPlaceholderText('Username');
```

```js
import { screen } from '@testing-library/react';

function ComponentA(props) {
// props.children is not reported
return <div>{props.children}</div>;
}

render(<ComponentA />);
```

```js
// If is not importing a testing-library package

Expand Down
7 changes: 7 additions & 0 deletions lib/rules/no-node-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
return;
}

if (
ASTUtils.isIdentifier(node.object) &&
node.object.name === 'props'
) {
return;
}

context.report({
node,
loc: node.property.loc.start,
Expand Down
35 changes: 35 additions & 0 deletions tests/lib/rules/no-node-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,41 @@ ruleTester.run(RULE_NAME, rule, {
);
`,
},
{
code: `// issue #386 examples, props.children should not be reported
import { screen } from '${testingFramework}';
jest.mock('@/some/path', () => ({
someProperty: jest.fn((props) => props.children),
}));
`,
},
{
code: `// issue #386 examples
import { screen } from '${testingFramework}';
function ComponentA(props) {
if (props.children) {
// ...
}
return <div>{props.children}</div>
}
`,
},
{
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) {
// ...
}
return <div>{props.nextSibling}</div>
}
`,
},
{
settings: {
'testing-library/utils-module': 'test-utils',
Expand Down

0 comments on commit 5f4287f

Please sign in to comment.