TestDispatch (TestDispatch v0.3.2) 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

Finds a link by a given conn, test_selector and an optional test_value.

Will take a conn that was redirected. It takes the path that was redirected to and performs a get on it. If the status does not match the redirected status it will raise an error. By default the status is 302.

Checks for an incomming mail and puts it on the conn as the resp_html. It returns the conn on which the click_link/3 can be called.

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.

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.

Works like submit_form/3 but instead of an entity or test_selector it uses the text of the button to match on the third argument.

Link to this section Functions

Link to this function

click_link(tree \\ nil, conn, test_selector, test_value \\ nil)

View Source

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 = click_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 = click_link(conn, "post-123-upvote-comment", "1") iex> with %Plug.Conn{request_path: "/posts/1/comments/1", method: "POST"} <- result, do: :ok :ok

Link to this function

dispatch_form(conn, attrs \\ %{}, entity_or_test_selector \\ nil)

View Source

Specs

dispatch_form(Plug.Conn.t(), %{}, binary() | atom() | nil) :: Plug.Conn.t()

See submit_form/3 for documentation.

Link to this function

dispatch_form(conn, attrs, entity, test_selector)

View Source

Specs

dispatch_form(Plug.Conn.t(), %{}, atom(), binary()) :: Plug.Conn.t()

See submit_form/3 for documentation.

Link to this function

dispatch_link(floki_tree \\ nil, conn, test_selector, test_value \\ nil)

View Source

Specs

dispatch_link(nil | Floki.html_tree(), Plug.Conn.t(), binary(), binary() | nil) ::
  Plug.Conn.t()
dispatch_link(Plug.Conn.t(), binary(), binary() | nil, nil) :: Plug.Conn.t()

See click_link/4 for documentation.

Link to this function

follow_redirect(conn, status \\ 302)

View Source

Specs

follow_redirect(Plug.Conn.t(), integer()) :: Plug.Conn.t()

Will take a conn that was redirected. It takes the path that was redirected to and performs a get on it. If the status does not match the redirected status it will raise an error. By default the status is 302.

Examples

iex> conn = build_conn() |> get("/posts/1")
iex> conn = click_link(conn, "post-123-delete-post")
iex> result = follow_redirect(conn, 302) |> html_response(200)
iex> if result =~ "Posts Index", do: :ok
:ok
Link to this function

receive_mail(conn, email_match \\ %{})

View Source

Specs

receive_mail(Plug.Conn.t(), %{}) :: Plug.Conn.t()

Checks for an incomming mail and puts it on the conn as the resp_html. It returns the conn on which the click_link/3 can be called.

Params

  • :subject Match the received mail to the subject
  • :to Match the received mail to the receiver of the mail
  • :from Match the received mail to the sender of the mail

Examples

iex> conn = build_conn() |> get("/posts/1") |> click_link("post-123-send-as-mail")
iex> conn = receive_mail(conn, %{subject: "Post 1"})
iex> result = click_link(conn, "post-123-show") |> html_response(200)
iex> if result =~ "Posts 123", do: :ok
Link to this function

submit_form(conn, attrs \\ %{}, entity_or_test_selector \\ nil)

View Source

Specs

submit_form(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 submit_form/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.

Link to this function

submit_form(conn, attrs, entity, test_selector)

View Source

Specs

submit_form(Plug.Conn.t(), %{}, atom(), binary()) :: Plug.Conn.t()

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.

Link to this function

submit_with_button(conn, attrs \\ %{}, button_text)

View Source

Specs

submit_with_button(Plug.Conn.t(), %{}, binary()) :: Plug.Conn.t()

Works like submit_form/3 but instead of an entity or test_selector it uses the text of the button to match on the third argument.

Examples

iex> submit_with_button(conn, %{answer_option: "elixir"}, "Finish Quiz")
%Plug.Conn{params: %{"answer_option" => "elixir"}}