88//------------------------------------------------------------------------------
99
1010import { RuleTester } from "eslint" ;
11- import { queries , queriesByVariant } from "../../../queries" ;
1211import * as rule from "../../../rules/prefer-in-document" ;
1312
1413//------------------------------------------------------------------------------
@@ -28,17 +27,29 @@ function invalidCase(code, output) {
2827}
2928
3029const valid = [
31- ...queries . map ( ( q ) => [
30+ ...[ "getByText" , "getByRole" ] . map ( ( q ) => [
3231 `expect(screen.${ q } ('foo')).toBeInTheDocument()` ,
3332 `expect(${ q } ('foo')).toBeInTheDocument()` ,
3433 `expect(wrapper.${ q } ('foo')).toBeInTheDocument()` ,
34+ `let foo;
35+ foo = screen.${ q } ('foo');
36+ foo = somethingElse;
37+ expect(foo).toHaveLength(1);` ,
3538 ] ) ,
39+ `let foo;
40+ foo = "bar";
41+ expect(foo).toHaveLength(1);` ,
42+ `let foo;
43+ foo = "bar";
44+ expect(foo).toHaveLength(0);` ,
45+ `let foo;
46+ expect(foo).toHaveLength(1);` ,
3647 `expect(screen.notAQuery('foo-bar')).toHaveLength(1)` ,
37- `expect(screen.getByText ('foo-bar')).toHaveLength(2)` ,
48+ `expect(screen.getAllByText ('foo-bar')).toHaveLength(2)` ,
3849] ;
3950const invalid = [
4051 // Invalid cases that applies to all variants
41- ...queries . map ( ( q ) => [
52+ ...[ "getByText" , "getAllByRole" ] . map ( ( q ) => [
4253 invalidCase (
4354 `expect(screen.${ q } ('foo')).toHaveLength(1)` ,
4455 `expect(screen.${ q } ('foo')).toBeInTheDocument()`
@@ -51,9 +62,37 @@ const invalid = [
5162 `expect(wrapper.${ q } ('foo')).toHaveLength(1)` ,
5263 `expect(wrapper.${ q } ('foo')).toBeInTheDocument()`
5364 ) ,
65+ invalidCase (
66+ `const foo = screen.${ q } ('foo');
67+ expect(foo).toHaveLength(1);` ,
68+ `const foo = screen.${ q } ('foo');
69+ expect(foo).toBeInTheDocument();`
70+ ) ,
71+ invalidCase (
72+ `const foo = ${ q } ('foo');
73+ expect(foo).toHaveLength(1);` ,
74+ `const foo = ${ q } ('foo');
75+ expect(foo).toBeInTheDocument();`
76+ ) ,
77+ invalidCase (
78+ `let foo;
79+ foo = ${ q } ('foo');
80+ expect(foo).toHaveLength(1);` ,
81+ `let foo;
82+ foo = ${ q } ('foo');
83+ expect(foo).toBeInTheDocument();`
84+ ) ,
85+ invalidCase (
86+ `let foo;
87+ foo = screen.${ q } ('foo');
88+ expect(foo).toHaveLength(1);` ,
89+ `let foo;
90+ foo = screen.${ q } ('foo');
91+ expect(foo).toBeInTheDocument();`
92+ ) ,
5493 ] ) ,
5594 // Invalid cases that applies to queryBy* and queryAllBy*
56- ...queriesByVariant . query . map ( ( q ) => [
95+ ...[ "queryByText" , "queryAllByText" ] . map ( ( q ) => [
5796 invalidCase (
5897 `expect(${ q } ('foo')).toHaveLength(0)` ,
5998 `expect(${ q } ('foo')).not.toBeInTheDocument()`
@@ -66,18 +105,120 @@ const invalid = [
66105 `expect(${ q } ('foo')).not.toBeNull()` ,
67106 `expect(${ q } ('foo')).toBeInTheDocument()`
68107 ) ,
108+ invalidCase (
109+ `expect(${ q } ('foo')) .not .toBeNull()` ,
110+ `expect(${ q } ('foo')).toBeInTheDocument()`
111+ ) ,
69112 invalidCase (
70113 `expect(${ q } ('foo')).toBeDefined()` ,
71114 `expect(${ q } ('foo')).toBeInTheDocument()`
72115 ) ,
73116 invalidCase (
74- `expect(${ q } ('foo')).not.toBeDefined()` ,
75- `expect(${ q } ('foo')).not.toBeInTheDocument()`
117+ `expect(${ q } ('foo')) .not .toBeDefined()` ,
118+ `expect(${ q } ('foo')) .not .toBeInTheDocument()`
119+ ) ,
120+ invalidCase (
121+ `let foo;
122+ foo = screen.${ q } ('foo');
123+ expect(foo).toHaveLength(0);` ,
124+ `let foo;
125+ foo = screen.${ q } ('foo');
126+ expect(foo).not.toBeInTheDocument();`
127+ ) ,
128+ invalidCase (
129+ `let foo;
130+ foo = screen.${ q } ('foo');
131+ expect(foo) .not.toBeNull();` ,
132+ `let foo;
133+ foo = screen.${ q } ('foo');
134+ expect(foo).toBeInTheDocument();`
135+ ) ,
136+ invalidCase (
137+ `let foo = screen.${ q } ('foo');
138+ expect(foo).not.toBeNull();` ,
139+ `let foo = screen.${ q } ('foo');
140+ expect(foo).toBeInTheDocument();`
76141 ) ,
77142 ] ) ,
143+ invalidCase (
144+ `it("foo", async () => {
145+ expect(await findByRole("button")).toBeDefined();
146+ })` ,
147+ `it("foo", async () => {
148+ expect(await findByRole("button")).toBeInTheDocument();
149+ })`
150+ ) ,
151+ invalidCase (
152+ `it("foo", async () => {
153+ expect(await findByRole("button")).not.toBeNull();
154+ })` ,
155+ `it("foo", async () => {
156+ expect(await findByRole("button")).toBeInTheDocument();
157+ })`
158+ ) ,
159+ invalidCase (
160+ `it("foo", async () => {
161+ expect(await screen.findByText(/Compressing video/)).toBeDefined();
162+ })` ,
163+ `it("foo", async () => {
164+ expect(await screen.findByText(/Compressing video/)).toBeInTheDocument();
165+ })`
166+ ) ,
167+ invalidCase (
168+ `it("foo", async () => {
169+ expect(await screen.findByText(/Compressing video/)).not.toBeDefined();
170+ })` ,
171+ `it("foo", async () => {
172+ expect(await screen.findByText(/Compressing video/)).not.toBeInTheDocument();
173+ })`
174+ ) ,
175+ invalidCase (
176+ `it("foo", async () => {
177+ const compressingFeedback = await screen.findByText(/Compressing video/);
178+ expect(compressingFeedback).toBeDefined();
179+ });` ,
180+ `it("foo", async () => {
181+ const compressingFeedback = await screen.findByText(/Compressing video/);
182+ expect(compressingFeedback).toBeInTheDocument();
183+ });`
184+ ) ,
185+ invalidCase (
186+ `it("foo", async () => {
187+ const compressingFeedback = await screen.findByText(/Compressing video/);
188+ expect(compressingFeedback).not.toBeNull();
189+ });` ,
190+ `it("foo", async () => {
191+ const compressingFeedback = await screen.findByText(/Compressing video/);
192+ expect(compressingFeedback).toBeInTheDocument();
193+ });`
194+ ) ,
195+ invalidCase (
196+ `it("foo", async () => {
197+ let compressingFeedback;
198+ compressingFeedback = await screen.findByText(/Compressing video/);
199+ expect(compressingFeedback).toBeDefined();
200+ });` ,
201+ `it("foo", async () => {
202+ let compressingFeedback;
203+ compressingFeedback = await screen.findByText(/Compressing video/);
204+ expect(compressingFeedback).toBeInTheDocument();
205+ });`
206+ ) ,
207+ invalidCase (
208+ `it("foo", async () => {
209+ let compressingFeedback;
210+ compressingFeedback = await screen.findByText(/Compressing video/);
211+ expect(compressingFeedback).not.toBeDefined();
212+ });` ,
213+ `it("foo", async () => {
214+ let compressingFeedback;
215+ compressingFeedback = await screen.findByText(/Compressing video/);
216+ expect(compressingFeedback).not.toBeInTheDocument();
217+ });`
218+ ) ,
78219] ;
79220
80- const ruleTester = new RuleTester ( { parserOptions : { ecmaVersion : 2015 } } ) ;
221+ const ruleTester = new RuleTester ( { parserOptions : { ecmaVersion : 2017 } } ) ;
81222ruleTester . run ( "prefer-in-document" , rule , {
82223 valid : [ ] . concat ( ...valid ) ,
83224 invalid : [ ] . concat ( ...invalid ) ,
0 commit comments