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

fix(prefer-find-by): Autofix takes wait query's options into account #679

Merged
merged 2 commits into from
Oct 21, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/rules/prefer-find-by.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Suggest using `findBy*` methods instead of the `waitFor` + `getBy` queries (`testing-library/prefer-find-by`)

findBy* queries are a simple combination of getBy* queries and waitFor. The findBy\* queries accept the waitFor options as the last argument. (i.e. screen.findByText('text', queryOptions, waitForOptions))
`findBy*` queries are a simple combination of `getBy*` queries and `waitFor`. The `findBy*` queries accept the `waitFor` options as the last argument. (i.e. `screen.findByText('text', queryOptions, waitForOptions)`)
Belco90 marked this conversation as resolved.
Show resolved Hide resolved

## Rule details

This rule aims to use `findBy*` or `findAllBy*` queries to wait for elements, rather than using `waitFor`, or the deprecated methods `waitForElement` and `wait`.
This rule analyzes those cases where `waitFor` is used with just one query method, in the form of an arrow function with only one statement (that is, without a block of statements). Given the callback could be more complex, this rule does not consider function callbacks or arrow functions with blocks of code
This rule analyzes those cases where `waitFor` is used with just one query method, in the form of an arrow function with only one statement (that is, without a block of statements). Given the callback could be more complex, this rule does not consider function callbacks or arrow functions with blocks of code.

Examples of **incorrect** code for this rule

Expand Down Expand Up @@ -78,7 +78,7 @@ await waitFor(() => expect(getAllByText('bar')).toBeDisabled());

## When Not To Use It

- Not encouraging use of findBy shortcut from testing library best practices
- Not encouraging use of `findBy` shortcut from testing library best practices

## Further Reading

Expand Down
10 changes: 9 additions & 1 deletion lib/rules/prefer-find-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isArrowFunctionExpression,
isCallExpression,
isMemberExpression,
isObjectExpression,
isObjectPattern,
isProperty,
} from '../node-utils';
Expand Down Expand Up @@ -366,6 +367,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
return;
}

// if there is a second argument to AwaitExpression, it is the options
const waitOptions = node.arguments[1];
let waitOptionsSourceCode = '';
if (isObjectExpression(waitOptions)) {
waitOptionsSourceCode = `, ${sourceCode.getText(waitOptions)}`;
}

const queryVariant = getFindByQueryVariant(fullQueryMethod);
const callArguments = getQueryArguments(argument.body);
const queryMethod = fullQueryMethod.split('By')[1];
Expand All @@ -389,7 +397,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
}
const newCode = `${caller}.${queryVariant}${queryMethod}(${callArguments
.map((callArgNode) => sourceCode.getText(callArgNode))
.join(', ')})`;
.join(', ')}${waitOptionsSourceCode})`;
return fixer.replaceText(node, newCode);
},
});
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/rules/prefer-find-by.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,29 @@ ruleTester.run(RULE_NAME, rule, {
queryMethod
)}('foo', { name: 'baz' })
})
`,
})),
// Issue #579, https://github.com/testing-library/eslint-plugin-testing-library/issues/579
// findBy can have two sets of options: await screen.findByText('text', queryOptions, waitForOptions)
...createScenario((waitMethod: string, queryMethod: string) => ({
Belco90 marked this conversation as resolved.
Show resolved Hide resolved
code: `import {${waitMethod}} from '${testingFramework}';
const button = await ${waitMethod}(() => screen.${queryMethod}('Count is: 0'), { timeout: 100, interval: 200 })
`,
errors: [
{
messageId: 'preferFindBy',
data: {
queryVariant: getFindByQueryVariant(queryMethod),
queryMethod: queryMethod.split('By')[1],
prevQuery: queryMethod,
waitForMethodName: waitMethod,
},
},
],
output: `import {${waitMethod}} from '${testingFramework}';
const button = await screen.${buildFindByMethod(
queryMethod
)}('Count is: 0', { timeout: 100, interval: 200 })
`,
})),
]),
Expand Down