Skip to content
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

feat: allow baseElement and container in prefer-screen-queries #201

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
5 changes: 5 additions & 0 deletions docs/rules/prefer-screen-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ const { rerender, unmount, asFragment } = render(<Foo />);
rerender(<Foo />);
asFragment();
unmount();

// using baseElement
const { getByText } = render(<Foo />, { baseElement: treeA });
// using container
const { getAllByText } = render(<Foo />, { container: treeA });
```

## Further Reading
Expand Down
6 changes: 5 additions & 1 deletion lib/node-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';

export function isCallExpression(
node: TSESTree.Node
Expand Down Expand Up @@ -105,4 +105,8 @@ export function hasThenProperty(node: TSESTree.Node) {

export function isArrowFunctionExpression(node: TSESTree.Node): node is TSESTree.ArrowFunctionExpression {
return node && node.type === 'ArrowFunctionExpression'
}

export function isObjectExpression(node: TSESTree.Expression): node is TSESTree.ObjectExpression {
return node?.type === AST_NODE_TYPES.ObjectExpression
gndelia marked this conversation as resolved.
Show resolved Hide resolved
}
22 changes: 18 additions & 4 deletions lib/rules/prefer-screen-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ import {
isCallExpression,
isProperty,
isIdentifier,
isObjectExpression,
} from '../node-utils';

export const RULE_NAME = 'prefer-screen-queries';
export type MessageIds = 'preferScreenQueries';
type Options = [];

const ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING = ['container', 'baseElement']
const ALL_QUERIES_COMBINATIONS_REGEXP = ALL_QUERIES_COMBINATIONS.join('|');

function usesContainerOrBaseElement(node: TSESTree.CallExpression) {
const secondArgument = node.arguments[1]
return isObjectExpression(secondArgument) && secondArgument.properties.some((property) => isProperty(property) && isIdentifier(property.key) && ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING.includes(property.key.name))
}

export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: RULE_NAME,
meta: {
Expand Down Expand Up @@ -50,9 +57,14 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({

return {
VariableDeclarator(node) {
const isWithinFunction = isCallExpression(node.init) && isIdentifier(node.init.callee) && node.init.callee.name === 'within';
if (!isCallExpression(node.init) || !isIdentifier(node.init.callee)) {
return
}
const isWithinFunction = node.init.callee.name === 'within';
// TODO add the custom render option #198
const usesRenderOptions = node.init.callee.name === 'render' && usesContainerOrBaseElement(node.init);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I left this comment as a reminder we need to address custom renders here 😅


if (!isWithinFunction) {
if (!isWithinFunction && !usesRenderOptions) {
return
}

Expand Down Expand Up @@ -94,11 +106,13 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
isMemberExpression(node.parent) &&
isCallExpression(node.parent.object) &&
isIdentifier(node.parent.object.callee) &&
node.parent.object.callee.name !== 'within'
node.parent.object.callee.name !== 'within' &&
node.parent.object.callee.name === 'render' && !usesContainerOrBaseElement(node.parent.object)
) {
reportInvalidUsage(node);
return;
}

if (
isMemberExpression(node.parent) &&
isIdentifier(node.parent.object) &&
Expand All @@ -109,4 +123,4 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
},
};
},
});
});
77 changes: 73 additions & 4 deletions tests/lib/rules/prefer-screen-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const ruleTester = createRuleTester();

ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: `const baz = () => 'foo'`
gndelia marked this conversation as resolved.
Show resolved Hide resolved
},
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
code: `screen.${queryMethod}()`,
})),
Expand Down Expand Up @@ -80,12 +83,55 @@ ruleTester.run(RULE_NAME, rule, {
const utils = render(baz);
utils.unmount();
`
}
},
...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
code: `
const { ${queryMethod} } = render(baz, { baseElement: treeA })
expect(${queryMethod}(baz)).toBeDefined()
`
})),
...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
code: `
const { ${queryMethod}: aliasMethod } = render(baz, { baseElement: treeA })
expect(aliasMethod(baz)).toBeDefined()
`
})),
...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
code: `
const { ${queryMethod} } = render(baz, { container: treeA })
expect(${queryMethod}(baz)).toBeDefined()
`
})),
...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
code: `
const { ${queryMethod}: aliasMethod } = render(baz, { container: treeA })
expect(aliasMethod(baz)).toBeDefined()
`
})),
...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
code: `
const { ${queryMethod} } = render(baz, { baseElement: treeB, container: treeA })
expect(${queryMethod}(baz)).toBeDefined()
`
})),
...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
code: `
const { ${queryMethod}: aliasMethod } = render(baz, { baseElement: treeB, container: treeA })
expect(aliasMethod(baz)).toBeDefined()
`
})),
...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
code: `
render(foo, { baseElement: treeA }).${queryMethod}()
`
}))
],

invalid: [
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
code: `${queryMethod}()`,
code: `
const { ${queryMethod} } = render(foo)
${queryMethod}()`,
errors: [
{
messageId: 'preferScreenQueries',
Expand All @@ -95,7 +141,6 @@ ruleTester.run(RULE_NAME, rule, {
},
],
})),

...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
code: `render().${queryMethod}()`,
errors: [
Expand All @@ -107,7 +152,17 @@ ruleTester.run(RULE_NAME, rule, {
},
],
})),

...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
code: `render(foo, { hydrate: true }).${queryMethod}()`,
errors: [
{
messageId: 'preferScreenQueries',
data: {
name: queryMethod,
},
},
],
})),
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
code: `component.${queryMethod}()`,
errors: [
Expand Down Expand Up @@ -161,6 +216,20 @@ ruleTester.run(RULE_NAME, rule, {
},
],
})),
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
code: `
const { ${queryMethod} } = render(baz, { hydrate: true })
${queryMethod}(baz)
`,
errors: [
{
messageId: 'preferScreenQueries',
data: {
name: queryMethod,
},
},
],
})),
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
code: `
const [myVariable] = within()
Expand Down