8
8
//------------------------------------------------------------------------------
9
9
10
10
import { RuleTester } from "eslint" ;
11
- import { queries , queriesByVariant } from "../../../queries" ;
12
11
import * as rule from "../../../rules/prefer-in-document" ;
13
12
14
13
//------------------------------------------------------------------------------
@@ -28,17 +27,29 @@ function invalidCase(code, output) {
28
27
}
29
28
30
29
const valid = [
31
- ...queries . map ( ( q ) => [
30
+ ...[ "getByText" , "getByRole" ] . map ( ( q ) => [
32
31
`expect(screen.${ q } ('foo')).toBeInTheDocument()` ,
33
32
`expect(${ q } ('foo')).toBeInTheDocument()` ,
34
33
`expect(wrapper.${ q } ('foo')).toBeInTheDocument()` ,
34
+ `let foo;
35
+ foo = screen.${ q } ('foo');
36
+ foo = somethingElse;
37
+ expect(foo).toHaveLength(1);` ,
35
38
] ) ,
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);` ,
36
47
`expect(screen.notAQuery('foo-bar')).toHaveLength(1)` ,
37
- `expect(screen.getByText ('foo-bar')).toHaveLength(2)` ,
48
+ `expect(screen.getAllByText ('foo-bar')).toHaveLength(2)` ,
38
49
] ;
39
50
const invalid = [
40
51
// Invalid cases that applies to all variants
41
- ...queries . map ( ( q ) => [
52
+ ...[ "getByText" , "getAllByRole" ] . map ( ( q ) => [
42
53
invalidCase (
43
54
`expect(screen.${ q } ('foo')).toHaveLength(1)` ,
44
55
`expect(screen.${ q } ('foo')).toBeInTheDocument()`
@@ -51,9 +62,37 @@ const invalid = [
51
62
`expect(wrapper.${ q } ('foo')).toHaveLength(1)` ,
52
63
`expect(wrapper.${ q } ('foo')).toBeInTheDocument()`
53
64
) ,
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
+ ) ,
54
93
] ) ,
55
94
// Invalid cases that applies to queryBy* and queryAllBy*
56
- ...queriesByVariant . query . map ( ( q ) => [
95
+ ...[ "queryByText" , "queryAllByText" ] . map ( ( q ) => [
57
96
invalidCase (
58
97
`expect(${ q } ('foo')).toHaveLength(0)` ,
59
98
`expect(${ q } ('foo')).not.toBeInTheDocument()`
@@ -66,18 +105,120 @@ const invalid = [
66
105
`expect(${ q } ('foo')).not.toBeNull()` ,
67
106
`expect(${ q } ('foo')).toBeInTheDocument()`
68
107
) ,
108
+ invalidCase (
109
+ `expect(${ q } ('foo')) .not .toBeNull()` ,
110
+ `expect(${ q } ('foo')).toBeInTheDocument()`
111
+ ) ,
69
112
invalidCase (
70
113
`expect(${ q } ('foo')).toBeDefined()` ,
71
114
`expect(${ q } ('foo')).toBeInTheDocument()`
72
115
) ,
73
116
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();`
76
141
) ,
77
142
] ) ,
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
+ ) ,
78
219
] ;
79
220
80
- const ruleTester = new RuleTester ( { parserOptions : { ecmaVersion : 2015 } } ) ;
221
+ const ruleTester = new RuleTester ( { parserOptions : { ecmaVersion : 2017 } } ) ;
81
222
ruleTester . run ( "prefer-in-document" , rule , {
82
223
valid : [ ] . concat ( ...valid ) ,
83
224
invalid : [ ] . concat ( ...invalid ) ,
0 commit comments