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 theoneOflist, a price between 10 and 40,inPlaylistat 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.seedis a number that fixes the random generator, so the same seed gives the same test data on every run.mockfakes a server for models that have one, with alatencyin 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
- In
fixtures.js, seed eight generated albums withapp.store.seed('album', 8), then swap the shop over to the store (step 2) and look at what the generator made. - In
Shop.pzl, drop theALBUMSimport and returnthis.ctx.store.findMany('album')fromdata(). - In
Album.pzl, returnthis.ctx.store.findOne('album', params.id). - Back in
fixtures.js, importALBUMSfrom./data/albums.jsand seed with it instead, so Music keeps its real names.