Test a multi-step form reducer
Model a workflow as explicit phases
A finite-state model names a limited set of modes and defines permitted moves between them. Here a reservation has details, review and done phases; the draft text is additional data. Using one phase instead of three independent flags prevents contradictory screens. A transition table: details + next with nonblank name → review; review + back → details; review + confirm → done; reset from any phase → details. Other recognized events outside their permitted phase are no-ops; unknown action types throw.
import React, { useReducer } from 'react';function formReducer(state, action) { switch (action.type) { case 'nameChanged': return state.phase === 'details' ? {...state, name: action.name} : state; case 'next': return state.phase === 'details' && state.name.trim() !== '' ? {...state, phase:'review'} : state; case 'back': return state.phase === 'review' ? {...state, phase:'details'} : state; case 'confirm': return state.phase === 'review' ? {...state, phase:'done'} : state; case 'reset': return {phase:'details', name:''}; default: throw new Error('Unknown form action'); }}export default function App() { const [state, dispatch] = useReducer(formReducer, {phase:'details', name:''}); return <main><h1>Workshop registration</h1><p role="status">Step: {state.phase}</p> {state.phase === 'details' && <form onSubmit={e => {e.preventDefault(); dispatch({type:'next'});}}> <label htmlFor="reg-name">Display name</label><input id="reg-name" value={state.name} onChange={e => dispatch({type:'nameChanged', name:e.target.value})} required aria-describedby="reg-hint" /> <p id="reg-hint">Enter a nonblank display name to continue.</p><button disabled={!state.name.trim()}>Review</button></form>} {state.phase === 'review' && <section><h2>Review</h2><p>{state.name}</p> <button onClick={() => dispatch({type:'back'})}>Back</button><button onClick={() => dispatch({type:'confirm'})}>Confirm locally</button></section>} {state.phase === 'done' && <p>Local registration complete for {state.name}.</p>} <button onClick={() => dispatch({type:'reset'})}>Start over</button></main>;}Enter a fictional name such as Alex, review it, go back, and confirm after reviewing again. The name survives Back. The final message means only that local state reached done; it does not reserve anything remotely. Reset clears the name. The reducer rejects whitespace-only names even when called outside the form. A real application would require server validation and authorization. Browser focus and announcement behavior need separate testing.
Make transition checks independent of React
import React from 'react';function phaseReducer(phase, action) { if (action.type === 'next' && phase === 'details') return 'review'; if (action.type === 'back' && phase === 'review') return 'details'; if (action.type === 'reset') return 'details'; return phase;}export default function App() { const cases = [['details', 'next', 'review'], ['review','back','details'], ['details','back','details'], ['review','reset','details']]; return <main><h1>Transition table</h1><ul>{cases.map(([start,type,expected]) => <li key={start + ':' + type}>{start} + {type}: {phaseReducer(start,{type}) === expected ? 'Pass' : 'Fail'}</li>)}</ul></main>;}All four rows should say Pass. This smaller phase-only model intentionally omits input validation and treats unrecognized events as no-ops. It illustrates table-driven checks; it is not the full form reducer. For the actual form, freeze the initial input and assert the resulting phase, name, unchanged identity for rejected moves, and reset behavior. Test direct function calls first, then mount the actual UI and exercise its controls.
Derive what the screen can do
import React, { useReducer } from 'react';function advance(phase, action) { if (action.type === 'reviewed' && phase === 'details') return 'review'; if (action.type === 'reset') return 'details'; return phase;}export default function App() { const [phase, dispatch] = useReducer(advance, 'details'); const canReview = phase === 'details'; return <main><p>{phase}</p><button disabled={!canReview} onClick={() => dispatch({type:'reviewed'})}>Review</button> <button onClick={() => dispatch({type:'reset'})}>Reset</button></main>;}There is no separate canReview state to synchronize. It is calculated from the phase, and the reducer still checks the transition. Practice: test confirmation before review, whitespace-only input, Back preserving a name, repeated confirmation, and reset. Add a cancellation event with a documented allowed phase and test both allowed and rejected cases.