Skip to content

Commit 72edd9b

Browse files
committed
Fix: trigger phx-change event for input with phx-debounce=blur
1 parent e42ddae commit 72edd9b

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Add locale to avoid console warnings. Commit [becf5e6] @peaceful-james
1111

12+
### Fixed
13+
- Trigger `phx-change` event for input with `phx-debounce="blur"`
14+
1215
## [0.6.2] 2025-04-24
1316
### Changed
1417
- `Frame.evaluate/3`: Don't transform map keys in return value. Example: `js {camelCase: {a: 1}} -> ex %{"camelCase": %{"a": 1}}`. Previously attempted to underscore and atom-ize keys, which led to issue [#24](https://github.com/ftes/phoenix_test_playwright/pull/24). Commit [5ff530]

lib/phoenix_test/playwright.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,9 @@ defmodule PhoenixTest.Playwright do
793793
|> fun.(%{timeout: timeout(opts)})
794794
|> handle_response(selector)
795795

796+
# trigger phx-change if phx-debounce="blur"
797+
Frame.blur(session.frame_id, selector)
798+
796799
%{session | last_input_selector: selector}
797800
end
798801

lib/phoenix_test/playwright/frame.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ defmodule PhoenixTest.Playwright.Frame do
147147
|> unwrap_response(& &1)
148148
end
149149

150+
def blur(frame_id, selector, opts \\ []) do
151+
params = %{selector: selector}
152+
params = Enum.into(opts, params)
153+
154+
[guid: frame_id, method: :blur, params: params]
155+
|> post()
156+
|> unwrap_response(& &1)
157+
end
158+
150159
defp unwrap_response(response, fun) do
151160
case response do
152161
%{error: _} = error -> {:error, error}

test/phoenix_test/playwright_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ defmodule PhoenixTest.PlaywrightTest do
204204
|> assert_has("#form-data", text: "book-characters: Frodo")
205205
end
206206

207+
test "triggers phx-change event if phx-debounce=blur", %{conn: conn} do
208+
conn
209+
|> visit("/live/index")
210+
|> fill_in("Debounce blur", with: "triggers")
211+
|> assert_has("#form-data", text: "debounce-blur: triggers")
212+
end
213+
207214
test "raises an error when element can't be found with label", %{conn: conn} do
208215
assert_raise AssertionError, ~r/Could not find element/, fn ->
209216
conn

test/support/index_live.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ defmodule PhoenixTest.IndexLive do
187187
<label for={@uploads.avatar.ref}>Avatar</label>
188188
<.live_file_input upload={@uploads.avatar} />
189189
190+
<label for="debounce-blur">Debounce blur</label>
191+
<input id="debounce-blur" phx-debounce="blur" name="debounce-blur" phx-update="ignore" />
192+
190193
<button type="submit" name="full_form_button" value="save">Save Full Form</button>
191194
</form>
192195

0 commit comments

Comments
 (0)