diff --git a/lib/phoenix_live_view/utils.ex b/lib/phoenix_live_view/utils.ex index bf053ce3b..20e6a76ed 100644 --- a/lib/phoenix_live_view/utils.ex +++ b/lib/phoenix_live_view/utils.ex @@ -96,13 +96,11 @@ defmodule Phoenix.LiveView.Utils do def force_assign(assigns, nil, key, val), do: Map.put(assigns, key, val) def force_assign(assigns, changed, key, val) do - # If the current value is a map, we store it in changed so - # we can perform nested change tracking. Also note the use - # of put_new is important. We want to keep the original value + # We store old value in changed so we can perform nested change tracking. + # Also note the use of put_new is important. We want to keep the original value # from assigns and not any intermediate ones that may appear. current_val = Map.get(assigns, key) - changed_val = if is_map(current_val), do: current_val, else: true - changed = Map.put_new(changed, key, changed_val) + changed = Map.put_new(changed, key, current_val) Map.put(%{assigns | __changed__: changed}, key, val) end diff --git a/test/phoenix_component_test.exs b/test/phoenix_component_test.exs index 697bf8fe3..e87e29b63 100644 --- a/test/phoenix_component_test.exs +++ b/test/phoenix_component_test.exs @@ -50,6 +50,23 @@ defmodule Phoenix.ComponentUnitTest do assert socket.assigns.existing == %{foo: :bam} assert socket.assigns.__changed__.existing == %{foo: :bar} end + + test "keeps whole lists in changes" do + socket = assign(@socket, existing: [:foo, :bar]) + socket = Utils.clear_changed(socket) + + socket = assign(socket, existing: [:foo, :baz]) + assert socket.assigns.existing == [:foo, :baz] + assert socket.assigns.__changed__.existing == [:foo, :bar] + + socket = assign(socket, existing: [:foo, :bat]) + assert socket.assigns.existing == [:foo, :bat] + assert socket.assigns.__changed__.existing == [:foo, :bar] + + socket = assign(socket, %{existing: [:foo, :bam]}) + assert socket.assigns.existing == [:foo, :bam] + assert socket.assigns.__changed__.existing == [:foo, :bar] + end end describe "assign with assigns" do @@ -102,7 +119,7 @@ defmodule Phoenix.ComponentUnitTest do notexisting: "new-notexisting", live_action: nil, flash: %{}, - __changed__: %{existing: true, notexisting: true} + __changed__: %{existing: nil, notexisting: nil} } end @@ -120,7 +137,7 @@ defmodule Phoenix.ComponentUnitTest do notexisting: "new-notexisting", live_action: nil, flash: %{}, - __changed__: %{existing: true, notexisting: true, existing2: true} + __changed__: %{existing: nil, notexisting: nil, existing2: nil} } end @@ -143,11 +160,11 @@ defmodule Phoenix.ComponentUnitTest do live_action: nil, flash: %{}, __changed__: %{ - existing: true, - existing2: true, - notexisting: true, - notexisting2: true, - notexisting3: true + existing: nil, + existing2: nil, + notexisting: nil, + notexisting2: nil, + notexisting3: nil } } end