@@ -145,38 +145,12 @@ func TestSelector_TextValueContains(t *testing.T) {
145
145
}
146
146
}
147
147
148
- func TestSelector_Exists (t * testing.T ) {
149
- tests := map [string ]struct {
150
- selector string
151
- responseBody string
152
- }{
153
- "element exists" : {
154
- selector : `div[data-test-id^="product-"]` ,
155
- responseBody : `<div data-test-id="product-5">first</div>` ,
156
- },
157
- }
158
- for name , test := range tests {
159
- t .Run (name , func (t * testing.T ) {
160
- apitest .New ().
161
- HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
162
- _ , _ = w .Write ([]byte (test .responseBody ))
163
- w .WriteHeader (http .StatusOK )
164
- }).
165
- Get ("/" ).
166
- Expect (t ).
167
- Status (http .StatusOK ).
168
- Assert (selector .Exists (test .selector )).
169
- End ()
170
- })
171
- }
172
- }
173
-
174
148
func TestSelector_Exists_NoMatch (t * testing.T ) {
175
149
verifier := & mockVerifier {
176
150
EqualMock : func (t * testing.T , expected , actual interface {}, msgAndArgs ... interface {}) bool {
177
- expectedError := "did not find expected value for selector '.myClass'"
151
+ expectedError := "expected found='true' for selector '.myClass'"
178
152
if actual .(error ).Error () != expectedError {
179
- t .Fatalf ( "actual was unexpected: %v" , actual )
153
+ t .Fatal ( )
180
154
}
181
155
return true
182
156
},
@@ -194,31 +168,47 @@ func TestSelector_Exists_NoMatch(t *testing.T) {
194
168
End ()
195
169
}
196
170
197
- func TestSelector_MultipleExists (t * testing.T ) {
171
+ func TestSelector_Exists (t * testing.T ) {
198
172
tests := map [string ]struct {
173
+ exists bool
199
174
selector []string
200
175
responseBody string
201
176
}{
202
- "element exists" : {
177
+ "exists" : {
178
+ exists : true ,
179
+ selector : []string {`div[data-test-id^="product-"]` },
180
+ },
181
+ "multiple exists" : {
182
+ exists : true ,
203
183
selector : []string {`div[data-test-id^="product-"]` , `.otherClass` },
204
- responseBody : `<div>
205
- <div class="myClass">first</div>
206
- <div class="otherClass">something second</div>
207
- <div data-test-id="product-5">first</div>
208
- </div>` ,
184
+ },
185
+ "not exists" : {
186
+ exists : false ,
187
+ selector : []string {`div[data-test-id^="product-4"]` },
188
+ },
189
+ "multiple not exists" : {
190
+ exists : false ,
191
+ selector : []string {`div[data-test-id^="product-4"]` , `.notExistClass` },
209
192
},
210
193
}
211
194
for name , test := range tests {
212
195
t .Run (name , func (t * testing.T ) {
196
+ sel := selector .NotExists (test .selector ... )
197
+ if test .exists {
198
+ sel = selector .Exists (test .selector ... )
199
+ }
213
200
apitest .New ().
214
201
HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
215
- _ , _ = w .Write ([]byte (test .responseBody ))
202
+ _ , _ = w .Write ([]byte (`<div>
203
+ <div class="myClass">first</div>
204
+ <div class="otherClass">something second</div>
205
+ <div data-test-id="product-5">first</div>
206
+ </div>` ))
216
207
w .WriteHeader (http .StatusOK )
217
208
}).
218
209
Get ("/" ).
219
210
Expect (t ).
220
- Status (http .StatusOK ).
221
- Assert (selector .Exists (test .selector ... )).
211
+ Assert (sel ).
222
212
End ()
223
213
})
224
214
}
@@ -315,7 +305,7 @@ func TestSelector_Selection_NotMatch(t *testing.T) {
315
305
func TestSelector_MultipleExists_NoMatch (t * testing.T ) {
316
306
verifier := & mockVerifier {
317
307
EqualMock : func (t * testing.T , expected , actual interface {}, msgAndArgs ... interface {}) bool {
318
- expectedError := "did not find expected value for selector '.myClass'"
308
+ expectedError := "expected found='true' for selector '.myClass'"
319
309
if actual .(error ).Error () != expectedError {
320
310
t .Fatalf ("actual was unexpected: %v" , actual )
321
311
}
0 commit comments