reducer의 정상·경계·알 수 없는 action을 자동 검증합니다.
UI에서 분리한 순수 reducer는 DOM 없이 입력과 결과를 검증할 수 있습니다. 실제 프로젝트에서는 Vitest나 Jest에서 같은 사례를 실행합니다.
구현 호출 횟수보다 사용자가 기대하는 상태 전이를 검사합니다.
초기값, 빈 목록, 중복과 허용되지 않은 action을 포함합니다.
정상 사례 하나만 테스트하면 경계 규칙 회귀를 놓칩니다.
로컬 프로젝트에서 확인해보세요remove action 테스트와 원본 불변성 검사를 추가하세요.
function reducer(state, action) {
if (action.type === "add") return [...state, action.value];
if (action.type === "clear") return [];
throw new Error("unknown action");
}
const tests = [
() => reducer([], { type: "add", value: "React" }).length === 1,
() => reducer(["A"], { type: "clear" }).length === 0,
];
export default function App() {
const results = tests.map((test, index) => ({
name: "case " + (index + 1),
passed: test(),
}));
return (
<main>
<h1>Reducer 테스트</h1>
<ul>
{results.map((x) => (
<li key={x.name}>
{x.passed ? "PASS" : "FAIL"} {x.name}
</li>
))}
</ul>
</main>
);
}