View Source Heyya.LiveCase (heyya v1.1.0)
Heyya.LiveCase
module provides helper methods that make it easier to write a
more linear live view test. It takes care of the fact that there's
connection state, view state, and last html state.
It does this by creating a LiveTestSession
struct via start/2
that
holds the connection, view, and html in a single struct. This is
wrapped in an CaseTemplate so that you can use it in your tests.
These ideas origionally came from: https://www.reddit.com/r/elixir/comments/ydyt2m/better_liveview_tests/
This implementation doesn't hide follow (in large part because of the ergonomics of follow_redirect being a macro)
Example
defmodule MyPhoenixWeb.ListLiveTest do
use Heyya.LiveCase
use MyPhoenixWeb.ConnCase
test "widget list with new button", %{conn: conn} do
start(conn, ~p|/widgets|)
|> assert_html("Widgets List")
|> click("a", "New Widgets")
|> follow(~p|/widgets/new|)
end
end
Summary
Functions
Asserts that the html in the LiveTestSession matches the provided expected html.
This macro will assert that the rendered html matches the snapshot.
This method will use LiveViewTest.render_async to ensure that all outstanding async operations are completed. By default this will wait the ExUnit default timeout.
This method clicks on an element that's selected via selector then renders the result. Finally returning a new LiveTestSession with updated rendered html
Macro that creates code to follow a redirect. This takes in a LiveTestSession and will assert that a redirect exists.
Sets the form specified by the selector to a new value.
Refute that the html in the LiveTestSession matches the provided html string.
Macro that creates code to start a new LiveTestSession. This takes in a plug connection (Usually from your ConnTest)
Submits the form and renders the result of submitting.
Functions
@spec assert_element(Heyya.LiveTestSession.t(), String.t(), String.t() | nil) :: Heyya.LiveTestSession.t()
@spec assert_html(Heyya.LiveTestSession.t(), String.t() | Regex.t()) :: Heyya.LiveTestSession.t()
Asserts that the html in the LiveTestSession matches the provided expected html.
Returns the LiveTestSession.
@spec assert_matches_snapshot( Heyya.LiveTestSession.t(), keyword() ) :: any()
This macro will assert that the rendered html matches the snapshot.
Options
The macro accepts the following options in a keyword list:
:name
- The name of the snapshot. Defaults to the line number. It's not recommended to use the default since moving the test will change the snapshot name. Note: names are scoped per function.:selector
- The selector to use to match the snapshot. Defaults to "main". Selector should always select a single element so that LiveViewTest can render it to html.
Example
For example the following will start the live view at /numbers and then assert that the element selected by #btn-32 matches the snapshot named "numbers"
test "/numbers renders the a button", %{conn: conn} do
conn
|> start(~p"/numbers")
|> assert_matches_snapshot(name: "numbers", selector: "#btn-32")
end
@spec assert_page_title(Heyya.LiveTestSession.t(), String.t() | Regex.t()) :: Heyya.LiveTestSession.t()
This method will use LiveViewTest.render_async to ensure that all outstanding async operations are completed. By default this will wait the ExUnit default timeout.
@spec click(Heyya.LiveTestSession.t(), String.t(), String.t() | nil) :: Heyya.LiveTestSession.t()
This method clicks on an element that's selected via selector then renders the result. Finally returning a new LiveTestSession with updated rendered html
This will also assert that there is an element that's specified by the selector
This doesn't follow navigations or redirect.
returns: %Heyya.LiveTestSession{}
@spec focus(Heyya.LiveTestSession.t(), binary()) :: Heyya.LiveTestSession.t()
Macro that creates code to follow a redirect. This takes in a LiveTestSession and will assert that a redirect exists.
@spec form(Heyya.LiveTestSession.t(), String.t(), any()) :: Heyya.LiveTestSession.t()
Sets the form specified by the selector to a new value.
This doesn't submit the form
returns: %Heyya.LiveTestSession{}
@spec refute_element(Heyya.LiveTestSession.t(), String.t(), String.t() | nil) :: Heyya.LiveTestSession.t()
@spec refute_html(Heyya.LiveTestSession.t(), String.t() | Regex.t()) :: Heyya.LiveTestSession.t()
Refute that the html in the LiveTestSession matches the provided html string.
Returns the LiveTestSession.
@spec refute_page_title(Heyya.LiveTestSession.t(), String.t() | Regex.t()) :: Heyya.LiveTestSession.t()
Macro that creates code to start a new LiveTestSession. This takes in a plug connection (Usually from your ConnTest)
@spec submit_form(Heyya.LiveTestSession.t(), String.t(), any()) :: Heyya.LiveTestSession.t()
Submits the form and renders the result of submitting.
This doesn't follow any redirects.
returns: %Heyya.LiveTestSession{}