Tayori.Test (Tayori v0.1.0)

Copy Markdown View Source

Helpers for Phoenix.LiveViewTest, to test the news in an app's LiveViews with no browser: write entries, read the count of unread entries, open the panel and read it.

import Tayori.Test

test "a Pro customer sees the news for Pro", %{conn: conn} do
  publish!("Faster search")
  publish!("Shared boards", segments: ["plan:pro"])

  {:ok, view, _html} = conn |> log_in(pro_user()) |> live(~p"/projects")

  assert unread(view) == 2
  assert [%{title: "Shared boards", new: true}, %{title: "Faster search"}] = open_news(view)
  assert unread(view) == 0
end

The entries are written in the app's test database, through Tayori.Entries, so Tayori must be in the app's supervision tree in the tests too (it is when the app starts).

Summary

Functions

Announces the entries whose time has come, as Tayori's scheduler does, in the test's process: the app's notifier is called, the subscribers emailed and the webhooks told, or, with the :oban option, their jobs enqueued. Each entry is announced once.

Closes the panel of a LiveView.

The entries the panel of a LiveView shows, in order: each with its :title, :categories, and whether it is marked :new.

Opens the panel of a LiveView, as a click on its button does, and returns its entries (entries/1). It marks the news as seen.

Writes and publishes an entry, in the first language. Options

The count of unread entries that the button of a LiveView shows (0 when it shows none).

Functions

announce!()

@spec announce!() :: :ok

Announces the entries whose time has come, as Tayori's scheduler does, in the test's process: the app's notifier is called, the subscribers emailed and the webhooks told, or, with the :oban option, their jobs enqueued. Each entry is announced once.

entry = publish!("Exports")
announce!()
assert_enqueued worker: Tayori.Worker, args: %{"job" => "notify", "entry_id" => entry.id}

close_news(view)

@spec close_news(struct()) :: :ok

Closes the panel of a LiveView.

entries(view)

@spec entries(struct()) :: [
  %{title: String.t(), categories: [String.t()], new: boolean()}
]

The entries the panel of a LiveView shows, in order: each with its :title, :categories, and whether it is marked :new.

open_news(view)

@spec open_news(struct()) :: [map()]

Opens the panel of a LiveView, as a click on its button does, and returns its entries (entries/1). It marks the news as seen.

publish!(title, opts \\ [])

@spec publish!(String.t(), keyword()) :: Tayori.Entry.t()

Writes and publishes an entry, in the first language. Options:

  • :body - Markdown ("About <title>.");
  • :categories, :segments - lists of strings;
  • :at - the publication time (now), a time in the future schedules it;
  • :translations - other languages, %{"pt-PT" => "Título"} or %{"pt-PT" => {"Título", "Corpo"}}.

unread(view)

@spec unread(struct()) :: non_neg_integer()

The count of unread entries that the button of a LiveView shows (0 when it shows none).