Skip to main content
Frontend tutorial

6.2. Your turn

Please test the rest of the list yourself. For CounterStore:

  • ignore invalid input: start from a value, type something that isn't a number, and check that the value stays.
  • reset: any value goes back to 0.

And for Store:

  • new store: one counter, sum is 0, and canAddMoreCounters is true.
  • add and remove counters: addCounter stops at four, and removeCounter makes room again.

Two matchers you haven't used yet:

expect(store.counters).toHaveLength(4);
expect(store.counters).not.toContain(counter);

.not turns any matcher around. Each test creates its own store, so the tests never get in each other's way.

Need a hint?
  • new CounterStore(5) starts at 5, so you can check that setValue('abc') keeps it.
  • removeCounter takes the counter itself, for example store.counters[0].

When all six tests pass, choose Show solution and compare. There's more than one good way to write them.

Work locally

Download this step’s starting code (.zip) and extract it. Open a terminal in the folder with package.json. With Node.js 24 and pnpm installed, run the tests:

pnpm install --frozen-lockfile
pnpm test:run

Run pnpm dev to open the chapter 4 counter app.

Bring your browser edits

Choose Download my files in the ⋯ menu. It saves the lesson’s files as JSON. Stop the local server with Ctrl+C, copy the download into the project folder and run:

node import-draft.mjs ./06-02-your-turn-my-files.json
pnpm test:run

The import replaces the project’s exercise files, so commit or back up local edits first.

Loading this step’s code…
Starting the live preview…