Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findAll with multiple selectors returns unexpected results #227

Open
mrwarszawa opened this issue Nov 28, 2023 · 0 comments
Open

findAll with multiple selectors returns unexpected results #227

mrwarszawa opened this issue Nov 28, 2023 · 0 comments

Comments

@mrwarszawa
Copy link

Based on the documentation I was expecting that selectors passed to findAll would be applied to the same element:

Find the descendant elements which match all the given selectors.

This is not what I'm observing. It seems that selectors are applied one by one also to child nodes.

Example:

<p class="c1"> 
   <a class="c2"/> 
 </p>

with selector findAll [Selector.class "c1", Selector.class "c2"]

I'd expect this not to match anything, but it does match one element.

I hit this issue when writing code to find a div with a certain class. The query looked like findAll [Selector.tag "div", Selector.class "c"] for HTML structure:

<div>
  <div>
    <div class="c">
    </div>
  </div>
</div>

I was very surprised to get multiple finds from findAll .

Code to illustrate the issue:

module ExampleTest exposing(exampleTest1,exampleTest2)

import Html exposing (div,a, p) 
import Html.Attributes as Html
import Test.Html.Query exposing (fromHtml,findAll,count)
import Test.Html.Selector as Selector
import Test exposing (Test,test)
import Expect

exampleTest1 : Test
exampleTest1 = test "ExampleTest1 (expected 0)" <| \_ -> (div [] [ p [Html.class "c1"] [a [Html.class "c2"] []]]) 
                    |> fromHtml
                    |> findAll [Selector.class "c1", Selector.class "c2"]
                    |> count (Expect.equal <| 0)          
                    


exampleTest2 : Test
exampleTest2 = test "ExampleTest2 (expected 1)" <| \_ -> (div [] [ div [] [div [Html.class "c"] []]]) 
                    |> fromHtml
                    |> findAll [Selector.tag "div", Selector.class "c"]
                    |> count (Expect.equal <| 1)          

> 
> Compiling > Starting tests
> 
> elm-test 0.19.1-revision12
> --------------------------
> 
> Running 2 tests. To reproduce these results, run: elm-test --fuzz 100 --seed 370349610011760
> 
> > ExampleTest
> > ExampleTest1 (expected 0)
> 
>     ▼ Query.fromHtml
> 
>         <div>
>             <p class="c1">
>                 <a class="c2">
>                 </a>
>             </p>
>         </div>
> 
> 
>     ▼ Query.findAll [ class "c1", class "c2" ]
> 
>         1)  <a class="c2">
>             </a>
> 
> 
>     ▼ Query.count
> 
>     Expect.equal
> 
> 
> > ExampleTest
> > ExampleTest2 (expected 1)
> 
>     ▼ Query.fromHtml
> 
>         <div>
>             <div>
>                 <div class="c">
>                 </div>
>             </div>
>         </div>
> 
> 
>     ▼ Query.findAll [ tag "div", class "c" ]
> 
>         1)  <div class="c">
>             </div>
> 
>         2)  <div class="c">
>             </div>
> 
> 
>     ▼ Query.count
> 
>     Expect.equal
> 
> 
> 
> TEST RUN FAILED
> 
> Duration: 281 ms
> Passed:   0
> Failed:   2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant