Fude.Test (Fude v0.1.0)

Copy Markdown View Source

Helpers for testing charts with Phoenix.LiveViewTest: they read what a chart shows, as the server made it for the browser.

import Fude.Test

test "shows the revenue of each region", %{conn: conn} do
  {:ok, view, _html} = live(conn, ~p"/sales")

  assert kind(view, "revenue") == "bar"
  assert series(view, "revenue") == ["Online", "Retail"]
  assert [%{"Region" => "North", "Revenue" => 120, "Channel" => "Online"} | _] = data(view, "revenue")
end

Each helper takes the view (or a render of it, a string) and the id of the chart. They need lazy_html, which the tests of LiveView 1.1 use.

Summary

Types

A LiveView under test (a Phoenix.LiveViewTest.View), or a render of one.

Functions

What the server sent the browser for the chart: its kind, scales and layers.

The data that the chart draws, as its table has it: a map for each mark, by the titles of the columns ("Region", "Revenue"), with a category by its name and a time as a Date or a DateTime.

What a screen reader says of the chart.

Why the chart does not draw (a field that is not in the data...), or nil when it draws.

The chart's kind: "bar", "line"...

The names of the chart's series (its colors), in order: [] for a chart of one.

The chart's title, or nil.

Types

view()

@type view() :: struct() | String.t()

A LiveView under test (a Phoenix.LiveViewTest.View), or a render of one.

Functions

chart(view, id)

@spec chart(view(), String.t()) :: map()

What the server sent the browser for the chart: its kind, scales and layers.

data(view, id)

@spec data(view(), String.t()) :: [map()]

The data that the chart draws, as its table has it: a map for each mark, by the titles of the columns ("Region", "Revenue"), with a category by its name and a time as a Date or a DateTime.

description(view, id)

@spec description(view(), String.t()) :: String.t() | nil

What a screen reader says of the chart.

error(view, id)

@spec error(view(), String.t()) :: String.t() | nil

Why the chart does not draw (a field that is not in the data...), or nil when it draws.

kind(view, id)

@spec kind(view(), String.t()) :: String.t()

The chart's kind: "bar", "line"...

series(view, id)

@spec series(view(), String.t()) :: [String.t()]

The names of the chart's series (its colors), in order: [] for a chart of one.

title(view, id)

@spec title(view(), String.t()) :: String.t() | nil

The chart's title, or nil.

value(view, id)

@spec value(view(), String.t()) :: number() | String.t() | nil

The single number of a Fude.number/2, Fude.trend/2 or Fude.progress/2.