Skip to content

Instantly share code, notes, and snippets.

@ScottWhittaker
Created July 6, 2016 14:20
Show Gist options
  • Save ScottWhittaker/13b3d3b9a20537a6842ab640bcf544a7 to your computer and use it in GitHub Desktop.
Save ScottWhittaker/13b3d3b9a20537a6842ab640bcf544a7 to your computer and use it in GitHub Desktop.
Redux: Writing a Counter Reducer with Tests
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Getting Started with Redux</title>
<script src="https://wzrd.in/standalone/expect@latest"></script>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
// Open the console tab to see that the tests pass.
// https://egghead.io/lessons/javascript-redux-writing-a-counter-reducer-with-tests
// Reducer for the counter example
const counter = (state = 0, action) => {
switch(action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}
expect(
counter(0, {type: 'INCREMENT'})
).toEqual(1);
expect(
counter(1, {type: 'INCREMENT'})
).toEqual(2);
expect(
counter(2, {type: 'DECREMENT'})
).toEqual(1);
expect(
counter(1, {type: 'DECREMENT'})
).toEqual(0);
expect(
counter(1, {type: 'UNKNOWN'})
).toEqual(1);
expect(
counter(undefined, {})
).toEqual(0);
console.log('Tests passed!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment