Skip to content

Commit

Permalink
Align LiveViewTest with JS DOM patching for phx-update="ignore" (#3125)
Browse files Browse the repository at this point in the history
* Add failing test

* Align LiveViewTest.DOM with JS implementation for phx-update="ignore"
  • Loading branch information
ftes committed Feb 19, 2024
1 parent 76f2c9e commit 062a2ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/phoenix_live_view/test/dom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,18 @@ defmodule Phoenix.LiveViewTest.DOM do
{tag, attrs, new_children}
end

defp apply_phx_update("ignore", _state, node, _streams) do
verify_phx_update_id!("ignore", attribute(node, "id"), node)
node
defp apply_phx_update("ignore", html_tree, node, _streams) do
container_id = attribute(node, "id")
verify_phx_update_id!("ignore", container_id, node)

{tag, attrs_before, children_before} = by_id(html_tree, container_id)
{_tag, attrs, _children} = node

attrs =
Enum.reject(attrs_before, fn {name, _} -> String.starts_with?(name, "data-") end) ++
Enum.filter(attrs, fn {name, _} -> String.starts_with?(name, "data-") end)

{tag, attrs, children_before}
end

defp apply_phx_update(type, _state, node, _streams) when type in [nil, "replace"] do
Expand Down
28 changes: 28 additions & 0 deletions test/phoenix_live_view/test/dom_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,34 @@ defmodule Phoenix.LiveViewTest.DOMTest do
assert new_html =~ ~S(<div id="2">b</div>)
assert new_html =~ ~S(<div id="3">a</div>)
end

test "patches only container data attributes when phx-update=ignore" do
html = """
<div data-phx-session="SESSIONMAIN" data-phx-main="true" id="phx-458">
<div id="div" remove data-remove update="a" data-update="a" phx-update="ignore">
<div id="1">a</div>
</div>
</div>
"""

inner_html = """
<div id="div" update="b" data-update="b" add data-add phx-update="ignore">
<div id="1" class="foo">b</div>
</div>
"""

{new_html, _removed_cids} = DOM.patch_id("phx-458", DOM.parse(html), DOM.parse(inner_html), [])

new_html = DOM.to_html(new_html)

assert new_html =~ ~S( remove)
refute new_html =~ ~S( data-remove)
assert new_html =~ ~S( update="a")
assert new_html =~ ~S( data-update="b")
refute new_html =~ ~S( add)
assert new_html =~ ~S( data-add)
assert new_html =~ ~S(<div id="1">a</div>)
end
end

describe "merge_diff" do
Expand Down

0 comments on commit 062a2ce

Please sign in to comment.