Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

18 changes: 18 additions & 0 deletions lib/test_dispatch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 \\ nil),
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.

Expand Down Expand Up @@ -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)

Expand Down
32 changes: 16 additions & 16 deletions test/test_dispatch_form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"

Expand All @@ -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"

Expand Down Expand Up @@ -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"

Expand All @@ -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"

Expand All @@ -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"

Expand All @@ -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 == %{}
Expand All @@ -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 == %{}
Expand All @@ -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"

Expand All @@ -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"

Expand All @@ -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 == %{}
Expand All @@ -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 == %{}
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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"

Expand Down