Skip to content

Stream reset behaviour changed in 1.1.9 in LiveViewTest #3993

@Aratramba

Description

@Aratramba

Environment

  • Elixir version (elixir -v): Elixir 1.18.1 (compiled with Erlang/OTP 27)
  • Phoenix version (mix deps): 1.8
  • Phoenix LiveView version (mix deps): 1.1.12
  • Operating system: OSX
  • Browsers you attempted to reproduce this bug on (the more the merrier): -
  • Does the problem persist after removing "assets/node_modules" and trying again? Yes/no: Yes

Actual behavior

Release 1.1.9 introduced a change that broke one of my tests relating to streams. I can't quite figure out what it was exactly by looking through the changelogs. It seems only related to tests.

See this standalone test liveview-1.1.9-stream-reset.exs.txt. Changing the phoenix_live_view dependency from 1.1.9 to 1.1.8 will make it pass.

I'm rendering a stream:

  @impl Phoenix.LiveView
  def render(assigns) do
    ~H"""
    <ul id="items" phx-update="stream">
      <li :for={{dom_id, item} <- @streams.items} id={dom_id}>
        {item.name}
      </li>
    </ul>
    """
  end

On mount I create the stream

  def mount(_params, _session, socket) do
    {:ok,
     socket
     |> stream_configure(:items, dom_id: &"item-#{&1.uuid}")
     |> stream(:items, [])}
  end

In handle params I reset it

  def handle_params(_params, _uri, socket) do
    paginated_items = paginate_items(0, 3)

    {:noreply,
     socket
     |> assign(:page, 0)
     |> stream(:items, paginated_items, reset: true)}
  end

In the test i simulate a hook call to load more

  def handle_event("load-more", %{}, socket) do
    new_page = socket.assigns.page + 1

    paginated_items = paginate_items(new_page * 3, 3)

    {:noreply,
     socket
     |> stream(:items, paginated_items, at: -1)
     |> assign(:page, new_page)}
  end
html = render_hook(view, "load-more", %{})

This used to add 3 items to the list.
Since 1.1.9 it replaces the existing items with 3 new ones.

Expected behaviour

The test passes in LiveView 1.1.8, but breaks in 1.1.9.

In LiveView 1.1.8 this resulted in 6 items after the render hook call.
In LiveView 1.1.9 this results in 3 new items after the render hook call.

If I change to reset: false in handle_params, it passes in 1.1.9.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions