Query.has matches children, not just root element
- Dominant language
- Elm
- Stars
- 68
- Forks
- 15
- PR merge metrics
- No merged PRs in 30d
Description
Query.has is documented as follows:
> Expect the element to match all of the given selectors.
> http://package.elm-lang.org/packages/eeue56/elm-html-test/5.1.0/Test-Html-Query#has
Here's the example given:
```
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" ] ]
```
https://github.com/eeue56/elm-html-test/blob/5.1.0/src/Test/Html/Query.elm#L394-L405
Based on this example, it sounds like it's making assertions against the node itself, but in practice, it looks like it traverses all of the node's children. Is this expected behavior? If so, perhaps the docs could be clearer.
Here's an example test that I'm not sure should fail or pass (it does pass currently):
```
test "Query.has doesn't consider children" <|
\() ->
div []
[ div []
[ span [ class "nested-span" ]
[ text "hello" ]
]
]
|> Query.fromHtml
|> Query.has [ Selector.class "nested-span" ]
```
@stoeffel and I ran up against this earlier, as this behavior causes the test used as documentation for `Query.children` to be a false positive. Children was returning the `ul`, and the test was still passing because there were `li` elements within the `ul`:
```
test "The
- only has
- children" <|
\() ->
div []
[ ul [ class "items active" ]
[ li [] [ text "first item" ]
, li [] [ text "second item" ]
, li [] [ text "third item" ]
]
]
|> Query.fromHtml
|> Query.find [ tag "ul" ]
|> Query.children []
|> Query.each (Query.has [ tag "li" ])
```
http://package.elm-lang.org/packages/eeue56/elm-html-test/5.1.0/Test-Html-Query#childrenAlso, it's not clear whether it enforces that a single element must match all the selectors, as this version of the example also passes:
```
test "The list has both the classes 'items' and 'active'" <|
\() ->
div []
[ ul []
[ li [] [ text "first item" ]
, li [] [ text "second item" ]
, li [ class "items active" ] [ text "third item" ]
]
]
|> Query.fromHtml
|> Query.find [ tag "ul" ]
|> Query.has [ tag "ul", classes [ "items", "active" ] ]
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with src/Test/Html/Query.elm around lines 394-405 and compare the documented Query.has example with the Query.children test shown in the issue. Verify whether matching is applied to the root element or descendants, and whether all selectors must match one element. Done means the behavior and documentation agree, with tests covering both cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- elm
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100