Skip to content

Commit

Permalink
Fix issue with new elements in stream DOM test (#2677)
Browse files Browse the repository at this point in the history
This resolves an issue for testing streams. New items didn't get assigned the data-phx-stream and thus subsequent stream resets didn't reset the list as the previously added items couldn't be matched for the data-phx-stream.

Co-authored-by: Chris McCord <[email protected]>
  • Loading branch information
danschultzer and chrismccord authored Dec 18, 2023
1 parent 4ca4885 commit db964c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/phoenix_live_view/test/dom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,11 @@ defmodule Phoenix.LiveViewTest.DOM do
child -> set_attr(child, "data-phx-stream", ref)
end

parent_id = parent_id(html_tree, id)

cond do
# skip added children that aren't ours if they are not being appended
not_appended? and parent_id(html_tree, id) != container_id ->
not_appended? && parent_id && parent_id != container_id ->
acc

# do not append existing child if already present, only update in place
Expand Down
14 changes: 14 additions & 0 deletions test/phoenix_live_view/integrations/stream_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ defmodule Phoenix.LiveView.StreamTest do

refute has_element?(lv, "li", "Apples")
refute has_element?(lv, "li", "Oranges")

lv
|> element("a", "Switch")
|> render_click()

assert_patched(lv, "/healthy/fruits")

assert has_element?(lv, "h1", "Fruits")

refute has_element?(lv, "li", "Carrots")
refute has_element?(lv, "li", "Tomatoes")

assert has_element?(lv, "li", "Apples")
assert has_element?(lv, "li", "Oranges")
end

describe "within live component" do
Expand Down

0 comments on commit db964c5

Please sign in to comment.