Skip to content

Commit f4161bd

Browse files
authored
Add drag(source, to: target) (#85)
Add `Case.drag/3` to drag and drop in feature tests. Usage: ```ex |> drag("#source", to: "#target") |> drag(Selector.text("Draggable"), to: Selector.text("Target")) ``` Implements #84 See https://playwright.dev/docs/input#drag-and-drop
1 parent 6b33aaf commit f4161bd

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

lib/phoenix_test/playwright.ex

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,39 @@ defmodule PhoenixTest.Playwright do
560560
conn
561561
end
562562

563+
@drag_opts_schema [
564+
to: [
565+
type_spec: quote(do: selector()),
566+
type_doc: "`t:selector/0`",
567+
required: true,
568+
doc: "The target selector."
569+
]
570+
]
571+
572+
@doc """
573+
Drag and drop a source element to a target element.
574+
575+
## Options
576+
#{NimbleOptions.docs(@drag_opts_schema)}
577+
578+
## Examples
579+
|> drag("#source", to: "#target")
580+
|> drag(Selector.text("Draggable"), to: Selector.text("Target"))
581+
"""
582+
@spec drag(t(), selector(), [
583+
unquote(NimbleOptions.option_typespec(@drag_opts_schema))
584+
]) :: t()
585+
def drag(conn, source_selector, to: target_selector) do
586+
source_selector = conn |> maybe_within() |> Selector.concat(source_selector)
587+
target_selector = conn |> maybe_within() |> Selector.concat(target_selector)
588+
589+
conn.frame_id
590+
|> Frame.drag_and_drop(source_selector, target_selector)
591+
|> handle_response(source_selector)
592+
593+
conn
594+
end
595+
563596
def assert_path(conn, path, opts \\ []) do
564597
if opts[:query_params] do
565598
retry(fn -> Assertions.assert_path(conn, path, opts) end, timeout(opts))

lib/phoenix_test/playwright/case.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ defmodule PhoenixTest.Playwright.Case do
2424
import PhoenixTest
2525

2626
import PhoenixTest.Playwright,
27+
# styler:sort
2728
only: [
2829
add_cookies: 2,
2930
add_session_cookie: 3,
@@ -34,6 +35,7 @@ defmodule PhoenixTest.Playwright.Case do
3435
click: 4,
3536
click_button: 4,
3637
click_link: 4,
38+
drag: 3,
3739
press: 3,
3840
press: 4,
3941
screenshot: 2,

lib/phoenix_test/playwright/frame.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,13 @@ defmodule PhoenixTest.Playwright.Frame do
158158
|> post()
159159
|> Result.from_response(& &1)
160160
end
161+
162+
def drag_and_drop(frame_id, source_selector, target_selector, opts \\ []) do
163+
params = %{source: source_selector, target: target_selector, strict: true}
164+
params = Enum.into(opts, params)
165+
166+
[guid: frame_id, method: :drag_and_drop, params: params]
167+
|> post()
168+
|> Result.from_response(& &1)
169+
end
161170
end

test/phoenix_test/playwright_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,16 @@ defmodule PhoenixTest.PlaywrightTest do
873873
end
874874
end
875875

876+
describe "drag/3" do
877+
test "triggers a javascript event handler", %{conn: conn} do
878+
conn
879+
|> visit("/live/index")
880+
|> refute_has("#drag-status", text: "dropped")
881+
|> drag(Selector.text("Drag this"), to: Selector.text("Drop here"))
882+
|> assert_has("#drag-status", text: "dropped")
883+
end
884+
end
885+
876886
describe "add_cookies/2" do
877887
test "sets a plain cookie", %{conn: conn} do
878888
conn

test/support/index_live.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,12 @@ defmodule PhoenixTest.IndexLive do
441441
<div id="hook-with-redirect" phx-hook="SomeOtherHook"></div>
442442
443443
<.link data-confirm="Are you sure?" navigate="/live/page_2">Confirm to navigate</.link>
444+
445+
<div id="drag-and-drop">
446+
<div id="drag-status">pending</div>
447+
<div id="drag-source" style="background: yellow;" draggable="true">Drag this</div>
448+
<div id="drag-target" style="border: 1px dashed black;" ondrop="document.getElementById('drag-status').innerHTML = 'dropped'">Drop here</div>
449+
</div>
444450
"""
445451
end
446452

0 commit comments

Comments
 (0)