Liking cljdoc? Tell your friends :D

Testing components

Most of a component is testable without HTTP, a browser or Datastar: drive the pipeline in process and look at :memory, :state and the rendered hiccup.

Driving the pipeline

(require '[taia.ds.datastar.interface :as ds])

(ds/set-component-search-paths! ["my.app.components"])

(def req {:params      {:logged-user {:user/id "tester"} :id "3"}       ; event params = query params
          :body-params {:todos1 {:todos [...] :state "displayed"}}})    ; the signal store

(let [{:keys [ctx html]} (ds/process-component-event req "todomvc.todomvc" "todos1" "toggle" nil)]
  (:memory ctx)   ; after the transition
  (:state ctx)
  html)           ; hiccup, or an effects list

The examples base wraps exactly this in test_support.clj: run builds the request from {:memory :state :params :user}, step feeds one result into the next request the way the browser does, and testid-text / extract-post-urls parse the rendered HTML with hickory so assertions target data-testid attributes, not strings. Copy the file; it is forty lines.

Start each case from the store it is about, rather than chaining one case's result into the next; keep step for the cases where the round trip itself is the point:

(def ^:private at-one {:memory {:count 1} :state :displayed})

"an increment from 1"
(-> (run "basics.counter" "counter1" at-one "increment")
    (select-keys [:memory :state]))
:= {:memory {:count 2} :state :displayed}

"the render shows the new count"
(-> (run "basics.counter" "counter1" at-one "increment")
    :html
    (testid-text "count"))
:= "2"

examples_test.clj is the complete executable spec of the examples and the best place to see the style.

How the tests are written

The same rules hold for the library's own tests (inline RCF (tests …) blocks):

  • One function per test. A function that hands work to another one is tested with that collaborator mocked by spy.core, asserting both its return value and (spy/called-once-with? collaborator args…) := true; the collaborator has tests of its own. The flow procs are the reference: process-event.controller tests handle-event directly, and step with handle-event spied.
  • Standalone cases. Each case starts from a named value of its own (at-one, holding-running), never from the result of the case before it.
  • Threads one step per line, from the input: (-> holding-running / (handle-event {:event ["increment" {}]}) / sent / :ctx).
  • Whole values. Compare the full expected map or vector with :=; narrow with select-keys or a named helper only when the case is about one part.

The counter and live counter show this style on a component. Their controllers test each action, then the machine with test-machine (see Testing the machine alone). Their views test the render fn with ds/ctx->data-signals and inspect/state-pre spied.

Driving a connected component's flow

run/step cover a connected component's browser events too: nothing is kept between events, so each one starts from its request. For a server event (send-event!: no request, its own params), or to run events through one long-lived flow as a connection does, start the flow headless:

(def cf (ds/start-component-flow! {:component-name "live.countdown" :component-id "countdown1"}))
(ds/run-flow-event! cf req "init")                    ; a browser event: req carries signals and params
(ds/run-flow-event! cf nil "tick" {:remaining 4})     ; a server event, like send-event!: params only
(ds/stop-component-flow! cf)

test_support.clj wraps these as connect, flow-event and server-event; block 7 of examples_test.clj drives the countdown with them.

Testing the machine alone

taia.ds.state-machines.interface/transition is pure: (transition machine {:state :a :memory {}} [:GO {}] {:state-key :state}).

test-machine asserts, per state and event, where the machine lands and the ordered trace of the actions and invokes it went through. It runs neither: an action is recorded by its fn's name, an invoke by its id, answered from :invoke-returns. The actions have tests of their own.

(require '[taia.ds.state-machines.test-helpers :refer [test-machine]])
;; in a base of a Polylith workspace that has the brick: taia.ds.state-machines.interface.test-helpers

(tests
 (test-machine
  {:displayed {:INCREMENT {:target :displayed :trace [:increment]}
               :SAVE      {:target :saved     :trace [:store-form :save :save-on-done]}}}
  machine
  {:invoke-returns {:save {:id 1}}})
 := :ok)

It returns :ok, or the failing [from event] pairs with expected and actual target and trace. For a machine that calls the world from somewhere test-machine cannot reach, make-machine-testable swaps named functions for their vars so with-redefs can intercept them.

Testing the render fn alone

Call it with a hand-made ctx and parse the result with hickory. Two traps:

  • The render fn's top-level ctx keys are only real if the invoke or action before that event stamps them. A render test that passes the key by hand passes while production renders nil. Test the transition → render path together for anything beyond :memory.
  • Never gate a render on (:event ctx); the event is not in ctx.

What needs a browser

Datastar itself: data-bind writing into the store, morph behaviour, data-init firing, the SSE stream and the reconnect watchdog. Cover those with an end-to-end suite against a real page and watch the browser console: a thrown Datastar expression aborts the attribute pass for the whole patched subtree without any server-side symptom.

End-to-end (Playwright)

bb e2e-test                 # all specs; needs Node >= 18
bb e2e-test counter         # one spec file
bb e2e-test --headed

scripts/e2e.clj installs e2e/ deps, starts the examples app on port 7799 in its own JVM, runs npx playwright test with E2E_BASE_URL set, and stops the JVM (log in e2e/test-results/server.log). Against an already running bb dev: E2E_BASE_URL=http://localhost:7788 npx playwright test from e2e/.

One spec per example in e2e/tests/. Every spec installs console-guard.ts, which fails the test on any pageerror or console.error (network aborts on navigation are ignored) — the only reliable detector of an aborted attribute pass. live-counter.spec.ts drives a connected component's clicks through its flow, with two browser contexts as two users counting independently.

Conventions used in this repository

Tests are inline RCF (tests …) blocks (hyperfiddle.rcf), run by Kaocha with -Dhyperfiddle.rcf.generate-tests=true (bb test). Values are bound with def so they can be inspected in a REPL; assertions are top-level :=. Because the dev server enables RCF, a lazily-loaded component namespace runs its tests on first request — keep test blocks free of process-global side effects, or restore what they change.

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close