-
Notifications
You must be signed in to change notification settings - Fork 930
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: use flokki for parsing html in ~X tests
Summary: Flokki is pretty well tested so we can use it and remove one dependency if we normalize html attribute ordering for tests. Test Plan: ```sh mix test ```
- Loading branch information
1 parent
24cb4cf
commit 9fd04a7
Showing
3 changed files
with
37 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,51 @@ | ||
defmodule Phoenix.LiveViewTest.HTML do | ||
require EasyHTML | ||
|
||
defmacro sigil_X({:<<>>, _, [binary]}, []) when is_binary(binary) do | ||
Macro.escape(EasyHTML.parse!(binary)) | ||
Macro.escape(parse_sorted!(binary)) | ||
end | ||
|
||
defmacro sigil_x(term, []) do | ||
quote bind_quoted: [term: term] do | ||
EasyHTML.parse!(term) | ||
parse_sorted!(term) | ||
end | ||
end | ||
|
||
def t2h(template) do | ||
template | ||
|> Phoenix.LiveViewTest.rendered_to_string() | ||
|> EasyHTML.parse!() | ||
|> parse_sorted!() | ||
end | ||
|
||
@doc """ | ||
This function will parse a binary into a list of in the format | ||
of floki, however the attributes of any node are in sorted | ||
order. | ||
``` | ||
{"node_name", [{"attribute_name", "attribute_value"}], [content]} | ||
``` | ||
or | ||
``` | ||
"string contents with no html/xml nodes" | ||
``` | ||
While soting the html attributes does mean we can't detect | ||
differences in behavior, it also keeps the order of map | ||
key/value from failing tests. | ||
""" | ||
def parse_sorted!(value) do | ||
value | ||
|> Floki.parse_fragment!() | ||
|> Enum.map(&normalize_attribute_order/1) | ||
end | ||
|
||
defp normalize_attribute_order({node_type, attributes, content}), | ||
do: {node_type, Enum.sort(attributes), Enum.map(content, &normalize_attribute_order/1)} | ||
|
||
defp normalize_attribute_order(values) when is_list(values), | ||
do: Enum.map(values, &normalize_attribute_order/1) | ||
|
||
defp normalize_attribute_order(value), do: value | ||
end |