Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/Test/Html/Internal/ElmHtml/Query.elm
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,25 @@ 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 _, 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

( Nothing, Just className ) ->
className

( Nothing, Nothing ) ->
""
)
|> String.split " "


Expand Down
21 changes: 21 additions & 0 deletions tests/src/Test/Html/QueryTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ 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 nothing if classes are added both using Attr.attribute and Attr.property" <|
\() ->
Html.div
[ Attr.attribute "class" "hello"
, Attr.property "className" (Encode.string "world")
]
[]
|> Query.fromHtml
|> Query.has [ class "hello" ]
|> expectationToIsPassing
|> Expect.equal False
]
]
, describe "Query.contains" <|
Expand Down
Loading