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: ignore getBy inside of within for prefer-presence-queries on abs… #717

Closed
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
14 changes: 12 additions & 2 deletions lib/rules/prefer-presence-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
return {
'CallExpression Identifier'(node: TSESTree.Identifier) {
const expectCallNode = findClosestCallNode(node, 'expect');
const withinCallNode = findClosestCallNode(node, 'within');

if (!expectCallNode || !isMemberExpression(expectCallNode.parent)) {
return;
Expand All @@ -79,9 +80,18 @@ export default createTestingLibraryRule<Options, MessageIds>({
return;
}

if (presence && isPresenceAssert && !isPresenceQuery) {
if (
presence &&
(withinCallNode || isPresenceAssert) &&
!isPresenceQuery
) {
context.report({ node, messageId: 'wrongPresenceQuery' });
} else if (absence && isAbsenceAssert && isPresenceQuery) {
} else if (
!withinCallNode &&
absence &&
isAbsenceAssert &&
isPresenceQuery
) {
context.report({ node, messageId: 'wrongAbsenceQuery' });
}
},
Expand Down
48 changes: 48 additions & 0 deletions tests/lib/rules/prefer-presence-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,12 @@ ruleTester.run(RULE_NAME, rule, {
// right after clicking submit button it disappears
expect(submitButton).not.toBeInTheDocument()
`,
`// checking absence on getBy* inside a within with queryBy* outside the within
expect(within(screen.getByRole("button")).queryByText("Hello")).not.toBeInTheDocument()
`,
`// checking presence on getBy* inside a within with getBy* outside the within
expect(within(screen.getByRole("button")).getByText("Hello")).toBeInTheDocument()
`,
],
invalid: [
// cases: asserting absence incorrectly with `getBy*` queries
Expand Down Expand Up @@ -1199,5 +1205,47 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [{ line: 4, column: 14, messageId: 'wrongAbsenceQuery' }],
},
{
code: `
// case: asserting within check does still work with improper outer clause
expect(within(screen.getByRole("button")).getByText("Hello")).not.toBeInTheDocument()`,
errors: [{ line: 3, column: 46, messageId: 'wrongAbsenceQuery' }],
},
{
code: `
// case: asserting within check does still work with improper outer clause
expect(within(screen.getByRole("button")).queryByText("Hello")).toBeInTheDocument()`,
errors: [{ line: 3, column: 46, messageId: 'wrongPresenceQuery' }],
},
{
code: `
// case: asserting within check does still work with improper outer clause and improper inner clause
expect(within(screen.queryByRole("button")).getByText("Hello")).not.toBeInTheDocument()`,
errors: [
{ line: 3, column: 25, messageId: 'wrongPresenceQuery' },
{ line: 3, column: 48, messageId: 'wrongAbsenceQuery' },
],
},
{
code: `
// case: asserting within check does still work with proper outer clause and improper inner clause
expect(within(screen.queryByRole("button")).queryByText("Hello")).not.toBeInTheDocument()`,
errors: [{ line: 3, column: 25, messageId: 'wrongPresenceQuery' }],
},
{
code: `
// case: asserting within check does still work with proper outer clause and improper inner clause
expect(within(screen.queryByRole("button")).getByText("Hello")).toBeInTheDocument()`,
errors: [{ line: 3, column: 25, messageId: 'wrongPresenceQuery' }],
},
{
code: `
// case: asserting within check does still work with improper outer clause and improper inner clause
expect(within(screen.queryByRole("button")).queryByText("Hello")).toBeInTheDocument()`,
errors: [
{ line: 3, column: 25, messageId: 'wrongPresenceQuery' },
{ line: 3, column: 48, messageId: 'wrongPresenceQuery' },
],
},
],
});