defmodule TestDispatch do @moduledoc """ 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. """ import Phoenix.ConnTest, only: [dispatch: 4, redirected_to: 2] import Phoenix.Controller, only: [endpoint_module: 1] import TestDispatch.Form import TestDispatch.Link @doc """ 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/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. """ @spec dispatch_form(Plug.Conn.t(), %{}, binary() | atom() | nil) :: Plug.Conn.t() def dispatch_form(conn, attrs \\ %{}, entity_or_test_selector \\ nil) def dispatch_form(%Plug.Conn{} = conn, %{} = attrs, entity_or_test_selector) when is_binary(entity_or_test_selector) or is_nil(entity_or_test_selector) or is_atom(entity_or_test_selector) do {form, selector_type} = find_form(conn, entity_or_test_selector) selector_tuple = {selector_type, entity_or_test_selector} form |> find_inputs(selector_tuple) |> Enum.map(&input_to_tuple(&1, selector_tuple)) |> update_input_values(attrs) |> prepend_entity(selector_tuple) |> send_to_action(form, conn) end def dispatch_form(conn, entity_or_test_selector, nil), do: dispatch_form(conn, %{}, entity_or_test_selector) @doc """ Works like `dispatch/3`. The test_selector is used to find the right form and the entity is used to find and fill the inputs correctly. """ @spec dispatch_form(Plug.Conn.t(), %{}, atom(), binary()) :: Plug.Conn.t() def dispatch_form(%Plug.Conn{} = conn, %{} = attrs, entity, test_selector) do {form, _} = find_form(conn, test_selector) selector_tuple = {:entity, entity} form |> find_inputs(selector_tuple) |> Enum.map(&input_to_tuple(&1, selector_tuple)) |> update_input_values(attrs) |> prepend_entity(selector_tuple) |> send_to_action(form, conn) end @doc """ 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 `` 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
| Comment | Upvote |
|---|---|
| This is perfect | Upvote Comment |