forked from elm-explorations/test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.elm
More file actions
532 lines (410 loc) · 16.6 KB
/
Query.elm
File metadata and controls
532 lines (410 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
module Test.Html.Query exposing
( Single, Multiple, fromHtml
, find, findAll, children, first, index, keep
, count, contains, has, hasNot, each
, prettyPrintSingle
)
{-| Querying HTML structure.
@docs Single, Multiple, fromHtml
## Querying
@docs find, findAll, children, first, index, keep
## Expecting
@docs count, contains, has, hasNot, each
## Debugging
@docs prettyPrintSingle
-}
import Expect exposing (Expectation)
import Html exposing (Html)
import Json.Decode
import Test.Html.Internal.ElmHtml.InternalTypes as InternalTypes
import Test.Html.Internal.Inert as Inert
import Test.Html.Query.Internal as Internal exposing (failWithQuery)
import Test.Html.Selector exposing (Selector)
import Test.Html.Selector.Internal exposing (selectorToString)
{- DESIGN NOTES:
The reason for having `Query.index` and `Query.first` instead of doing them as
selectors (which would let you do e.g. `Query.find [ first ]` to get the
first child, instead of `Query.children [] |> Query.first` like you have to
do now) is that it's not immediately obvious what a query like this would do:
Query.findAll [ first, tag "li" ]
Is that getting the first descendant, and then checking whether it's an <li>?
Or is it finding the first <li> descendant? (Yes.) Also this is a findAll
but it's only ever returning a single result despite being typed as a Multiple.
Arguably `id` could be treated the same way - since you *should* only have
one id, *should* only ever return one result. However, in that case, it's
possible that you have multiple IDs - and in that case you actually want the
test to fail so you find out about the mistake!
-}
{-| A query that expects to find exactly one element.
Contrast with [`Multiple`](#Multiple).
-}
type alias Single msg =
Internal.Single msg
{-| A query that may find any number of elements, including zero.
Contrast with [`Single`](#Single).
-}
type alias Multiple msg =
Internal.Multiple msg
{-| Translate a `Html` value into a `Single` query. This is how queries
typically begin.
import Html
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (text)
test "Button has the expected text" <|
\() ->
Html.button [] [ Html.text "I'm a button!" ]
|> Query.fromHtml
|> Query.has [ text "I'm a button!" ]
-}
fromHtml : Html msg -> Single msg
fromHtml html =
Internal.Single True <|
case Inert.fromHtml html of
Ok node ->
Internal.Query node []
Err (Inert.DecodeError decodeError) ->
Internal.InternalError (Json.Decode.errorToString decodeError)
Err (Inert.ValidationErrors validations) ->
Internal.ValidationErrors validations
-- TRAVERSAL --
{-| Find the descendant elements which match all the given selectors.
import Html exposing (div, ul, li)
import Html.Attributes exposing (class)
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (tag)
import Expect
test "The list has three items" <|
\() ->
div []
[ ul [ class "items active" ]
[ li [] [ text "first item" ]
, li [] [ text "second item" ]
, li [] [ text "third item" ]
]
]
|> Query.fromHtml
|> Query.findAll [ tag "li" ]
|> Query.count (Expect.equal 3)
-}
findAll : List Selector -> Single msg -> Multiple msg
findAll selectors (Internal.Single showTrace query) =
Internal.FindAll selectors
|> Internal.prependSelector query
|> Internal.Multiple showTrace
{-| Find the descendant elements of the result of `findAll` which match all the given selectors.
import Html exposing (div, ul, li)
import Html.Attributes exposing (class)
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (tag)
import Expect
test "The list has three items" <|
\() ->
div []
[ ul [ class "items active" ]
[ li [] [ a [] [ text "first item" ]]
, li [] [ a [] [ text "second item" ]]
, li [] [ a [] [ text "third item" ]]
, li [] [ button [] [ text "button" ]]
]
]
|> Query.fromHtml
|> Query.findAll [ tag "li" ]
|> Query.keep ( tag "a" )
|> Expect.all
[ Query.each (Query.has [ tag "a" ])
, Query.first >> Query.has [ text "first item" ]
]
-}
keep : Selector -> Multiple msg -> Multiple msg
keep selector (Internal.Multiple showTrace query) =
Internal.FindAll [ selector ]
|> Internal.prependSelector query
|> Internal.Multiple showTrace
{-| Return the matched element's immediate child elements.
import Html exposing (div, ul, li)
import Html.Attributes exposing (class)
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (tag, classes)
test "The <ul> only has <li> children" <|
\() ->
div []
[ ul [ class "items active" ]
[ li [ class "item"] [ text "first item" ]
, li [ class "item selected"] [ text "second item" ]
, li [ class "item"] [ text "third item" ]
]
]
|> Query.fromHtml
|> Query.find [ class "items" ]
|> Query.children [ class "selected" ]
|> Query.count (Expect.equal 1)
-}
children : List Selector -> Single msg -> Multiple msg
children selectors (Internal.Single showTrace query) =
Internal.Children selectors
|> Internal.prependSelector query
|> Internal.Multiple showTrace
{-| Find exactly one descendant element which matches all the given selectors.
If no descendants match, or if more than one matches, the test will fail.
import Html exposing (div, ul, li)
import Html.Attributes exposing (class)
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (tag, classes)
test "The list has both the classes 'items' and 'active'" <|
\() ->
div []
[ ul [ class "items active" ]
[ li [] [ text "first item" ]
, li [] [ text "second item" ]
, li [] [ text "third item" ]
]
]
|> Query.fromHtml
|> Query.find [ tag "ul" ]
|> Query.has [ classes [ "items", "active" ] ]
-}
find : List Selector -> Single msg -> Single msg
find selectors (Internal.Single showTrace query) =
Internal.Find selectors
|> Internal.prependSelector query
|> Internal.Single showTrace
{-| Return the first element in a match. If there were no matches, the test
will fail.
`Query.first` is a shorthand for `Query.index 0` - they do the same thing.
import Html exposing (div, ul, li)
import Html.Attributes exposing (class)
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (tag, classes)
test "The first <li> is called 'first item'" <|
\() ->
div []
[ ul [ class "items active" ]
[ li [] [ text "first item" ]
, li [] [ text "second item" ]
, li [] [ text "third item" ]
]
]
|> Query.fromHtml
|> Query.findAll [ tag "li" ]
|> Query.first
|> Query.has [ text "first item" ]
-}
first : Multiple msg -> Single msg
first (Internal.Multiple showTrace query) =
Internal.First
|> Internal.prependSelector query
|> Internal.Single showTrace
{-| Return the element in a match at the given index. For example,
`Query.index 0` would match the first element, and `Query.index 1` would match
the second element.
You can pass negative numbers to get elements from the end - for example, `Query.index -1`
will match the last element, and `Query.index -2` will match the second-to-last.
If the index falls outside the bounds of the match, the test will fail.
import Html exposing (div, ul, li)
import Html.Attributes exposing (class)
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (tag, classes)
test "The second <li> is called 'second item'" <|
\() ->
div []
[ ul [ class "items active" ]
[ li [] [ text "first item" ]
, li [] [ text "second item" ]
, li [] [ text "third item" ]
]
]
|> Query.fromHtml
|> Query.findAll [ tag "li" ]
|> Query.index 1
|> Query.has [ text "second item" ]
-}
index : Int -> Multiple msg -> Single msg
index position (Internal.Multiple showTrace query) =
Internal.Index position
|> Internal.prependSelector query
|> Internal.Single showTrace
-- EXPECTATIONS --
{-| Expect the number of elements matching the query fits the given expectation.
import Html exposing (div, ul, li)
import Html.Attributes exposing (class)
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (tag)
import Expect
test "The list has three items" <|
\() ->
div []
[ ul [ class "items active" ]
[ li [] [ text "first item" ]
, li [] [ text "second item" ]
, li [] [ text "third item" ]
]
]
|> Query.fromHtml
|> Query.findAll [ tag "li" ]
|> Query.count (Expect.equal 3)
-}
count : (Int -> Expectation) -> Multiple msg -> Expectation
count expect ((Internal.Multiple showTrace query) as multiple) =
(List.length >> expect >> failWithQuery showTrace "Query.count" query)
|> Internal.multipleToExpectation multiple
{-| Expect the element to have at least one descendant matching each node in the list.
import Html exposing (div, ul, li)
import Html.Attributes exposing (class)
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (tag, classes)
test "The list has two li: one with the text \"third item\" and \
another one with \"first item\"" <|
\() ->
div []
[ ul [ class "items active" ]
[ li [] [ text "first item" ]
, li [] [ text "second item" ]
, li [] [ text "third item" ]
]
]
|> Query.fromHtml
|> Query.contains
[ li [] [ text "third item" ]
, li [] [ text "first item" ]
]
-}
contains : List (Html msg) -> Single msg -> Expectation
contains expectedHtml (Internal.Single showTrace query) =
case
List.map Inert.fromHtml expectedHtml
|> collectResults
of
Ok expectedElmHtml ->
Internal.contains
(List.map Inert.toElmHtml expectedElmHtml)
query
|> failWithQuery showTrace "Query.contains" query
Err errors ->
errors
|> List.map
(\error ->
(case error of
Inert.DecodeError decodeError ->
[ "Internal Error: failed to decode the virtual dom. Please report this at <https://github.com/elm-explorations/test/issues>."
, Json.Decode.errorToString decodeError
]
Inert.ValidationErrors { deduped } ->
deduped
|> List.map InternalTypes.validationMessage
)
|> String.join "\n"
)
|> String.join "\n\n"
|> Expect.fail
collectResults : List (Result x a) -> Result (List x) (List a)
collectResults listOfResults =
let
step : Result (List x) (List a) -> List (Result x a) -> Result (List x) (List a)
step acc list =
case ( acc, list ) of
( Err errors, [] ) ->
Err (List.reverse errors)
( Ok values, [] ) ->
Ok (List.reverse values)
( Err errors, (Err x) :: rest ) ->
step (Err (x :: errors)) rest
( Ok _, (Err x) :: rest ) ->
step (Err [ x ]) rest
( Err errors, (Ok _) :: rest ) ->
step (Err errors) rest
( Ok values, (Ok a) :: rest ) ->
step (Ok (a :: values)) rest
in
step (Ok []) listOfResults
{-| Expect the element to match all of the given selectors.
import Html exposing (div, ul, li)
import Html.Attributes exposing (class)
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (tag, classes)
test "The list has both the classes 'items' and 'active'" <|
\() ->
div []
[ ul [ class "items active" ]
[ li [] [ text "first item" ]
, li [] [ text "second item" ]
, li [] [ text "third item" ]
]
]
|> Query.fromHtml
|> Query.find [ tag "ul" ]
|> Query.has [ tag "ul", classes [ "items", "active" ] ]
-}
has : List Selector -> Single msg -> Expectation
has selectors (Internal.Single showTrace query) =
Internal.has selectors query
|> failWithQuery showTrace ("Query.has " ++ Internal.joinAsList selectorToString selectors) query
{-| Expect the element to **not** match all of the given selectors.
import Html exposing (div)
import Html.Attributes as Attributes
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (tag, class)
test "The div element has no progress-bar class" <|
\() ->
div [ Attributes.class "button" ] []
|> Query.fromHtml
|> Query.find [ tag "div" ]
|> Query.hasNot [ tag "div", class "progress-bar" ]
-}
hasNot : List Selector -> Single msg -> Expectation
hasNot selectors (Internal.Single showTrace query) =
let
queryName =
"Query.hasNot " ++ Internal.joinAsList selectorToString selectors
in
Internal.hasNot selectors query
|> failWithQuery showTrace queryName query
{-| Expect that a [`Single`](#Single) expectation will hold true for each of the
[`Multiple`](#Multiple) matched elements.
import Html exposing (div, ul, li)
import Html.Attributes exposing (class)
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (tag, classes)
test "The list has both the classes 'items' and 'active'" <|
\() ->
div []
[ ul [ class "items active" ]
[ li [] [ text "first item" ]
, li [] [ text "second item" ]
, li [] [ text "third item" ]
]
]
|> Query.fromHtml
|> Query.findAll [ tag "ul" ]
|> Query.each
(Expect.all
[ Query.has [ tag "ul" ]
, Query.has [ classes [ "items", "active" ] ]
]
)
-}
each : (Single msg -> Expectation) -> Multiple msg -> Expectation
each check (Internal.Multiple showTrace query) =
Internal.expectAll check query
|> failWithQuery showTrace "Query.each" query
{-| Pretty prints the result of a query as HTML if successful -}
prettyPrintSingle : Single msg -> Result String String
prettyPrintSingle (Internal.Single _ query) =
case Internal.traverse query of
Ok [ element ] ->
Ok <| Internal.prettyPrint element
Ok results ->
Err <| "Query.prettyPrintSingle expected exactly one result from query, but found " ++ String.fromInt (List.length results)
Err queryError ->
Err <| "Query.prettyPrintSingle " ++ Internal.queryErrorToString queryError