@@ -15,14 +15,34 @@ import * as rule from "../../../rules/prefer-to-have-value";
15
15
// Tests
16
16
//------------------------------------------------------------------------------
17
17
18
- const ruleTester = new RuleTester ( { parserOptions : { ecmaVersion : 2015 } } ) ;
18
+ const ruleTester = new RuleTester ( { parserOptions : { ecmaVersion : 2020 } } ) ;
19
19
20
20
const errors = [ { messageId : "use-to-have-value" } ] ;
21
- ruleTester . run ( "prefer-empty " , rule , {
21
+ ruleTester . run ( "prefer-to-have-value " , rule , {
22
22
valid : [
23
23
`expect(element).toHaveValue('foo')` ,
24
24
`expect(element.value).toBeGreaterThan(2);` ,
25
25
`expect(element.value).toBeLessThan(2);` ,
26
+
27
+ `const element = document.getElementById('asdfasf');
28
+ expect(element.value).toEqual('foo');` ,
29
+
30
+ `let element;
31
+ element = someOtherFunction();
32
+ expect(element.value).toStrictEqual('foo');` ,
33
+
34
+ `const element = { value: 'foo' };
35
+ expect(element.value).toBe('foo');` ,
36
+
37
+ `const element = document.getElementById('asdfasf');
38
+ expect(element.value).not.toEqual('foo');` ,
39
+
40
+ `let element;
41
+ element = someOtherFunction();
42
+ expect(element.value).not.toStrictEqual('foo');` ,
43
+
44
+ `const element = { value: 'foo' };
45
+ expect(element.value).not.toBe('foo');` ,
26
46
] ,
27
47
invalid : [
28
48
{
@@ -45,35 +65,159 @@ ruleTester.run("prefer-empty", rule, {
45
65
errors,
46
66
output : `expect(element).not.toHaveValue("foo")` ,
47
67
} ,
68
+ //==========================================================================
48
69
{
49
- code : `expect(element. value).toBe(' foo' )` ,
70
+ code : `expect(screen.getByRole("textbox"). value).toEqual(" foo" )` ,
50
71
errors,
51
- output : `expect(element) .toHaveValue(' foo' )` ,
72
+ output : `expect(screen.getByRole("textbox")) .toHaveValue(" foo" )` ,
52
73
} ,
53
74
{
54
- code : `expect(element. value).toEqual(' foo' )` ,
75
+ code : `expect(screen.queryByRole("dropdown"). value).toEqual(" foo" )` ,
55
76
errors,
56
- output : `expect(element) .toHaveValue(' foo' )` ,
77
+ output : `expect(screen.queryByRole("dropdown")) .toHaveValue(" foo" )` ,
57
78
} ,
58
79
{
59
- code : `expect(element. value).toStrictEqual(' foo') ` ,
80
+ code : `async function x() { expect((await screen.findByRole("textbox")). value).toEqual(" foo") } ` ,
60
81
errors,
61
- output : `expect(element) .toHaveValue(' foo') ` ,
82
+ output : `async function x() { expect((await screen.findByRole("textbox"))) .toHaveValue(" foo") } ` ,
62
83
} ,
63
84
{
64
- code : `expect(element.value).not. toBe(' foo') ` ,
85
+ code : `const element = screen.getByRole("textbox"); expect(element.value).toBe(" foo"); ` ,
65
86
errors,
66
- output : `expect(element).not. toHaveValue(' foo') ` ,
87
+ output : `const element = screen.getByRole("textbox"); expect(element).toHaveValue(" foo"); ` ,
67
88
} ,
68
89
{
69
- code : `expect(element. value).not.toEqual(' foo' )` ,
90
+ code : `expect(screen.getByRole("textbox"). value).not.toEqual(" foo" )` ,
70
91
errors,
71
- output : `expect(element) .not.toHaveValue(' foo' )` ,
92
+ output : `expect(screen.getByRole("textbox")) .not.toHaveValue(" foo" )` ,
72
93
} ,
73
94
{
74
- code : `expect(element. value).not.toStrictEqual(' foo' )` ,
95
+ code : `expect(screen.queryByRole("dropdown"). value).not.toEqual(" foo" )` ,
75
96
errors,
76
- output : `expect(element).not.toHaveValue('foo')` ,
97
+ output : `expect(screen.queryByRole("dropdown")).not.toHaveValue("foo")` ,
98
+ } ,
99
+ {
100
+ code : `async function x() { expect((await screen.getByRole("textbox")).value).not.toEqual("foo") }` ,
101
+ errors,
102
+ output : `async function x() { expect((await screen.getByRole("textbox"))).not.toHaveValue("foo") }` ,
103
+ } ,
104
+ {
105
+ code : `const element = screen.getByRole("textbox"); expect(element.value).not.toBe("foo");` ,
106
+ errors,
107
+ output : `const element = screen.getByRole("textbox"); expect(element).not.toHaveValue("foo");` ,
108
+ } ,
109
+ //==========================================================================
110
+ {
111
+ code : `expect(screen.getByTestId('bananas').value).toEqual('foo')` ,
112
+ errors : [
113
+ {
114
+ ...errors [ 0 ] ,
115
+ suggestions : [
116
+ {
117
+ desc : "Replace toEqual with toHaveValue" ,
118
+ output : `expect(screen.getByTestId('bananas')).toHaveValue('foo')` ,
119
+ } ,
120
+ ] ,
121
+ } ,
122
+ ] ,
123
+ } ,
124
+ {
125
+ code : `expect(screen.queryByTestId('bananas').value).toBe('foo')` ,
126
+ errors : [
127
+ {
128
+ ...errors [ 0 ] ,
129
+ suggestions : [
130
+ {
131
+ desc : "Replace toBe with toHaveValue" ,
132
+ output : `expect(screen.queryByTestId('bananas')).toHaveValue('foo')` ,
133
+ } ,
134
+ ] ,
135
+ } ,
136
+ ] ,
137
+ } ,
138
+ {
139
+ code : `async function x() { expect((await screen.findByTestId("bananas")).value).toStrictEqual("foo") }` ,
140
+ errors : [
141
+ {
142
+ ...errors [ 0 ] ,
143
+ suggestions : [
144
+ {
145
+ desc : "Replace toStrictEqual with toHaveValue" ,
146
+ output : `async function x() { expect((await screen.findByTestId("bananas"))).toHaveValue("foo") }` ,
147
+ } ,
148
+ ] ,
149
+ } ,
150
+ ] ,
151
+ } ,
152
+ {
153
+ code : `let element; element = screen.getByTestId('bananas'); expect(element.value).toEqual('foo');` ,
154
+ errors : [
155
+ {
156
+ ...errors [ 0 ] ,
157
+ suggestions : [
158
+ {
159
+ desc : "Replace toEqual with toHaveValue" ,
160
+ output : `let element; element = screen.getByTestId('bananas'); expect(element).toHaveValue('foo');` ,
161
+ } ,
162
+ ] ,
163
+ } ,
164
+ ] ,
165
+ } ,
166
+ {
167
+ code : `expect(screen.getByTestId('bananas').value).not.toEqual('foo')` ,
168
+ errors : [
169
+ {
170
+ ...errors [ 0 ] ,
171
+ suggestions : [
172
+ {
173
+ desc : "Replace toEqual with toHaveValue" ,
174
+ output : `expect(screen.getByTestId('bananas')).not.toHaveValue('foo')` ,
175
+ } ,
176
+ ] ,
177
+ } ,
178
+ ] ,
179
+ } ,
180
+ {
181
+ code : `expect(screen.queryByTestId('bananas').value).not.toBe('foo')` ,
182
+ errors : [
183
+ {
184
+ ...errors [ 0 ] ,
185
+ suggestions : [
186
+ {
187
+ desc : "Replace toBe with toHaveValue" ,
188
+ output : `expect(screen.queryByTestId('bananas')).not.toHaveValue('foo')` ,
189
+ } ,
190
+ ] ,
191
+ } ,
192
+ ] ,
193
+ } ,
194
+ {
195
+ code : `async function x() { expect((await screen.findByTestId("bananas")).value).not.toStrictEqual("foo") }` ,
196
+ errors : [
197
+ {
198
+ ...errors [ 0 ] ,
199
+ suggestions : [
200
+ {
201
+ desc : "Replace toStrictEqual with toHaveValue" ,
202
+ output : `async function x() { expect((await screen.findByTestId("bananas"))).not.toHaveValue("foo") }` ,
203
+ } ,
204
+ ] ,
205
+ } ,
206
+ ] ,
207
+ } ,
208
+ {
209
+ code : `let element; element = screen.getByTestId('bananas'); expect(element.value).not.toEqual('foo');` ,
210
+ errors : [
211
+ {
212
+ ...errors [ 0 ] ,
213
+ suggestions : [
214
+ {
215
+ desc : "Replace toEqual with toHaveValue" ,
216
+ output : `let element; element = screen.getByTestId('bananas'); expect(element).not.toHaveValue('foo');` ,
217
+ } ,
218
+ ] ,
219
+ } ,
220
+ ] ,
77
221
} ,
78
222
] ,
79
223
} ) ;
0 commit comments