Skip to content

feat(prefer-screen-queries): detect render in wrappers correctly #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
81 changes: 53 additions & 28 deletions lib/rules/prefer-screen-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { ASTUtils, TSESTree } from '@typescript-eslint/experimental-utils';

import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
getDeepestIdentifierNode,
getFunctionName,
getInnermostReturningFunction,
isCallExpression,
isMemberExpression,
isObjectExpression,
Expand Down Expand Up @@ -54,6 +57,22 @@ export default createTestingLibraryRule<Options, MessageIds>({
defaultOptions: [],

create(context, _, helpers) {
const renderWrapperNames: string[] = [];

function detectRenderWrapper(node: TSESTree.Identifier): void {
const innerFunction = getInnermostReturningFunction(context, node);

if (innerFunction) {
renderWrapperNames.push(getFunctionName(innerFunction));
}
}

function isReportableRender(node: TSESTree.Identifier): boolean {
return (
helpers.isRenderUtil(node) || renderWrapperNames.includes(node.name)
);
}

function reportInvalidUsage(node: TSESTree.Identifier) {
context.report({
node,
Expand All @@ -78,6 +97,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
}
}

function isIdentifierAllowed(name: string) {
return ['screen', ...withinDeclaredVariables].includes(name);
}

// keep here those queries which are safe and shouldn't be reported
// (from within, from render + container/base element, not related to TL, etc)
const safeDestructuredQueries: string[] = [];
Expand All @@ -93,7 +116,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
return;
}

const isComingFromValidRender = helpers.isRenderUtil(node.init.callee);
const isComingFromValidRender = isReportableRender(node.init.callee);

if (!isComingFromValidRender) {
// save the destructured query methods as safe since they are coming
Expand All @@ -113,52 +136,54 @@ export default createTestingLibraryRule<Options, MessageIds>({
// save the destructured query methods as safe since they are coming
// from within or render + base/container options
saveSafeDestructuredQueries(node);
return;
}

if (ASTUtils.isIdentifier(node.id)) {
} else if (ASTUtils.isIdentifier(node.id)) {
withinDeclaredVariables.push(node.id.name);
}
},
'CallExpression > Identifier'(node: TSESTree.Identifier) {
if (!helpers.isBuiltInQuery(node)) {
CallExpression(node) {
const identifierNode = getDeepestIdentifierNode(node);

if (!identifierNode) {
return;
}

if (
!safeDestructuredQueries.some((queryName) => queryName === node.name)
) {
reportInvalidUsage(node);
if (helpers.isRenderUtil(identifierNode)) {
detectRenderWrapper(identifierNode);
}
},
'MemberExpression > Identifier'(node: TSESTree.Identifier) {
function isIdentifierAllowed(name: string) {
return ['screen', ...withinDeclaredVariables].includes(name);

if (!helpers.isBuiltInQuery(identifierNode)) {
return;
}

if (!helpers.isBuiltInQuery(node)) {
if (!isMemberExpression(identifierNode.parent)) {
const isSafeDestructuredQuery = safeDestructuredQueries.some(
(queryName) => queryName === identifierNode.name
);
if (isSafeDestructuredQuery) {
return;
}

reportInvalidUsage(identifierNode);
return;
}

const memberExpressionNode = identifierNode.parent;
if (
ASTUtils.isIdentifier(node) &&
isMemberExpression(node.parent) &&
isCallExpression(node.parent.object) &&
ASTUtils.isIdentifier(node.parent.object.callee) &&
node.parent.object.callee.name !== 'within' &&
helpers.isRenderUtil(node.parent.object.callee) &&
!usesContainerOrBaseElement(node.parent.object)
isCallExpression(memberExpressionNode.object) &&
ASTUtils.isIdentifier(memberExpressionNode.object.callee) &&
memberExpressionNode.object.callee.name !== 'within' &&
isReportableRender(memberExpressionNode.object.callee) &&
!usesContainerOrBaseElement(memberExpressionNode.object)
) {
reportInvalidUsage(node);
reportInvalidUsage(identifierNode);
return;
}

if (
isMemberExpression(node.parent) &&
ASTUtils.isIdentifier(node.parent.object) &&
!isIdentifierAllowed(node.parent.object.name)
ASTUtils.isIdentifier(memberExpressionNode.object) &&
!isIdentifierAllowed(memberExpressionNode.object.name)
) {
reportInvalidUsage(node);
reportInvalidUsage(identifierNode);
}
},
};
Expand Down
81 changes: 78 additions & 3 deletions tests/lib/rules/prefer-screen-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ruleTester.run(RULE_NAME, rule, {
(query) => `
import { render } from '@testing-library/react'
import { ${query} } from 'custom-queries'

test("imported custom queries, since they can't be used through screen", () => {
render(foo)
${query}('bar')
Expand All @@ -58,7 +58,7 @@ ruleTester.run(RULE_NAME, rule, {
...CUSTOM_QUERY_COMBINATIONS.map(
(query) => `
import { render } from '@testing-library/react'

test("render-returned custom queries, since they can't be used through screen", () => {
const { ${query} } = render(foo)
${query}('bar')
Expand All @@ -71,7 +71,7 @@ ruleTester.run(RULE_NAME, rule, {
},
code: `
import { render } from '@testing-library/react'

test("custom queries + custom-queries setting, since they can't be used through screen", () => {
const { ${query} } = render(foo)
${query}('bar')
Expand Down Expand Up @@ -413,5 +413,80 @@ ruleTester.run(RULE_NAME, rule, {
],
} as const)
),
{
code: ` // issue #367 - example A
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two scenarios provided by @julienw added as test cases.

import { render } from '@testing-library/react';

function setup() {
return render(<div />);
}

it('foo', async () => {
const { getByText } = await setup();
expect(getByText('foo')).toBeInTheDocument();
});

it('bar', () => {
const { getByText } = setup();
expect(getByText('foo')).toBeInTheDocument();
});
`,
errors: [
{
messageId: 'preferScreenQueries',
line: 10,
column: 16,
data: {
name: 'getByText',
},
},
{
messageId: 'preferScreenQueries',
line: 15,
column: 16,
data: {
name: 'getByText',
},
},
],
},
{
code: ` // issue #367 - example B
import { render } from '@testing-library/react';

function setup() {
return render(<div />);
}

it('foo', () => {
const { getByText } = setup();
expect(getByText('foo')).toBeInTheDocument();
});

it('bar', () => {
const results = setup();
const { getByText } = results;
expect(getByText('foo')).toBe('foo');
});
`,
errors: [
{
messageId: 'preferScreenQueries',
line: 10,
column: 16,
data: {
name: 'getByText',
},
},
{
messageId: 'preferScreenQueries',
line: 16,
column: 16,
data: {
name: 'getByText',
},
},
],
},
],
});