4. Architecture, Testing & Tooling

Practice Lab 2: Typed State Reducer, Narrowing & Unit Tests

Combine Modules 3 and 4 in a hands-on lab: implement a pure state reducer handling discriminated union actions, narrow untrusted payloads, and verify every transition with automated checks.

2 min readUpdated 2026-12-01
In This Lesson
  • Implement a pure immutable reducer handling discriminated action objects
  • Validate and narrow untrusted payload inputs before updating state
  • Verify every action branch and edge case with automated assertions

In React (useReducer), Redux, and TypeScript frontends, pure reducers ((state, action) => nextState) keep complex UI logic predictable and 100% unit-testable without needing a browser!

Your Mission

Implement boardReducer(state, action) in the playground below:

  • "ADD_TASK": validates typeof action.title === "string" and non-empty trimmed title; appends { id: action.id, title: action.title.trim(), status: "todo" } immutably.
  • "MOVE_TASK": updates only the matching task's status ("todo" | "doing" | "done") immutably.
  • "REMOVE_TASK": filters out the task with matching action.id.
  • Unknown action.type: throws new Error("Unknown action").
JavaScriptLab 2: Pure State Reducer & Action Narrowing
Output

Click “Run Code” to execute this snippet right in your browser.

Lab 2 Completion Checklist

0/3 done