From 39cdbbba7d6d403a91cb671135125cd5e419b8ab Mon Sep 17 00:00:00 2001 From: Steffen Deusch Date: Thu, 25 Jan 2024 23:34:32 +0100 Subject: [PATCH] Add e2e tests for live navigation and guards --- test/e2e/test_helper.exs | 30 ++- test/e2e/tests/navigation.spec.js | 214 ++++++++++++++++++++++ test/support/e2e/navigation.ex | 292 ++++++++++++++++++++++++++++++ 3 files changed, 533 insertions(+), 3 deletions(-) create mode 100644 test/e2e/tests/navigation.spec.js create mode 100644 test/support/e2e/navigation.ex diff --git a/test/e2e/test_helper.exs b/test/e2e/test_helper.exs index 4eb047fc3..025c5a61f 100644 --- a/test/e2e/test_helper.exs +++ b/test/e2e/test_helper.exs @@ -26,10 +26,12 @@ defmodule Phoenix.LiveViewTest.E2E.Layout do def render("live.html", assigns) do ~H""" + + +
+
+

Navigation

+ + <.link navigate="/navigation/a" style="background-color: #f1f5f9; padding: 0.5rem;"> + LiveView A + + + <.link navigate="/navigation/b" style="background-color: #f1f5f9; padding: 0.5rem;"> + LiveView B + + + <.link navigate="/navigation/c" style="background-color: #f1f5f9; padding: 0.5rem;"> + LiveView C + + + <.link navigate="/stream" style="background-color: #f1f5f9; padding: 0.5rem;"> + LiveView (other session) + +
+ +
+ <%= @inner_content %> +
+ + <.flash_group flash={@flash} /> +
+ """ + end + + defp flash_group(assigns) do + ~H""" +
+ <.flash kind={:info} title="Success!" flash={@flash} /> + <.flash kind={:error} title="Error!" flash={@flash} /> +
+ """ + end + + attr :id, :string + attr :flash, :map, default: %{} + attr :title, :string, default: nil + attr :kind, :atom, values: [:info, :error] + attr :rest, :global + + slot(:inner_block, required: false) + + defp flash(assigns) do + assigns = assign_new(assigns, :id, fn -> "flash-#{assigns.kind}" end) + + ~H""" +
JS.hide(to: "##{@id}")} + role="alert" + style={"position: fixed; top: 0.5rem; right: 0.5rem; margin-right: 0.5rem; width: 20rem; z-index: 50; border-radius: 0.375rem; padding: 0.75rem; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05); background-color: #{(@kind == :info && "#ECFDF5") || (@kind == :error && "#FEF2F2")}; color: #{(@kind == :info && "#065F46") || (@kind == :error && "#991B1B")}; ring-width: 1px; ring-color: #{(@kind == :info && "#10B981") || (@kind == :error && "#F43F5E")};"} + {@rest} + > +

+ <%= @title %> +

+

<%= msg %>

+ +
+ """ + end +end + +defmodule Phoenix.LiveViewTest.E2E.Navigation.ALive do + use Phoenix.LiveView + + @impl Phoenix.LiveView + def mount(_params, _session, socket) do + socket + |> assign(:param_current, nil) + |> then(&{:ok, &1}) + end + + @impl Phoenix.LiveView + def handle_params(params, _uri, socket) do + param = Map.get(params, "param") + + socket + |> assign(:param_current, param) + |> assign(:param_next, System.unique_integer()) + |> then(&{:noreply, &1}) + end + + @impl Phoenix.LiveView + def render(assigns) do + ~H""" +

This is page A

+ +

Current param: <%= @param_current %>

+ + <.link + patch={"/navigation/a?param=#{@param_next}"} + style="padding-left: 1rem; padding-right: 1rem; padding-top: 0.5rem; padding-bottom: 0.5rem; background-color: #e2e8f0; display: inline-flex; align-items: center; border-radius: 0.375rem; cursor: pointer;" + > + Patch this LiveView + + """ + end +end + +defmodule Phoenix.LiveViewTest.E2E.Navigation.BLive do + use Phoenix.LiveView + + @impl Phoenix.LiveView + def mount(_params, _session, socket) do + socket + |> assign(:form, to_form(%{})) + |> assign(:form_dirty, false) + |> then(&{:ok, &1}) + end + + @impl Phoenix.LiveView + def handle_event("validate", params, socket) do + socket + |> assign(:form, to_form(params)) + |> assign(:form_dirty, true) + |> then(&{:noreply, &1}) + end + + def handle_event("submit", _params, socket) do + socket + |> assign(:form, %{}) + |> assign(:form_dirty, false) + |> put_flash(:info, "Submitted successfully!") + |> then(&{:noreply, &1}) + end + + @impl Phoenix.LiveView + def render(assigns) do + ~H""" +

This is page C

+ +
+ + + + +
+ """ + end +end + +defmodule Phoenix.LiveViewTest.E2E.Navigation.CLive do + use Phoenix.LiveView + + @impl Phoenix.LiveView + def mount(_params, _session, socket) do + socket + |> then(&{:ok, &1}) + end + + @impl Phoenix.LiveView + def handle_params(params, _uri, socket) do + socket + |> assign(:container, not is_nil(params["container"])) + |> apply_action(socket.assigns.live_action, params) + |> then(&{:noreply, &1}) + end + + def apply_action(socket, :index, _params) do + items = + for i <- 1..100 do + %{id: "item-#{i}", name: i} + end + + stream(socket, :items, items) + end + + def apply_action(socket, :show, %{"id" => id}) do + assign(socket, :id, id) + end + + @impl Phoenix.LiveView + def render(assigns) do + ~H""" +

This is page D

+ +
+ +
+ +
+

Item <%= @id %>

+
+ """ + end +end