AshBackendDrivenOptimisticUi.Test (AshBackendDrivenOptimisticUi v0.1.0)

Copy Markdown View Source

One-line checks that make refactoring an action safe. use this module inside an ExUnit module.

assert_snapshots_current()

in one test fails when any rule of the app has no committed snapshot, differs from it, or when a snapshot file names a rule that is gone. It covers every rule, so nothing is added per action. Fix it with mix optimistic_ui.snapshot --update, read the diff, commit. The snapshot_dir: option points elsewhere.

wallet = optimistic MyApp.Wallet, :spend, wallet, %{cost: 5}, actor: user, topics: ["wallet:1"]

evaluates the action's rule against the record and input, runs the real action, then fails unless every set of the rule equals the real record's value and every push of the rule was broadcast on one of topics: (its id, when it has one, included). Options: actor:, ctx: (default %{}), topics: (default [], which skips the push check) and reacting: (default []), the records of other resources as the client holds them before the action. For each reaction of the rule that answers a type among them and an event the rule pushed, the reaction's sets are evaluated against that record, the pushed payload (event) and the record's own ctx, and must equal the reloaded record; its pushes are checked on topics: too. A ctx: left out reads the record's own ctx field. Returns the real record. Values are compared as the client sees them: JSON, decimals as floats.

Summary

Functions

Fails unless every rule of the app has its committed snapshot, unchanged, and no snapshot file is left without a rule.

Runs action for real and fails unless it did what its rule said it would; returns the real record.

What the reaction of record's resource in resource's action rule gives when that rule pushes event (%{event: name, payload: map}), read against record and its own ctx, without running anything. Same shape as rules/5.

Whether resource's action rule takes record out of the client's list for input, without running the action: a remove the rule reaches through its wheres. A rule of a generic action runs on every row the client shows, so asking this of each row tells which rows the client takes out.

What resource's action rule gives for record and input, without running the action: %{sets: %{"attr" => value}, pushes: [%{event: "name", payload: map}]}. Takes the options optimistic takes; a ctx: left out reads the record's own ctx field.

Functions

assert_snapshots_current(opts \\ [])

Fails unless every rule of the app has its committed snapshot, unchanged, and no snapshot file is left without a rule.

optimistic(resource, action, record, input, opts \\ [])

(macro)

Runs action for real and fails unless it did what its rule said it would; returns the real record.

reaction(resource, action, record, map)

@spec reaction(module(), atom(), struct(), %{event: String.t(), payload: map()}) :: %{
  sets: %{required(String.t()) => term()},
  pushes: [%{event: String.t(), payload: map()}]
}

What the reaction of record's resource in resource's action rule gives when that rule pushes event (%{event: name, payload: map}), read against record and its own ctx, without running anything. Same shape as rules/5.

removes?(resource, action, record, input, opts \\ [])

@spec removes?(module(), atom(), struct(), map() | keyword(), keyword()) :: boolean()

Whether resource's action rule takes record out of the client's list for input, without running the action: a remove the rule reaches through its wheres. A rule of a generic action runs on every row the client shows, so asking this of each row tells which rows the client takes out.

rules(resource, action, record, input, opts \\ [])

@spec rules(module(), atom(), struct() | nil, map() | keyword(), keyword()) :: %{
  sets: %{required(String.t()) => term()},
  pushes: [%{event: String.t(), payload: map()}]
}

What resource's action rule gives for record and input, without running the action: %{sets: %{"attr" => value}, pushes: [%{event: "name", payload: map}]}. Takes the options optimistic takes; a ctx: left out reads the record's own ctx field.