Test data

An app's records usually come from a server. While you build, fixtures stand in for it: test records generated from your models' schemas, so the pages have something to show from the first minute.

Fixtures live in app/fixtures.js. Its default export can hold three things:

  • setup(app) runs before the first page renders, and is where you seed the store. app.store.seed('album', 8) makes eight albums from the schema alone, with every rule respected: a genre from the oneOf list, a price between 10 and 40, inPlaylist at its default. Hand it an array of records instead, app.store.seed('album', ALBUMS), and it keeps every field you wrote and generates only the ones you left out.
  • seed is a number that fixes the random generator, so the same seed gives the same test data on every run.
  • mock fakes a server for models that have one, with a latency in milliseconds to slow it down: handy for loading states. Music has no server yet, so it needs none.

Start the dev server with the flag: puzzle dev --fixtures. Without it the file is never imported, so test data can never reach production.

This preview runs your app the way puzzle dev --fixtures would: when app/fixtures.js exists, it is installed before the app starts.

Views read the store through this.ctx.store. findMany('album') returns every album and findOne('album', id) returns one, or null. A find inside data() also subscribes the view: when those records change, data() runs again.

Your turn

  1. In fixtures.js, seed eight generated albums with app.store.seed('album', 8), then swap the shop over to the store (step 2) and look at what the generator made.
  2. In Shop.pzl, drop the ALBUMS import and return this.ctx.store.findMany('album') from data().
  3. In Album.pzl, return this.ctx.store.findOne('album', params.id).
  4. Back in fixtures.js, import ALBUMS from ./data/albums.js and seed with it instead, so Music keeps its real names.

Chapters