TestDispatch v0.2.0 TestDispatch View Source
A module that contains a function to test the dispatch of forms. This will make it easier to write integration tests to check if forms in Phoenix templates will submit to the intended controller action with the right params.
Link to this section Summary
Functions
Will find a form in the HTML response of the given conn by entity or by
TestSelector
, or, if no entity or test_selector is provided, it will target
the last form found in the response.
Finds a link by a given conn, test_selector and an optional test_value.
Link to this section Functions
dispatch_form_with(conn, attrs \\ %{}, entity_or_test_selector \\ nil)
View Sourcedispatch_form_with(Plug.Conn.t(), %{}, binary() | atom() | nil) :: Plug.Conn.t()
Will find a form in the HTML response of the given conn by entity or by
TestSelector
, or, if no entity or test_selector is provided, it will target
the last form found in the response.
Next it will look for form controls (inputs, selects), convert these to params
and use the attributes passed to dispatch_form_with/3
to update the values
of the params. The params will now only contain field keys found in the
controls of the form.
If an entity is given, the params will be prepended by this entity.
Ultimately, the conn is dispatched to the conn's private.phoenix_endpoint
using Phoenix.ConnTest.dispatch/5
, with the params and with the method and
action found in the form.
dispatch_link(conn, test_selector, test_value \\ nil)
View Sourcedispatch_link(Plug.Conn.t(), binary(), binary() | nil) :: Plug.Conn.t()
Finds a link by a given conn, test_selector and an optional test_value.
Hereby it tries to get a response from the conn and find the first <a></a>
element that
has the combination of the test_selector and test_value. The link that is found will be
dispatched with Phoenix.ConnTest.dispatch/4
. The method will be derived from the link
by the data-method
attribute and has "get" as default. The path will be taken from the
href
.
Examples
With the given page on "/posts/1"
<html>
<body>
<h1>Post</h1>
<a href="/posts/1" data-method="delete" test-selector="post-123-delete-post">
Remove
</a>
<table>
<th>Comment</td>
<th>Upvote</td>
<tr>
<td>This is perfect</td>
<td>
<a href="/posts/1/comments/1"
data-method="post"
test-value="1"
test-selector="post-123-upvote-comment" >
Upvote Comment
</td>
</tr>
</table>
</body>
</html>
iex> conn = build_conn() |> get("/posts/1")
iex> result = dispatch_link(conn, "post-123-delete-post")
iex> with %Plug.Conn{request_path: "/posts/1", method: "DELETE"} <- result, do: :ok
:ok
iex> conn = build_conn() |> get("/posts/1")
iex> result = dispatch_link(conn, "post-123-upvote-comment", "1")
iex> with %Plug.Conn{request_path: "/posts/1/comments/1", method: "POST"} <- result, do: :ok
:ok