Skip to content

Commit 3149acd

Browse files
author
Stein Fletcher
committed
Add NotExists for selector
1 parent aff111d commit 3149acd

File tree

3 files changed

+40
-42
lines changed

3 files changed

+40
-42
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ apitest.New().
2121

2222
see also `selector.NthTextValue` and `selector.ContainsTextValue`
2323

24-
### `selector.Exists`
24+
### `selector.Exists` `selector.NotExists`
2525

2626
```go
2727
apitest.New().

selector.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ func Selection(selection string, selectionFunc func(*goquery.Selection) error) f
5454
}
5555

5656
func Exists(selections ...string) func(*http.Response, *http.Request) error {
57+
return expectExists(true, selections...)
58+
}
59+
60+
func NotExists(selections ...string) func(*http.Response, *http.Request) error {
61+
return expectExists(false, selections...)
62+
}
63+
64+
func expectExists(exists bool, selections ...string) func(*http.Response, *http.Request) error {
5765
return func(response *http.Response, request *http.Request) error {
5866
bodyBytes, err := ioutil.ReadAll(response.Body)
5967
if err != nil {
@@ -71,8 +79,8 @@ func Exists(selections ...string) func(*http.Response, *http.Request) error {
7179
found = true
7280
})
7381

74-
if !found {
75-
return fmt.Errorf("did not find expected value for selector '%s'", selection)
82+
if found != exists {
83+
return fmt.Errorf("expected found='%v' for selector '%s'", exists, selection)
7684
}
7785
}
7886

selector_test.go

+29-39
Original file line numberDiff line numberDiff line change
@@ -145,38 +145,12 @@ func TestSelector_TextValueContains(t *testing.T) {
145145
}
146146
}
147147

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-
174148
func TestSelector_Exists_NoMatch(t *testing.T) {
175149
verifier := &mockVerifier{
176150
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'"
178152
if actual.(error).Error() != expectedError {
179-
t.Fatalf("actual was unexpected: %v", actual)
153+
t.Fatal()
180154
}
181155
return true
182156
},
@@ -194,31 +168,47 @@ func TestSelector_Exists_NoMatch(t *testing.T) {
194168
End()
195169
}
196170

197-
func TestSelector_MultipleExists(t *testing.T) {
171+
func TestSelector_Exists(t *testing.T) {
198172
tests := map[string]struct {
173+
exists bool
199174
selector []string
200175
responseBody string
201176
}{
202-
"element exists": {
177+
"exists": {
178+
exists: true,
179+
selector: []string{`div[data-test-id^="product-"]`},
180+
},
181+
"multiple exists": {
182+
exists: true,
203183
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`},
209192
},
210193
}
211194
for name, test := range tests {
212195
t.Run(name, func(t *testing.T) {
196+
sel := selector.NotExists(test.selector...)
197+
if test.exists {
198+
sel = selector.Exists(test.selector...)
199+
}
213200
apitest.New().
214201
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>`))
216207
w.WriteHeader(http.StatusOK)
217208
}).
218209
Get("/").
219210
Expect(t).
220-
Status(http.StatusOK).
221-
Assert(selector.Exists(test.selector...)).
211+
Assert(sel).
222212
End()
223213
})
224214
}
@@ -315,7 +305,7 @@ func TestSelector_Selection_NotMatch(t *testing.T) {
315305
func TestSelector_MultipleExists_NoMatch(t *testing.T) {
316306
verifier := &mockVerifier{
317307
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'"
319309
if actual.(error).Error() != expectedError {
320310
t.Fatalf("actual was unexpected: %v", actual)
321311
}

0 commit comments

Comments
 (0)