From 4966bcd18a0d894421faa580922a911f3eac723a Mon Sep 17 00:00:00 2001 From: nathanmmiller <37555055+nathanmmiller@users.noreply.github.com> Date: Mon, 16 Jan 2023 17:30:07 -0500 Subject: [PATCH 1/2] fix: ignore getBy inside of within for prefer-presence-queries on absence queries --- lib/rules/prefer-presence-queries.ts | 6 ++++++ tests/lib/rules/prefer-presence-queries.test.ts | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/rules/prefer-presence-queries.ts b/lib/rules/prefer-presence-queries.ts index 9ec96c38..3f0e4315 100644 --- a/lib/rules/prefer-presence-queries.ts +++ b/lib/rules/prefer-presence-queries.ts @@ -59,6 +59,12 @@ export default createTestingLibraryRule({ return { 'CallExpression Identifier'(node: TSESTree.Identifier) { const expectCallNode = findClosestCallNode(node, 'expect'); + const withinCallNode = findClosestCallNode(node, 'within'); + + if (withinCallNode) { + //Ignore false-positives where our query is inside a within + return; + } if (!expectCallNode || !isMemberExpression(expectCallNode.parent)) { return; diff --git a/tests/lib/rules/prefer-presence-queries.test.ts b/tests/lib/rules/prefer-presence-queries.test.ts index 2b5187f2..5846d23d 100644 --- a/tests/lib/rules/prefer-presence-queries.test.ts +++ b/tests/lib/rules/prefer-presence-queries.test.ts @@ -837,6 +837,9 @@ 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() + `, ], invalid: [ // cases: asserting absence incorrectly with `getBy*` queries @@ -1199,5 +1202,11 @@ ruleTester.run(RULE_NAME, rule, { `, errors: [{ line: 4, column: 14, messageId: 'wrongAbsenceQuery' }], }, + { + code: ` + // case: asserting within check does still work with proper outer clause + expect(within(screen.getByRole("button")).getByText("Hello")).not.toBeInTheDocument()`, + errors: [{ line: 3, column: 46, messageId: 'wrongAbsenceQuery' }], + }, ], }); From 21e0e3a1c2dd164f40f93974158a5f9b08c08416 Mon Sep 17 00:00:00 2001 From: nathanmmiller <37555055+nathanmmiller@users.noreply.github.com> Date: Mon, 16 Jan 2023 17:30:07 -0500 Subject: [PATCH 2/2] fix: ignore getBy inside of within for prefer-presence-queries on absence queries --- lib/rules/prefer-presence-queries.ts | 14 +++++- .../lib/rules/prefer-presence-queries.test.ts | 48 +++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/lib/rules/prefer-presence-queries.ts b/lib/rules/prefer-presence-queries.ts index 9ec96c38..dd816408 100644 --- a/lib/rules/prefer-presence-queries.ts +++ b/lib/rules/prefer-presence-queries.ts @@ -59,6 +59,7 @@ export default createTestingLibraryRule({ return { 'CallExpression Identifier'(node: TSESTree.Identifier) { const expectCallNode = findClosestCallNode(node, 'expect'); + const withinCallNode = findClosestCallNode(node, 'within'); if (!expectCallNode || !isMemberExpression(expectCallNode.parent)) { return; @@ -79,9 +80,18 @@ export default createTestingLibraryRule({ 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' }); } }, diff --git a/tests/lib/rules/prefer-presence-queries.test.ts b/tests/lib/rules/prefer-presence-queries.test.ts index 2b5187f2..a5e8b3c9 100644 --- a/tests/lib/rules/prefer-presence-queries.test.ts +++ b/tests/lib/rules/prefer-presence-queries.test.ts @@ -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 @@ -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' }, + ], + }, ], });