From a97db011e9a76bfb6bd071bce0659af0f0f6284b Mon Sep 17 00:00:00 2001 From: FatboyPunk Date: Wed, 20 Oct 2021 14:31:23 +0200 Subject: [PATCH 1/3] duplicate dispatch_form and name it submit_form --- lib/test_dispatch.ex | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/test_dispatch.ex b/lib/test_dispatch.ex index f9b11e7..c61c1a1 100644 --- a/lib/test_dispatch.ex +++ b/lib/test_dispatch.ex @@ -65,6 +65,19 @@ defmodule TestDispatch do |> send_to_action(form, conn) end + @spec submit_form(Plug.Conn.t(), %{}, binary() | atom() | nil) :: Plug.Conn.t() + def submit_form(conn, attrs, entity_or_test_selector), + do: dispatch_form(conn, attrs, entity_or_test_selector) + + @doc """ + Works like `submit_form/3`. The test_selector is used to find the right form and the + entity is used to find and fill the inputs correctly. + """ + @spec submit_form(Plug.Conn.t(), %{}, atom(), binary()) :: Plug.Conn.t() + + def submit_form(conn, attrs, entity, test_selector), + do: dispatch_form(conn, attrs, entity, test_selector) + @doc """ Finds a link by a given conn, test_selector and an optional test_value. @@ -135,6 +148,11 @@ defmodule TestDispatch do |> find_link(test_selector, test_value) |> _dispatch_link(conn) + @spec dispatch_link(nil | Floki.html_tree(), Plug.Conn.t(), binary(), binary() | nil) :: + Plug.Conn.t() + def click_link(conn, test_selector, test_value, tree), + do: dispatch_link(conn, test_selector, test_value, tree) + def _dispatch_link(link, conn) do endpoint = endpoint_module(conn) From 5d1c6b4c0ab6122d5892483485447eb7632a2cb3 Mon Sep 17 00:00:00 2001 From: FatboyPunk Date: Thu, 21 Oct 2021 13:58:28 +0200 Subject: [PATCH 2/3] test on submit_form/4 instead of dispatch_form --- test/test_dispatch_form_test.exs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/test_dispatch_form_test.exs b/test/test_dispatch_form_test.exs index 62ded40..1c41ae1 100644 --- a/test/test_dispatch_form_test.exs +++ b/test/test_dispatch_form_test.exs @@ -17,7 +17,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/new", %{form: "entity_and_form_controls"}) - |> dispatch_form(attrs, :user) + |> submit_form(attrs, :user) assert html_response(dispatched_conn, 200) == "user created" @@ -44,7 +44,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/new", %{form: "entity_and_form_controls"}) - |> dispatch_form(attrs, :user) + |> submit_form(attrs, :user) assert html_response(dispatched_conn, 200) == "not all required params are set" @@ -65,7 +65,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/new", %{form: "entity_and_form_controls"}) - |> dispatch_form(:user) + |> submit_form(:user) assert html_response(dispatched_conn, 200) == "not all required params are set" @@ -93,7 +93,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/new", %{form: "lacking_required_form_controls"}) - |> dispatch_form(attrs, :user) + |> submit_form(attrs, :user) assert html_response(dispatched_conn, 200) == "not all required params are set" @@ -120,7 +120,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/new", %{form: "test_selector_and_form_controls"}) - |> dispatch_form(attrs, "new-user") + |> submit_form(attrs, "new-user") assert html_response(dispatched_conn, 200) == "user created" @@ -137,7 +137,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/new", %{form: "test_selector_and_form_controls"}) - |> dispatch_form("new-user") + |> submit_form("new-user") assert html_response(dispatched_conn, 200) == "not all required params are set" @@ -156,7 +156,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/index") - |> dispatch_form("export-users") + |> submit_form("export-users") assert html_response(dispatched_conn, 200) == "users exported" assert params == %{} @@ -172,7 +172,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/index") - |> dispatch_form(attrs, "export-users") + |> submit_form(attrs, "export-users") assert html_response(dispatched_conn, 200) == "users exported" assert params == %{} @@ -194,7 +194,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/new", %{form: "only_form_controls"}) - |> dispatch_form(attrs) + |> submit_form(attrs) assert html_response(dispatched_conn, 200) == "user created" @@ -212,7 +212,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/new", %{form: "only_form_controls"}) - |> dispatch_form(%{}) + |> submit_form(%{}) assert html_response(dispatched_conn, 200) == "not all required params are set" @@ -232,7 +232,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/index") - |> dispatch_form() + |> submit_form() assert html_response(dispatched_conn, 200) == "users exported" assert params == %{} @@ -249,7 +249,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/index") - |> dispatch_form(attrs) + |> submit_form(attrs) assert html_response(dispatched_conn, 200) == "users exported" assert params == %{} @@ -263,7 +263,7 @@ defmodule TestDispatch.FormTest do RuntimeError, "No form found for the given test_selector or entity: new-user", fn -> - dispatch_form(conn, "new-user") + submit_form(conn, "new-user") end ) end @@ -272,7 +272,7 @@ defmodule TestDispatch.FormTest do conn = get(conn, "/users/new", %{form: "only_form_controls"}) assert_raise(RuntimeError, "No form found for the given test_selector or entity: user", fn -> - dispatch_form(conn, :user) + submit_form(conn, :user) end) end @@ -283,7 +283,7 @@ defmodule TestDispatch.FormTest do |> Plug.Conn.resp(200, "no form here") assert_raise(RuntimeError, "No form found for the given test_selector or entity: user", fn -> - dispatch_form(conn, :user) + submit_form(conn, :user) end) end @@ -302,7 +302,7 @@ defmodule TestDispatch.FormTest do dispatched_conn = conn |> get("/users/new", %{form: "multiple_selector_and_form_controls"}) - |> dispatch_form(attrs, :user, "user-profile") + |> submit_form(attrs, :user, "user-profile") assert html_response(dispatched_conn, 200) == "not all required params are set" From 615f1cc3e3644ff376f1f529fecc10db49705fe2 Mon Sep 17 00:00:00 2001 From: FatboyPunk Date: Thu, 21 Oct 2021 14:03:46 +0200 Subject: [PATCH 3/3] create a changelog with an update for submit_form --- CHANGELOG.md | 10 ++++++++++ lib/test_dispatch.ex | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..c0fe8db --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,10 @@ +# Changelog + +## master + +### Enhancements + + * [#93](https://github.com/DefactoSoftware/test_dispatch/pull/93) Forms can be + submitted with submit_form/4 which has the same behaviour as the current + dispatch_form/4 + diff --git a/lib/test_dispatch.ex b/lib/test_dispatch.ex index c61c1a1..418eb5f 100644 --- a/lib/test_dispatch.ex +++ b/lib/test_dispatch.ex @@ -66,7 +66,7 @@ defmodule TestDispatch do end @spec submit_form(Plug.Conn.t(), %{}, binary() | atom() | nil) :: Plug.Conn.t() - def submit_form(conn, attrs, entity_or_test_selector), + def submit_form(conn, attrs \\ %{}, entity_or_test_selector \\ nil), do: dispatch_form(conn, attrs, entity_or_test_selector) @doc """