Skip to content

Commit

Permalink
test: use flokki for parsing html in ~X tests
Browse files Browse the repository at this point in the history
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
elliottneilclark committed Sep 22, 2023
1 parent 24cb4cf commit 9fd04a7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ defmodule Phoenix.LiveView.MixProject do
{:telemetry, "~> 0.4.2 or ~> 1.0"},
{:jason, "~> 1.0", optional: true},
{:floki, "~> 0.30.0", only: :test},
{:easyhtml, "~> 0.3.0", only: :test},
{:ex_doc, "~> 0.29", only: :docs},
{:makeup_eex, ">= 0.1.1", only: :docs},
{:html_entities, ">= 0.0.0", only: :test},
Expand Down
1 change: 0 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
%{
"castore": {:hex, :castore, "1.0.3", "7130ba6d24c8424014194676d608cb989f62ef8039efd50ff4b3f33286d06db8", [:mix], [], "hexpm", "680ab01ef5d15b161ed6a95449fac5c6b8f60055677a8e79acf01b27baa4390b"},
"earmark_parser": {:hex, :earmark_parser, "1.4.33", "3c3fd9673bb5dcc9edc28dd90f50c87ce506d1f71b70e3de69aa8154bc695d44", [:mix], [], "hexpm", "2d526833729b59b9fdb85785078697c72ac5e5066350663e5be6a1182da61b8f"},
"easyhtml": {:hex, :easyhtml, "0.3.0", "80e729ab4bfa7572fda74d05d7e8fb6b2e01b9d828a44e75e563d0e1a258eca7", [:mix], [{:floki, "~> 0.30", [hex: :floki, repo: "hexpm", optional: false]}], "hexpm", "9929ef5c0e85812f1d6ce66587597fe81a825ae9653bb851e942ceff45123afe"},
"esbuild": {:hex, :esbuild, "0.7.1", "fa0947e8c3c3c2f86c9bf7e791a0a385007ccd42b86885e8e893bdb6631f5169", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "66661cdf70b1378ee4dc16573fcee67750b59761b2605a0207c267ab9d19f13c"},
"ex_doc": {:hex, :ex_doc, "0.30.4", "e8395c8e3c007321abb30a334f9f7c0858d80949af298302daf77553468c0c39", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "9a19f0c50ffaa02435668f5242f2b2a61d46b541ebf326884505dfd3dd7af5e4"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
Expand Down
42 changes: 37 additions & 5 deletions test/support/html.ex
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

0 comments on commit 9fd04a7

Please sign in to comment.