elm-explorations / test

Write unit and fuzz tests for Elm code.
https://package.elm-lang.org/packages/elm-explorations/test/latest
BSD 3-Clause "New" or "Revised" License
236 stars 40 forks source link

findAll with multiple selectors returns unexpected results #227

Open mrwarszawa opened 9 months ago

mrwarszawa commented 9 months ago

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