Erabi.Test (Erabi v0.1.0)

Copy Markdown View Source

Helpers for Phoenix.LiveViewTest, to choose and search in tests with no browser.

import Erabi.Test

test "labels a task", %{conn: conn} do
  {:ok, view, _html} = live(conn, ~p"/tasks/1")

  assert choices(view, "task_labels_picker") == ["Bug", "Feature", "Docs"]
  html = choose(view, "task_labels_picker", ["bug", "docs"])
  assert html =~ "2 labels"
end

choose/3 sends the form's phx-change with the values in the picker's input, as the picker does in a browser, and with the other pickers of the form as they are. The form must have an id. The values are checked by the LiveView, not here: a test can choose a value that the rules refuse, to see the error.

The inputs of a picker are not updated in LiveViewTest's copy of the page (they are under phx-update="ignore", as in a browser, where the hook keeps them). To submit, give the form the values:

view
|> form("#task-form", task: %{labels: ["bug"]})
|> render_submit()

search/3 runs a picker's search (Erabi.Live) as the picker does, and returns the labels it finds.

Summary

Functions

The labels of a picker's options, in order, as its list shows them before people type. (Not options/2: Phoenix.ConnTest has one.)

Chooses values in the picker with the id picker_id (a value, a list of them for a picker of several, or nil to clear it), and returns the rendered LiveView (as Phoenix.LiveViewTest.render_change/2 does). A value is what the picker posts: an option's value, as text or as it is (an atom, an integer).

The labels of the values chosen in a picker, as the server rendered it.

Runs the search of the picker picker_id for a query, as the picker does as people type, and returns the labels it finds (the first page).

Functions

choices(view, picker_id)

@spec choices(struct(), String.t()) :: [String.t()]

The labels of a picker's options, in order, as its list shows them before people type. (Not options/2: Phoenix.ConnTest has one.)

choose(view, picker_id, value)

@spec choose(struct(), String.t(), term()) :: String.t()

Chooses values in the picker with the id picker_id (a value, a list of them for a picker of several, or nil to clear it), and returns the rendered LiveView (as Phoenix.LiveViewTest.render_change/2 does). A value is what the picker posts: an option's value, as text or as it is (an atom, an integer).

chosen(view, picker_id)

@spec chosen(struct(), String.t()) :: [String.t()]

The labels of the values chosen in a picker, as the server rendered it.

search(view, picker_id, query)

@spec search(struct(), String.t(), String.t()) :: [String.t()]

Runs the search of the picker picker_id for a query, as the picker does as people type, and returns the labels it finds (the first page).