View Source Heyya.LiveCase (heyya v1.0.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

Link to this function

assert_element(test_session, selector, text \\ nil)

View Source
@spec assert_element(Heyya.LiveTestSession.t(), String.t(), String.t() | nil) ::
  Heyya.LiveTestSession.t()
Link to this function

assert_html(test_session, expected_html)

View Source

Asserts that the html in the LiveTestSession matches the provided expected html.

Returns the LiveTestSession.

Link to this macro

assert_matches_snapshot(before, options \\ [])

View Source (macro)
@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
Link to this function

assert_page_title(test_session, expected)

View Source
@spec assert_page_title(Heyya.LiveTestSession.t(), String.t() | Regex.t()) ::
  Heyya.LiveTestSession.t()
Link to this function

await_async(test_session, timeout \\ Application.fetch_env!(:ex_unit, :assert_receive_timeout))

View Source

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.

Link to this function

click(test_session, selector, text \\ nil)

View Source

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{}

Link to this function

focus(test_session, selector)

View Source
Link to this macro

follow(before, to \\ nil)

View Source (macro)

Macro that creates code to follow a redirect. This takes in a LiveTestSession and will assert that a redirect exists.

Link to this function

form(test_session, selector, opts)

View Source

Sets the form specified by the selector to a new value.

This doesn't submit the form

returns: %Heyya.LiveTestSession{}

Link to this function

refute_element(test_session, selector, text \\ nil)

View Source
@spec refute_element(Heyya.LiveTestSession.t(), String.t(), String.t() | nil) ::
  Heyya.LiveTestSession.t()
Link to this function

refute_html(test_session, unexpected_html)

View Source

Refute that the html in the LiveTestSession matches the provided html string.

Returns the LiveTestSession.

Link to this function

refute_page_title(test_session, unexpected)

View Source
@spec refute_page_title(Heyya.LiveTestSession.t(), String.t() | Regex.t()) ::
  Heyya.LiveTestSession.t()
Link to this macro

start(conn, path)

View Source (macro)

Macro that creates code to start a new LiveTestSession. This takes in a plug connection (Usually from your ConnTest)

Link to this function

submit_form(test_session, selector, opts)

View Source

Submits the form and renders the result of submitting.

This doesn't follow any redirects.

returns: %Heyya.LiveTestSession{}