From f5da58e7ecb40485f41528f33934db718b808c29 Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Sat, 4 Feb 2023 07:22:32 -0500 Subject: [PATCH] feat: add toBeVisible as a presence query --- lib/utils/index.ts | 7 +- .../lib/rules/prefer-presence-queries.test.ts | 142 +++++++++++++++++- 2 files changed, 147 insertions(+), 2 deletions(-) diff --git a/lib/utils/index.ts b/lib/utils/index.ts index adaef168..32efe74f 100644 --- a/lib/utils/index.ts +++ b/lib/utils/index.ts @@ -116,7 +116,12 @@ const ALL_RETURNING_NODES = [ ...METHODS_RETURNING_NODES, ]; -const PRESENCE_MATCHERS = ['toBeInTheDocument', 'toBeTruthy', 'toBeDefined']; +const PRESENCE_MATCHERS = [ + 'toBeInTheDocument', + 'toBeVisible', + 'toBeTruthy', + 'toBeDefined', +]; const ABSENCE_MATCHERS = ['toBeNull', 'toBeFalsy']; export { diff --git a/tests/lib/rules/prefer-presence-queries.test.ts b/tests/lib/rules/prefer-presence-queries.test.ts index 2b5187f2..bafc424e 100644 --- a/tests/lib/rules/prefer-presence-queries.test.ts +++ b/tests/lib/rules/prefer-presence-queries.test.ts @@ -138,6 +138,11 @@ ruleTester.run(RULE_NAME, rule, { matcher: '.toBeInTheDocument()', assertionType: 'presence', }), + ...getValidAssertions({ + query: queryName, + matcher: '.toBeVisible()', + assertionType: 'presence', + }), ...getValidAssertions({ query: queryName, matcher: '.toBeTruthy()', @@ -191,6 +196,12 @@ ruleTester.run(RULE_NAME, rule, { shouldUseScreen: true, assertionType: 'presence', }), + ...getValidAssertions({ + query: queryName, + matcher: '.toBeVisible()', + shouldUseScreen: true, + assertionType: 'presence', + }), ...getValidAssertions({ query: queryName, matcher: '.toBeTruthy()', @@ -251,6 +262,11 @@ ruleTester.run(RULE_NAME, rule, { matcher: '.toBeInTheDocument()', assertionType: 'presence', }), + ...getValidAssertions({ + query: queryName, + matcher: '.toBeVisible()', + assertionType: 'presence', + }), ...getValidAssertions({ query: queryName, matcher: '.toBeTruthy()', @@ -304,6 +320,12 @@ ruleTester.run(RULE_NAME, rule, { shouldUseScreen: true, assertionType: 'presence', }), + ...getValidAssertions({ + query: queryName, + matcher: '.toBeVisible()', + shouldUseScreen: true, + assertionType: 'presence', + }), ...getValidAssertions({ query: queryName, matcher: '.toBeTruthy()', @@ -374,6 +396,11 @@ ruleTester.run(RULE_NAME, rule, { matcher: '.not.toBeInTheDocument()', assertionType: 'absence', }), + ...getValidAssertions({ + query: queryName, + matcher: '.not.toBeVisible()', + assertionType: 'absence', + }), ...getValidAssertions({ query: queryName, matcher: '.not.toBeTruthy()', @@ -419,6 +446,12 @@ ruleTester.run(RULE_NAME, rule, { shouldUseScreen: true, assertionType: 'absence', }), + ...getValidAssertions({ + query: queryName, + matcher: '.not.toBeVisible()', + shouldUseScreen: true, + assertionType: 'absence', + }), ...getValidAssertions({ query: queryName, matcher: '.not.toBeTruthy()', @@ -465,6 +498,11 @@ ruleTester.run(RULE_NAME, rule, { matcher: '.not.toBeInTheDocument()', assertionType: 'absence', }), + ...getValidAssertions({ + query: queryName, + matcher: '.not.toBeVisible()', + assertionType: 'absence', + }), ...getValidAssertions({ query: queryName, matcher: '.not.toBeTruthy()', @@ -510,6 +548,12 @@ ruleTester.run(RULE_NAME, rule, { shouldUseScreen: true, assertionType: 'absence', }), + ...getValidAssertions({ + query: queryName, + matcher: '.not.toBeVisible()', + shouldUseScreen: true, + assertionType: 'absence', + }), ...getValidAssertions({ query: queryName, matcher: '.not.toBeTruthy()', @@ -557,6 +601,11 @@ ruleTester.run(RULE_NAME, rule, { matcher: '.not.toBeInTheDocument', assertionType: 'absence', }), + getDisabledValidAssertion({ + query: queryName, + matcher: '.not.toBeVisible', + assertionType: 'absence', + }), getDisabledValidAssertion({ query: queryName, matcher: '.not.toBeTruthy()', @@ -592,6 +641,12 @@ ruleTester.run(RULE_NAME, rule, { shouldUseScreen: true, assertionType: 'absence', }), + getDisabledValidAssertion({ + query: queryName, + matcher: '.not.toBeVisible', + shouldUseScreen: true, + assertionType: 'absence', + }), getDisabledValidAssertion({ query: queryName, matcher: '.not.toBeTruthy()', @@ -626,6 +681,11 @@ ruleTester.run(RULE_NAME, rule, { matcher: '.not.toBeInTheDocument', assertionType: 'absence', }), + getDisabledValidAssertion({ + query: queryName, + matcher: '.not.toBeVisible', + assertionType: 'absence', + }), getDisabledValidAssertion({ query: queryName, matcher: '.not.toBeTruthy()', @@ -661,6 +721,12 @@ ruleTester.run(RULE_NAME, rule, { shouldUseScreen: true, assertionType: 'absence', }), + getDisabledValidAssertion({ + query: queryName, + matcher: '.not.toBeVisible', + shouldUseScreen: true, + assertionType: 'absence', + }), getDisabledValidAssertion({ query: queryName, matcher: '.not.toBeTruthy()', @@ -695,6 +761,11 @@ ruleTester.run(RULE_NAME, rule, { matcher: '.toBeInTheDocument()', assertionType: 'presence', }), + getDisabledValidAssertion({ + query: queryName, + matcher: '.toBeVisible()', + assertionType: 'presence', + }), getDisabledValidAssertion({ query: queryName, matcher: '.not.toBeFalsy()', @@ -730,6 +801,12 @@ ruleTester.run(RULE_NAME, rule, { shouldUseScreen: true, assertionType: 'presence', }), + getDisabledValidAssertion({ + query: queryName, + matcher: '.toBeVisible()', + shouldUseScreen: true, + assertionType: 'presence', + }), getDisabledValidAssertion({ query: queryName, matcher: '.not.toBeFalsy()', @@ -764,6 +841,11 @@ ruleTester.run(RULE_NAME, rule, { matcher: '.toBeInTheDocument()', assertionType: 'presence', }), + getDisabledValidAssertion({ + query: queryName, + matcher: '.toBeVisible()', + assertionType: 'presence', + }), getDisabledValidAssertion({ query: queryName, matcher: '.not.toBeFalsy()', @@ -799,6 +881,12 @@ ruleTester.run(RULE_NAME, rule, { shouldUseScreen: true, assertionType: 'presence', }), + getDisabledValidAssertion({ + query: queryName, + matcher: '.toBeVisible()', + shouldUseScreen: true, + assertionType: 'presence', + }), getDisabledValidAssertion({ query: queryName, matcher: '.not.toBeFalsy()', @@ -833,7 +921,7 @@ ruleTester.run(RULE_NAME, rule, { // submit button exists const submitButton = screen.getByRole('button') fireEvent.click(submitButton) - + // right after clicking submit button it disappears expect(submitButton).not.toBeInTheDocument() `, @@ -861,6 +949,12 @@ ruleTester.run(RULE_NAME, rule, { messageId: 'wrongAbsenceQuery', assertionType: 'absence', }), + ...getInvalidAssertions({ + query: queryName, + matcher: '.not.toBeVisible()', + messageId: 'wrongAbsenceQuery', + assertionType: 'absence', + }), ...getInvalidAssertions({ query: queryName, matcher: '.not.toBeTruthy()', @@ -901,6 +995,13 @@ ruleTester.run(RULE_NAME, rule, { shouldUseScreen: true, assertionType: 'absence', }), + ...getInvalidAssertions({ + query: queryName, + matcher: '.not.toBeVisible()', + messageId: 'wrongAbsenceQuery', + shouldUseScreen: true, + assertionType: 'absence', + }), ...getInvalidAssertions({ query: queryName, matcher: '.not.toBeTruthy()', @@ -940,6 +1041,12 @@ ruleTester.run(RULE_NAME, rule, { messageId: 'wrongAbsenceQuery', assertionType: 'absence', }), + ...getInvalidAssertions({ + query: queryName, + matcher: '.not.toBeVisible()', + messageId: 'wrongAbsenceQuery', + assertionType: 'absence', + }), ...getInvalidAssertions({ query: queryName, matcher: '.not.toBeTruthy()', @@ -980,6 +1087,13 @@ ruleTester.run(RULE_NAME, rule, { shouldUseScreen: true, assertionType: 'absence', }), + ...getInvalidAssertions({ + query: queryName, + matcher: '.not.toBeVisible()', + messageId: 'wrongAbsenceQuery', + shouldUseScreen: true, + assertionType: 'absence', + }), ...getInvalidAssertions({ query: queryName, matcher: '.not.toBeTruthy()', @@ -1019,6 +1133,12 @@ ruleTester.run(RULE_NAME, rule, { messageId: 'wrongPresenceQuery', assertionType: 'presence', }), + ...getInvalidAssertions({ + query: queryName, + matcher: '.toBeVisible()', + messageId: 'wrongPresenceQuery', + assertionType: 'presence', + }), ...getInvalidAssertions({ query: queryName, matcher: '.not.toBeFalsy()', @@ -1059,6 +1179,13 @@ ruleTester.run(RULE_NAME, rule, { shouldUseScreen: true, assertionType: 'presence', }), + ...getInvalidAssertions({ + query: queryName, + matcher: '.toBeVisible()', + messageId: 'wrongPresenceQuery', + shouldUseScreen: true, + assertionType: 'presence', + }), ...getInvalidAssertions({ query: queryName, matcher: '.not.toBeFalsy()', @@ -1098,6 +1225,12 @@ ruleTester.run(RULE_NAME, rule, { messageId: 'wrongPresenceQuery', assertionType: 'presence', }), + ...getInvalidAssertions({ + query: queryName, + matcher: '.toBeVisible()', + messageId: 'wrongPresenceQuery', + assertionType: 'presence', + }), ...getInvalidAssertions({ query: queryName, matcher: '.not.toBeFalsy()', @@ -1138,6 +1271,13 @@ ruleTester.run(RULE_NAME, rule, { shouldUseScreen: true, assertionType: 'presence', }), + ...getInvalidAssertions({ + query: queryName, + matcher: '.toBeVisible()', + messageId: 'wrongPresenceQuery', + shouldUseScreen: true, + assertionType: 'presence', + }), ...getInvalidAssertions({ query: queryName, matcher: '.not.toBeFalsy()',