forked from phoenixframework/phoenix_live_view
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for phoenixframework#3083, phoenixframework#3107
- Loading branch information
Showing
6 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { test, expect } = require("@playwright/test"); | ||
const { syncLV, evalLV } = require("../../utils"); | ||
|
||
test("select multiple handles option updates properly", async ({ page }) => { | ||
await page.goto("/issues/3083?auto=false"); | ||
await syncLV(page); | ||
|
||
await expect(page.locator("select")).toHaveValues([]); | ||
|
||
await evalLV(page, "send(self(), {:select, [1,2]})"); | ||
await expect(page.locator("select")).toHaveValues(["1", "2"]); | ||
await evalLV(page, "send(self(), {:select, [2,3]})"); | ||
await expect(page.locator("select")).toHaveValues(["2", "3"]); | ||
|
||
// now focus the select by interacting with it | ||
await page.locator("select").click({ position: { x: 1, y: 1 }}); | ||
await expect(page.locator("select")).toHaveValues(["1"]); | ||
await evalLV(page, "send(self(), {:select, [1,2]})"); | ||
// because the select is focused, we do not expect the values to change | ||
await expect(page.locator("select")).toHaveValues(["1"]); | ||
// now blur the select by clicking on the body | ||
await page.locator("body").click(); | ||
await expect(page.locator("select")).toHaveValues(["1"]); | ||
// now update the selected values again | ||
await evalLV(page, "send(self(), {:select, [3,4]})"); | ||
// we had a bug here, where the select was focused, despite the blur | ||
await expect(page.locator("select")).not.toBeFocused(); | ||
await expect(page.locator("select")).toHaveValues(["3", "4"]); | ||
await page.waitForTimeout(1000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const { test, expect } = require("@playwright/test"); | ||
const { syncLV } = require("../../utils"); | ||
|
||
test("keeps value when updating select", async ({ page }) => { | ||
await page.goto("/issues/3107"); | ||
await syncLV(page); | ||
|
||
await expect(page.locator("select")).toHaveValue("ONE"); | ||
// focus the element and change the value, like a user would | ||
await page.locator("select").focus(); | ||
await page.locator("select").selectOption("TWO"); | ||
await syncLV(page); | ||
await expect(page.locator("select")).toHaveValue("TWO"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule Phoenix.LiveViewTest.E2E.Issue3083Live do | ||
use Phoenix.LiveView | ||
|
||
# https://github.com/phoenixframework/phoenix_live_view/issues/3083 | ||
|
||
@impl Phoenix.LiveView | ||
def mount(params, _session, socket) do | ||
if connected?(socket) and not (params["auto"] == "false") do | ||
:timer.send_interval(1000, self(), :tick) | ||
end | ||
|
||
{:ok, socket |> assign(options: [1, 2, 3, 4, 5], form: to_form(%{"ids" => []}))} | ||
end | ||
|
||
@impl Phoenix.LiveView | ||
def handle_info(:tick, socket) do | ||
selected = Enum.take_random([1, 2, 3, 4, 5], 2) | ||
params = %{"ids" => selected} | ||
|
||
{:noreply, socket |> assign(form: to_form(params))} | ||
end | ||
|
||
def handle_info({:select, values}, socket) do | ||
params = %{"ids" => values} | ||
|
||
{:noreply, socket |> assign(form: to_form(params))} | ||
end | ||
|
||
@impl Phoenix.LiveView | ||
def handle_event("validate", _params, socket) do | ||
{:noreply, socket} | ||
end | ||
|
||
@impl Phoenix.LiveView | ||
def render(assigns) do | ||
~H""" | ||
<.form id="form" for={@form} phx-change="validate"> | ||
<select | ||
id={@form[:ids].id} | ||
name={@form[:ids].name <> "[]"} | ||
multiple={true} | ||
> | ||
<%= Phoenix.HTML.Form.options_for_select(@options, @form[:ids].value) %> | ||
</select> | ||
<input type="text" placeholder="focus me!"/> | ||
</.form> | ||
""" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Phoenix.LiveViewTest.E2E.Issue3107Live do | ||
use Phoenix.LiveView | ||
|
||
# https://github.com/phoenixframework/phoenix_live_view/issues/3107 | ||
|
||
@impl Phoenix.LiveView | ||
def mount(_params, _session, socket) do | ||
{:ok, | ||
socket | ||
|> assign(:form, Phoenix.Component.to_form(%{})) | ||
|> assign(:disabled, true) | ||
} | ||
end | ||
|
||
@impl Phoenix.LiveView | ||
def handle_event("validate", _, socket) do | ||
{:noreply, assign(socket, :disabled, false)} | ||
end | ||
|
||
@impl Phoenix.LiveView | ||
def render(assigns) do | ||
~H""" | ||
<.form for={@form} phx-change="validate" style="display: flex;"> | ||
<select> | ||
<option value="ONE">ONE</option> | ||
<option value="TWO">TWO</option> | ||
</select> | ||
<button disabled={@disabled}>OK</button> | ||
</.form> | ||
""" | ||
end | ||
end |