-
Notifications
You must be signed in to change notification settings - Fork 963
Commit
to specify that a LiveView should not automatically connect on dead render. When navigated to and there's already a connection established, this option has no effect. See https://elixirforum.com/t/liveview-feature-to-cancel-second-render/67770
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule Phoenix.LiveViewTest.E2E.LifecycleLive do | ||
use Phoenix.LiveView | ||
|
||
@impl Phoenix.LiveView | ||
def mount(params, _session, socket) do | ||
auto_connect = case params do | ||
%{"auto_connect" => "false"} -> false | ||
_ -> true | ||
end | ||
|
||
{:ok, socket, auto_connect: auto_connect} | ||
end | ||
|
||
@impl Phoenix.LiveView | ||
def render(assigns) do | ||
~H""" | ||
<div>Hello!</div> | ||
<.link navigate="/lifecycle">Navigate to self (auto_connect=true)</.link> | ||
<.link navigate="/lifecycle?auto_connect=false">Navigate to self (auto_connect=false)</.link> | ||
""" | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const {test, expect} = require("../test-fixtures") | ||
const {syncLV} = require("../utils") | ||
|
||
test.describe("auto_connect", () => { | ||
let webSocketEvents = [] | ||
let networkEvents = [] | ||
let consoleMessages = [] | ||
|
||
test.beforeEach(async ({page}) => { | ||
networkEvents = [] | ||
webSocketEvents = [] | ||
consoleMessages = [] | ||
|
||
page.on("request", request => networkEvents.push({method: request.method(), url: request.url()})) | ||
|
||
page.on("websocket", ws => { | ||
ws.on("framesent", event => webSocketEvents.push({type: "sent", payload: event.payload})) | ||
ws.on("framereceived", event => webSocketEvents.push({type: "received", payload: event.payload})) | ||
ws.on("close", () => webSocketEvents.push({type: "close"})) | ||
}) | ||
|
||
page.on("console", msg => consoleMessages.push(msg.text())) | ||
}) | ||
|
||
test("connects by default", async ({page}) => { | ||
await page.goto("/lifecycle") | ||
await syncLV(page) | ||
|
||
expect(webSocketEvents).toHaveLength(2) | ||
}) | ||
|
||
test("does not connect when auto_connect is false", async ({page}) => { | ||
await page.goto("/lifecycle?auto_connect=true") | ||
// eslint-disable-next-line playwright/no-networkidle | ||
await page.waitForLoadState("networkidle") | ||
expect(webSocketEvents).toHaveLength(0) | ||
Check failure on line 36 in test/e2e/tests/lifecycle.spec.js
|
||
}) | ||
|
||
test("connects when navigating to a view with auto_connect=true", async ({page}) => { | ||
await page.goto("/lifecycle?auto_connect=false") | ||
// eslint-disable-next-line playwright/no-networkidle | ||
await page.waitForLoadState("networkidle") | ||
expect(webSocketEvents).toHaveLength(0) | ||
await page.getByRole("link", {name: "Navigate to self (auto_connect=true)"}).click() | ||
await syncLV(page) | ||
expect(webSocketEvents).toHaveLength(2) | ||
}) | ||
|
||
test("stays connected when navigating to a view with auto_connect=false", async ({page}) => { | ||
await page.goto("/lifecycle") | ||
await syncLV(page) | ||
expect(webSocketEvents).toHaveLength(2) | ||
await page.getByRole("link", {name: "Navigate to self (auto_connect=false)"}).click() | ||
await syncLV(page) | ||
expect(webSocketEvents).toHaveLength(7) | ||
Check failure on line 55 in test/e2e/tests/lifecycle.spec.js
|
||
}) | ||
}) |