Koyomi.Test (Koyomi v0.2.1)

Copy Markdown View Source

Helpers for Phoenix.LiveViewTest, to pick values in tests with no browser.

import Koyomi.Test

test "books a stay", %{conn: conn} do
  {:ok, view, _html} = live(conn, ~p"/book")

  html = pick(view, "booking_stay_picker", Date.range(~D[2027-03-01], ~D[2027-03-07]))
  assert html =~ "6 nights"
end

pick/3 sends the form's phx-change with the value in the picker's inputs, as the picker does in a browser, and with the other inputs of the form as they are. The form must have an id. The value is checked by the LiveView, not here: a test can pick a day 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("#booking-form", booking: %{stay: %{from: "2027-03-01", to: "2027-03-07"}})
|> render_submit()

request_days/4 sends the koyomi:days event of a picker with days and returns the reply, to test the day content of a LiveView.

Summary

Functions

Picks value in the picker with the id picker_id, and returns the rendered LiveView (as Phoenix.LiveViewTest.render_change/2 does).

Sends the koyomi:days event of the picker picker_id for the days from first to last, as the picker does when it shows them, and returns the reply: %{"days" => %{"2027-03-15" => %{...}}}.

Functions

pick(view, picker_id, value)

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

Picks value in the picker with the id picker_id, and returns the rendered LiveView (as Phoenix.LiveViewTest.render_change/2 does).

value is what the picker would post: a Date, a Date.Range (or {first, last}), a list of Date, a Time, a DateTime (posted with its offset), a NaiveDateTime, a Koyomi.Recurrence, an ISO string, or nil to clear it. In a relative picker, the key of a preset ("last_7_days" or :last_7_days) picks that preset, as a click on it does.

request_days(view, picker_id, first, last)

@spec request_days(struct(), String.t(), Date.t(), Date.t()) :: map()

Sends the koyomi:days event of the picker picker_id for the days from first to last, as the picker does when it shows them, and returns the reply: %{"days" => %{"2027-03-15" => %{...}}}.