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 test for disabled and readonly state
references phoenixframework#3009 references phoenixframework#2993 references phoenixframework#1759
- Loading branch information
Showing
4 changed files
with
142 additions
and
1 deletion.
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,65 @@ | ||
const { test, expect } = require("@playwright/test"); | ||
const { syncLV, attributeMutations } = require("../utils"); | ||
|
||
test("readonly state is restored after submits", async ({ page }) => { | ||
await page.goto("/form"); | ||
await syncLV(page); | ||
await expect(page.locator("input[name=a]")).toHaveAttribute("readonly"); | ||
let changesA = attributeMutations(page, "input[name=a]"); | ||
let changesB = attributeMutations(page, "input[name=b]"); | ||
// can submit multiple times and readonly input stays readonly | ||
await page.locator("button[type=submit]").click(); | ||
await syncLV(page); | ||
// a is readonly and should stay readonly | ||
await expect(await changesA()).toEqual(expect.arrayContaining([ | ||
{ attr: "data-phx-readonly", oldValue: null, newValue: "true" }, | ||
{ attr: "readonly", oldValue: "", newValue: "" }, | ||
{ attr: "data-phx-readonly", oldValue: "true", newValue: null }, | ||
{ attr: "readonly", oldValue: "", newValue: "" }, | ||
])); | ||
// b is not readonly, but LV will set it to readonly while submitting | ||
await expect(await changesB()).toEqual(expect.arrayContaining([ | ||
{ attr: "data-phx-readonly", oldValue: null, newValue: "false" }, | ||
{ attr: "readonly", oldValue: null, newValue: "" }, | ||
{ attr: "data-phx-readonly", oldValue: "false", newValue: null }, | ||
{ attr: "readonly", oldValue: "", newValue: null }, | ||
])); | ||
await expect(page.locator("input[name=a]")).toHaveAttribute("readonly"); | ||
await page.locator("button[type=submit]").click(); | ||
await syncLV(page); | ||
await expect(page.locator("input[name=a]")).toHaveAttribute("readonly"); | ||
}); | ||
|
||
test("button disabled state is restored after submits", async ({ page }) => { | ||
await page.goto("/form"); | ||
await syncLV(page); | ||
let changes = attributeMutations(page, "button[type=submit]"); | ||
await page.locator("button[type=submit]").click(); | ||
await syncLV(page); | ||
// submit button is disabled while submitting, but then restored | ||
await expect(await changes()).toEqual(expect.arrayContaining([ | ||
{ attr: "data-phx-disabled", oldValue: null, newValue: "false" }, | ||
{ attr: "disabled", oldValue: null, newValue: "" }, | ||
{ attr: "class", oldValue: null, newValue: "phx-submit-loading" }, | ||
{ attr: "data-phx-disabled", oldValue: "false", newValue: null }, | ||
{ attr: "disabled", oldValue: "", newValue: null }, | ||
{ attr: "class", oldValue: "phx-submit-loading", newValue: null }, | ||
])); | ||
}); | ||
|
||
test("non-form button (phx-disable-with) disabled state is restored after click", async ({ page }) => { | ||
await page.goto("/form"); | ||
await syncLV(page); | ||
let changes = attributeMutations(page, "button[type=button]"); | ||
await page.locator("button[type=button]").click(); | ||
await syncLV(page); | ||
// submit button is disabled while submitting, but then restored | ||
await expect(await changes()).toEqual(expect.arrayContaining([ | ||
{ attr: "data-phx-disabled", oldValue: null, newValue: "false" }, | ||
{ attr: "disabled", oldValue: null, newValue: "" }, | ||
{ attr: "class", oldValue: null, newValue: "phx-click-loading" }, | ||
{ attr: "data-phx-disabled", oldValue: "false", newValue: null }, | ||
{ attr: "disabled", oldValue: "", newValue: null }, | ||
{ attr: "class", oldValue: "phx-click-loading", newValue: null }, | ||
])); | ||
}); |
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,33 @@ | ||
defmodule Phoenix.LiveViewTest.E2E.FormLive do | ||
use Phoenix.LiveView | ||
|
||
@impl Phoenix.LiveView | ||
def mount(_params, _session, socket) do | ||
{:ok, socket} | ||
end | ||
|
||
@impl Phoenix.LiveView | ||
def handle_event("validate", _params, socket) do | ||
{:noreply, socket} | ||
end | ||
|
||
def handle_event("save", _params, socket) do | ||
{:noreply, socket} | ||
end | ||
|
||
def handle_event("button-test", _params, socket) do | ||
{:noreply, socket} | ||
end | ||
|
||
@impl Phoenix.LiveView | ||
def render(assigns) do | ||
~H""" | ||
<form id="test-form" phx-submit="save" phx-change="validate"> | ||
<input type="text" name="a" readonly value="foo" /> | ||
<input type="text" name="b" value="bar" /> | ||
<button type="submit" phx-disable-with="Submitting">Submit</button> | ||
<button type="button" phx-click="button-test" phx-disable-with="Loading">Non-form Button</button> | ||
</form> | ||
""" | ||
end | ||
end |