From f9336154ae0f2fe7e670a5d3a3b190c484b2c5b0 Mon Sep 17 00:00:00 2001 From: Steffen Deusch Date: Fri, 10 Jan 2025 14:24:46 +0100 Subject: [PATCH] add e2e test for #3529 --- test/e2e/support/issues/issue_3529.ex | 23 ++++++++++++++ test/e2e/test_helper.exs | 1 + test/e2e/tests/issues/3529.spec.js | 45 +++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 test/e2e/support/issues/issue_3529.ex create mode 100644 test/e2e/tests/issues/3529.spec.js diff --git a/test/e2e/support/issues/issue_3529.ex b/test/e2e/support/issues/issue_3529.ex new file mode 100644 index 000000000..4a1c916e6 --- /dev/null +++ b/test/e2e/support/issues/issue_3529.ex @@ -0,0 +1,23 @@ +defmodule Phoenix.LiveViewTest.E2E.Issue3529Live do + # https://github.com/phoenixframework/phoenix_live_view/issues/3529 + + use Phoenix.LiveView + + alias Phoenix.LiveView.JS + + def mount(_params, _session, socket) do + {:ok, assign(socket, :mounted, DateTime.utc_now())} + end + + def handle_params(_params, _uri, socket) do + {:noreply, assign(socket, :next, :rand.uniform())} + end + + def render(assigns) do + ~H""" +

Mounted at {@mounted}

+ <.link navigate={"/issues/3529?param=#{@next}"}>Navigate + <.link patch={"/issues/3529?param=#{@next}"}>Patch + """ + end +end diff --git a/test/e2e/test_helper.exs b/test/e2e/test_helper.exs index 413a7e685..5f3418fa3 100644 --- a/test/e2e/test_helper.exs +++ b/test/e2e/test_helper.exs @@ -158,6 +158,7 @@ defmodule Phoenix.LiveViewTest.E2E.Router do live "/3448", Issue3448Live live "/3496/a", Issue3496.ALive live "/3496/b", Issue3496.BLive + live "/3529", Issue3529Live live "/3651", Issue3651Live end end diff --git a/test/e2e/tests/issues/3529.spec.js b/test/e2e/tests/issues/3529.spec.js new file mode 100644 index 000000000..f2af4cadc --- /dev/null +++ b/test/e2e/tests/issues/3529.spec.js @@ -0,0 +1,45 @@ +const {test, expect} = require("../../test-fixtures") +const {syncLV} = require("../../utils") + +const pageText = async (page) => await page.evaluate(() => document.querySelector("h1").innerText) + +// https://github.com/phoenixframework/phoenix_live_view/issues/3529 +// https://github.com/phoenixframework/phoenix_live_view/pull/3625 +test("forward and backward navigation is handled properly (replaceRootHistory)", async ({page}) => { + await page.goto("/issues/3529") + await syncLV(page) + + let text = await pageText(page) + await page.getByRole("link", {name: "Navigate"}).click() + await syncLV(page) + + // navigate remounts and changes the text + expect(await pageText(page)).not.toBe(text) + text = await pageText(page) + + await page.getByRole("link", {name: "Patch"}).click() + await syncLV(page) + // patch does not remount + expect(await pageText(page)).toBe(text) + + // now we go back (should be patch again) + await page.goBack() + await syncLV(page) + expect(await pageText(page)).toBe(text) + + // and then we back to the initial page and use back/forward + // this should be a navigate -> remount! + await page.goBack() + await syncLV(page) + expect(await pageText(page)).not.toBe(text) + + // navigate + await page.goForward() + await syncLV(page) + text = await pageText(page) + + // now back again (navigate) + await page.goBack() + await syncLV(page) + expect(await pageText(page)).not.toBe(text) +})