Close the earlier lessons and build this from the brief. The starter already
has the screen and one failing test. You write deckReducer and the rest of
the tests.
The brief
The deck's state is { cards, index, right, seen }. Write deckReducer in
deckReducer.js with two actions:
| Action | Effect |
|---|---|
{ type: "card_answered", correct } | Moves to the next card, back to the first after the last. Adds 1 to seen, and to right when correct is true. |
{ type: "deck_restarted" } | Sets index, right and seen back to 0. Keeps the cards. |
Any other action throws Unknown action: <type>.
Then add tests to deckReducer.test.js for:
- a wrong answer;
- wrapping round after the last card;
- restarting;
- the reducer leaving the deck it was given unchanged;
- an unknown action throwing that message.
Acceptance checks:
- The page shows All 6 tests pass.
- Breaking any one rule in your reducer makes at least one test fail.
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Hint 1: one new object
Each action returns { ...deck, ... } with the fields that change. cards is
never touched.
Hint 2: testing a throw
Call the reducer inside try, store error.message in catch, then
expectEqual the message. Start the variable as "no error", so a reducer
that doesn't throw fails the test.
Show one solution
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Check yourself
- What does a reducer take, and what does it return?
- Why does
Date.now()belong in the event handler and not the reducer? - A
switchcase forgets itsreturn. What happens?
Completion is saved in this browser only. It does not measure mastery.