weft
ir 2.6.0 · warp 1.8.0
Part four · Step 16 of 19

Testing what you built

A fragment is a pure function of its values, so most of what you would mount a browser for is a call.

Goal

Test a fragment, a plan and an intent without a browser, and see why the interesting assertions are about what the compiler inferred rather than about pixels.

A sealed template rendered with a value set is a function call. That removes the reason most component tests need a DOM at all, and it leaves the assertions that are actually about this framework: what a fragment reads, what its class came out as, and what a route refuses.

1 · Render a fragment

sketch — not compiled
import { render } from '@weftjs/ir'
import { fragmentIR } from '@weftjs/core'

test('a row shows the price it was given', () => {
  const item = fragmentIR('item')
  const html = render(item.entry, { item: { sku: 'oat', name: 'Oat milk', price: 3.9 } })
  assert.match(String(html), /Oat milk/)
})

2 · Assert on the plan, not the pixels

sketch — not compiled
test('reading the currency cookie takes the page out of static', async () => {
  const plan = await planFor('/')
  assert.equal(plan.slots.body.class, 'shared')
  assert.deepEqual(plan.slots.body.reads, ['cookie:currency'])
})

That test fails the day somebody adds a read, which is exactly the day you want to be told. A snapshot of the markup would have failed too — and for a reason that did not name the cause.

3 · Test the refusals you rely on

sketch — not compiled
test('a public class on a private fragment is refused by name', async () => {
  await assert.rejects(() => build({ cache: { class: 'public' } }), /E_PRIVATE_AS_PUBLIC/)
})

A refusal you depend on is a behaviour, and a behaviour with no test is a behaviour that leaves quietly. Checking your own application has the helpers and what each one is for.