From 9ebb2324f2afbe06c08cac1b248530efa5b4fcdc Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 12 Jul 2025 09:38:56 +0200 Subject: [PATCH 1/2] Support classes set using the class attribute --- src/Test/Html/Internal/ElmHtml/Query.elm | 15 +++++++++++++-- tests/src/Test/Html/QueryTests.elm | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Test/Html/Internal/ElmHtml/Query.elm b/src/Test/Html/Internal/ElmHtml/Query.elm index 64b1c36c..95699e60 100644 --- a/src/Test/Html/Internal/ElmHtml/Query.elm +++ b/src/Test/Html/Internal/ElmHtml/Query.elm @@ -262,8 +262,19 @@ hasStyle style facts = classnames : Facts msg -> List String classnames facts = - Dict.get "className" facts.stringAttributes - |> Maybe.withDefault "" + (case ( Dict.get "class" facts.stringAttributes, Dict.get "className" facts.stringAttributes ) of + ( Just class, Just className ) -> + class ++ " " ++ className + + ( Just class, Nothing ) -> + class + + ( Nothing, Just className ) -> + className + + ( Nothing, Nothing ) -> + "" + ) |> String.split " " diff --git a/tests/src/Test/Html/QueryTests.elm b/tests/src/Test/Html/QueryTests.elm index 134ff5c2..3f8141dd 100644 --- a/tests/src/Test/Html/QueryTests.elm +++ b/tests/src/Test/Html/QueryTests.elm @@ -113,6 +113,25 @@ all = [ Query.has [ attribute (Attr.property "className" (Encode.string "hello world")) ] , Query.has [ attribute (Attr.property "className" (Encode.string "world hello")) ] ] + , test "matches a class added using Attr.attribute" <| + \() -> + divWithAttribute (Attr.attribute "class" "hello") + |> Query.fromHtml + |> Query.has [ class "hello" ] + , test "matches a class added using Attr.property" <| + \() -> + divWithAttribute (Attr.property "className" (Encode.string "hello")) + |> Query.fromHtml + |> Query.has [ class "hello" ] + , test "matches one class added using Attr.attribute and one added using Attr.property" <| + \() -> + Html.div + [ Attr.attribute "class" "hello" + , Attr.property "className" (Encode.string "world") + ] + [] + |> Query.fromHtml + |> Query.has [ class "hello", class "world" ] ] ] , describe "Query.contains" <| From 0238246dd68a34970b1df4cb461c5c94c62eba25 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 12 Jul 2025 19:25:06 +0200 Subject: [PATCH 2/2] Match nothing if both class and className are used --- src/Test/Html/Internal/ElmHtml/Query.elm | 10 ++++++++-- tests/src/Test/Html/QueryTests.elm | 6 ++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Test/Html/Internal/ElmHtml/Query.elm b/src/Test/Html/Internal/ElmHtml/Query.elm index 95699e60..8482e5ec 100644 --- a/src/Test/Html/Internal/ElmHtml/Query.elm +++ b/src/Test/Html/Internal/ElmHtml/Query.elm @@ -263,8 +263,14 @@ hasStyle style facts = classnames : Facts msg -> List String classnames facts = (case ( Dict.get "class" facts.stringAttributes, Dict.get "className" facts.stringAttributes ) of - ( Just class, Just className ) -> - class ++ " " ++ className + ( Just _, Just _ ) -> + -- If you use both the `class` attribute and the `className` property at the same time, + -- it’s undefined which classes you end up with. It depends on which order they are specified, + -- which order elm/virtual-dom happens to apply them, and which of them changed most recently. + -- Mixing both is not a good idea. Ideally, we’d show some nice error message explaining this + -- here, but since this is very much an edge case it does not feel worth the complexity. + -- Instead, silently claim that there are no classes (that no classes match the node). + "" ( Just class, Nothing ) -> class diff --git a/tests/src/Test/Html/QueryTests.elm b/tests/src/Test/Html/QueryTests.elm index 3f8141dd..198d7b01 100644 --- a/tests/src/Test/Html/QueryTests.elm +++ b/tests/src/Test/Html/QueryTests.elm @@ -123,7 +123,7 @@ all = divWithAttribute (Attr.property "className" (Encode.string "hello")) |> Query.fromHtml |> Query.has [ class "hello" ] - , test "matches one class added using Attr.attribute and one added using Attr.property" <| + , test "matches nothing if classes are added both using Attr.attribute and Attr.property" <| \() -> Html.div [ Attr.attribute "class" "hello" @@ -131,7 +131,9 @@ all = ] [] |> Query.fromHtml - |> Query.has [ class "hello", class "world" ] + |> Query.has [ class "hello" ] + |> expectationToIsPassing + |> Expect.equal False ] ] , describe "Query.contains" <|