@@ -4,22 +4,31 @@ export default ({ preferred, negatedPreferred, attribute }) => {
4
4
const doubleNegativeCases = negatedPreferred . startsWith ( "toBe" )
5
5
? [
6
6
{
7
- code : `expect(element ).not.${ negatedPreferred } ` ,
7
+ code : `const el = screen.getByText("foo"); expect(el ).not.${ negatedPreferred } ` ,
8
8
errors : [
9
9
{
10
10
message : `Use ${ preferred } instead of not.${ negatedPreferred } ` ,
11
11
} ,
12
12
] ,
13
- output : `expect(element ).${ preferred } ` ,
13
+ output : `const el = screen.getByText("foo"); expect(el ).${ preferred } ` ,
14
14
} ,
15
15
{
16
- code : `expect(element).not.${ preferred } ` ,
16
+ code : `expect(getByText("foo")).not.${ negatedPreferred } ` ,
17
+ errors : [
18
+ {
19
+ message : `Use ${ preferred } instead of not.${ negatedPreferred } ` ,
20
+ } ,
21
+ ] ,
22
+ output : `expect(getByText("foo")).${ preferred } ` ,
23
+ } ,
24
+ {
25
+ code : `const el = screen.getByText("foo"); expect(el).not.${ preferred } ` ,
17
26
errors : [
18
27
{
19
28
message : `Use ${ negatedPreferred } instead of not.${ preferred } ` ,
20
29
} ,
21
30
] ,
22
- output : `expect(element ).${ negatedPreferred } ` ,
31
+ output : `const el = screen.getByText("foo"); expect(el ).${ negatedPreferred } ` ,
23
32
} ,
24
33
]
25
34
: [ ] ;
@@ -57,85 +66,88 @@ export default ({ preferred, negatedPreferred, attribute }) => {
57
66
58
67
return {
59
68
valid : [
60
- "expect(element).not.toHaveProperty('value', 'foo')" ,
61
- `expect(element).${ preferred } ` ,
62
- `expect(element).${ negatedPreferred } ` ,
63
- "expect(element).toHaveProperty('value', 'bar')" ,
69
+ `const el = screen.getByText("foo"); expect(el).not.toHaveProperty('value', 'foo')` ,
70
+ `const el = screen.getByText("foo"); expect(el).${ preferred } ` ,
71
+ `const el = screen.getByText("foo"); expect(el).${ negatedPreferred } ` ,
72
+ `const el = screen.getByText("foo"); expect(el).toHaveProperty('value', 'bar')` ,
73
+ `const el = foo.bar(); expect(el).toHaveProperty("${ attribute } ", true)` ,
74
+ `expect(getFoo().${ attribute } ).toBe("bar")` ,
75
+ `expect(getFoo().${ attribute } ).not.toBe("bar")` ,
64
76
] ,
65
77
invalid : [
66
78
...doubleNegativeCases ,
67
79
...directChecks ,
68
80
{
69
- code : `expect(element ).toHaveProperty('${ attribute } ', true)` ,
81
+ code : `const el = screen.getByText("foo"); expect(el ).toHaveProperty('${ attribute } ', true)` ,
70
82
errors : [
71
83
{
72
84
message : `Use ${ preferred } instead of toHaveProperty('${ attribute } ', true)` ,
73
85
} ,
74
86
] ,
75
- output : `expect(element ).${ preferred } ` ,
87
+ output : `const el = screen.getByText("foo"); expect(el ).${ preferred } ` ,
76
88
} ,
77
89
{
78
- code : `expect(element ).toHaveProperty('${ attribute } ', false)` ,
90
+ code : `const el = screen.getByText("foo"); expect(el ).toHaveProperty('${ attribute } ', false)` ,
79
91
errors : [
80
92
{
81
93
message : `Use ${ negatedPreferred } instead of toHaveProperty('${ attribute } ', false)` ,
82
94
} ,
83
95
] ,
84
- output : `expect(element ).${ negatedPreferred } ` ,
96
+ output : `const el = screen.getByText("foo"); expect(el ).${ negatedPreferred } ` ,
85
97
} ,
86
98
{
87
- code : `expect(element ).toHaveAttribute('${ attribute } ', false)` ,
99
+ code : `const el = screen.getByText("foo"); expect(el ).toHaveAttribute('${ attribute } ', false)` ,
88
100
errors : [
89
101
{
90
102
message : `Use ${ negatedPreferred } instead of toHaveAttribute('${ attribute } ', false)` ,
91
103
} ,
92
104
] ,
93
- output : `expect(element ).${ negatedPreferred } ` ,
105
+ output : `const el = screen.getByText("foo"); expect(el ).${ negatedPreferred } ` ,
94
106
} ,
95
107
{
96
- code : `expect(element ).toHaveProperty('${ attribute } ')` ,
108
+ code : `const el = screen.getByText("foo"); expect(el ).toHaveProperty('${ attribute } ')` ,
97
109
errors : [
98
110
{
99
111
message : `Use ${ preferred } instead of toHaveProperty('${ attribute } ')` ,
100
112
} ,
101
113
] ,
102
- output : `expect(element ).${ preferred } ` ,
114
+ output : `const el = screen.getByText("foo"); expect(el ).${ preferred } ` ,
103
115
} ,
104
116
{
105
- code : `expect(element ).toHaveAttribute('${ attribute } ')` ,
117
+ code : `const el = screen.getByText("foo"); expect(el ).toHaveAttribute('${ attribute } ')` ,
106
118
errors : [
107
119
{
108
120
message : `Use ${ preferred } instead of toHaveAttribute('${ attribute } ')` ,
109
121
} ,
110
122
] ,
111
- output : `expect(element ).${ preferred } ` ,
123
+ output : `const el = screen.getByText("foo"); expect(el ).${ preferred } ` ,
112
124
} ,
113
125
{
114
- code : `expect(element ).not.toHaveAttribute('${ attribute } ')` ,
126
+ code : `const el = screen.getByText("foo"); expect(el ).not.toHaveAttribute('${ attribute } ')` ,
115
127
errors : [
116
128
{
117
129
message : `Use ${ negatedPreferred } instead of not.toHaveAttribute('${ attribute } ')` ,
118
130
} ,
119
131
] ,
120
- output : `expect(element ).${ negatedPreferred } ` ,
132
+ output : `const el = screen.getByText("foo"); expect(el ).${ negatedPreferred } ` ,
121
133
} ,
122
134
{
123
- code : `expect(element ).not.toHaveProperty('${ attribute } ')` ,
135
+ code : `const el = screen.getByText("foo"); expect(el ).not.toHaveProperty('${ attribute } ')` ,
124
136
errors : [
125
137
{
126
138
message : `Use ${ negatedPreferred } instead of not.toHaveProperty('${ attribute } ')` ,
127
139
} ,
128
140
] ,
129
- output : `expect(element ).${ negatedPreferred } ` ,
141
+ output : `const el = screen.getByText("foo"); expect(el ).${ negatedPreferred } ` ,
130
142
} ,
131
143
{
132
- code : `expect(element ).toHaveAttribute("${ attribute } ", "")` ,
144
+ code : `const el = screen.getByText("foo"); expect(el ).toHaveAttribute("${ attribute } ", "")` ,
133
145
errors : [
134
146
{
135
147
message : `Use ${ preferred } instead of toHaveAttribute("${ attribute } ", "")` ,
136
148
} ,
137
149
] ,
138
- output : `expect(element ).${ preferred } ` ,
150
+ output : `const el = screen.getByText("foo"); expect(el ).${ preferred } ` ,
139
151
} ,
140
152
{
141
153
code : `expect(getByText("foo")).toHaveAttribute("${ attribute } ", "true")` ,
@@ -174,7 +186,7 @@ export default ({ preferred, negatedPreferred, attribute }) => {
174
186
output : `expect(getByText("foo")).${ negatedPreferred } ` ,
175
187
} ,
176
188
{
177
- code : `expect(element ).toHaveProperty('${ attribute } ', foo)` ,
189
+ code : `const el = screen.getByText("foo"); expect(el ).toHaveProperty('${ attribute } ', foo)` ,
178
190
errors : [
179
191
{
180
192
message : `Use ${ preferred } instead of toHaveProperty('${ attribute } ', foo)` ,
0 commit comments