Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
49 changes: 37 additions & 12 deletions lib/rules/no-node-access.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import {
DefinitionType,
type ScopeVariable,
} from '@typescript-eslint/scope-manager';
import { TSESTree, ASTUtils } from '@typescript-eslint/utils';

import { createTestingLibraryRule } from '../create-testing-library-rule';
import { isCallExpression, isMemberExpression } from '../node-utils';
import {
getDeepestIdentifierNode,
getPropertyIdentifierNode,
isCallExpression,
isMemberExpression,
} from '../node-utils';
import {
ALL_RETURNING_NODES,
EVENT_HANDLER_METHODS,
getScope,
resolveToTestingLibraryFn,
} from '../utils';

Expand Down Expand Up @@ -88,24 +98,39 @@ export default createTestingLibraryRule<Options, MessageIds>({
}
}

function detectTestingLibraryFn(
node: TSESTree.CallExpression,
variable: ScopeVariable | null
) {
if (variable && variable.defs.length > 0) {
const def = variable.defs[0];
if (
def.type === DefinitionType.Variable &&
isCallExpression(def.node.init)
) {
return resolveToTestingLibraryFn(def.node.init, context);
}
}

return resolveToTestingLibraryFn(node, context);
}

return {
CallExpression(node: TSESTree.CallExpression) {
const { callee } = node;
const property = isMemberExpression(callee) ? callee.property : null;
const object = isMemberExpression(callee) ? callee.object : null;

const propertyName = ASTUtils.isIdentifier(property)
? property.name
: null;
const objectName = ASTUtils.isIdentifier(object) ? object.name : null;
const property = getDeepestIdentifierNode(node);
const identifier = getPropertyIdentifierNode(node);

const isEventHandlerMethod = EVENT_HANDLER_METHODS.some(
(method) => method === propertyName
(method) => method === property?.name
);
const hasUserEventInstanceName = userEventInstanceNames.has(
objectName ?? ''
identifier?.name ?? ''
);
const testingLibraryFn = resolveToTestingLibraryFn(node, context);

const variable = identifier
? ASTUtils.findVariable(getScope(context, node), identifier)
: null;
const testingLibraryFn = detectTestingLibraryFn(node, variable);

if (
!testingLibraryFn &&
Expand Down
43 changes: 42 additions & 1 deletion tests/lib/rules/no-node-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
// case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
import { screen } from '${testingFramework}';

const ui = {
Expand All @@ -251,6 +250,48 @@ ruleTester.run(RULE_NAME, rule, {
const select = ui.select.get();
expect(select).toHaveClass(selectClasses.select);
});
`,
},
{
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
// case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
import { screen, render } from 'test-utils';
import MyComponent from './MyComponent'

test('...', async () => {
const { user } = render(<MyComponent />)
await user.click(screen.getByRole("button"))
});
`,
},
{
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
// case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
import { screen, render } from 'test-utils';
import MyComponent from './MyComponent'

test('...', async () => {
const result = render(<MyComponent />)
await result.user.click(screen.getByRole("button"))
});
`,
},
{
settings: {
'testing-library/utils-module': 'TestUtils',
'testing-library/custom-renders': ['renderComponent'],
},
code: `
// case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
import { screen, renderComponent } from './TestUtils';
import MyComponent from './MyComponent'

test('...', async () => {
const result = renderComponent(<MyComponent />)
await result.user.click(screen.getByRole("button"))
});
`,
},
]
Expand Down