TrustLesson 23 of 23

How do you know it works?

Test a component by sending it messages, with no browser and no waiting.

Open Counter.ts. It is a counter whose reset waits three seconds before it happens. Checking that by hand means clicking and watching a clock. Counter.story.ts checks it another way, and nothing has run yet.

Predict

Read Counter.story.ts. There is no browser here and nothing is drawn. After the three send calls, what does the program log? And what must send be calling to get there?

Check your answer

count is 3. Each send calls update with the Model so far, and Story.make ran init to get the first one. It is the fold from lesson 10, with the real component's update.

Press Run and look at the times on the left.

Try it

  1. Turn the log into a check: yield* expectEqual("counts three clicks", story.model.count, 3). Change the 3 to a 4 and run, to see what a failure looks like. Then put it back.

  2. Now the reset. Add the two lines below. Before you run: is the count 3 or 0 on the second line? And how long will the program take?

story.send(CounterMessage.ClickedResetAfterDelay())
yield* expectEqual("still 3 while the delay is pending", story.model.count, 3)
  1. The check passes and the program still fails. Read the error. What did the story notice?

  2. Import DelayReset from ./Counter.ts, say how the Command ended, and check the result. Before you run: how long this time?

story.resolve(DelayReset, CounterMessage.CompletedDelayReset())
yield* expectEqual("resets when the delay completes", story.model.count, 0)
About steps 2 and 3

The count is 3, and it took no time. update returned the DelayReset Command, and a story doesn't run Commands. It holds them.

That is the error: the story ended with a Command nobody had settled. Every effect that update asks for has to be accounted for, so a test can't quietly forget one.

What you found

A three-second wait was tested in a few milliseconds, with no browser, no fake timer and nothing mocked. Lesson 15 is the reason. An effect leaves update as a value, so a test can look at the value, decide how it ended and hand back the message. In your project the same story is an ordinary test:

it.effect("resets after the delay", () =>
  Effect.gen(function*() {
    const story = yield* Story.make(Counter, {})
    story.send(CounterMessage.ClickedResetAfterDelay())
      .resolve(DelayReset, CounterMessage.CompletedDelayReset())
    assert.strictEqual(story.model.count, 0)
  }))

That is the course. Every lesson made the same move: describe what should happen, as a value, and let a runtime do it. A program, a screen, a Command, a Subscription, a Query and a test all came out of it.

The principles are on one page, and the playground has every lesson's code. For what the course leaves out (routing, server rendering, forms, the UI kit and other hosts), read the official guide.

23
Principle 23

Pure things test for free

update is a function and a Command is a value, so a test sends messages and reads the Model. No browser, no mocks, no clock.

OutputNot run yet

Not run yet

Read the code and say what it will do. Then press Run.