Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TapaniAla/6cf5db294108b0e1165dec6af2e85ee1 to your computer and use it in GitHub Desktop.
Save TapaniAla/6cf5db294108b0e1165dec6af2e85ee1 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://wzrd.in/standalone/expect@latest"></script>
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script>
</head>
<body>
</body>
</html>
const todos = (state=[], action) => {
switch (action.type) {
case 'ADD_TODO':
return [
...state,
{id: action.id,
text: action.text,
completed: false
}
];
case 'TOGGLE_TODO':
return state.map(todo => {
if(todo.id !== action.id) {
return todo;
}
return {
...todo,
completed: !todo.completed,
};
});
default:
return state;
}
};
const testAddTodo = () => {
const stateBefore = [];
const action = {
type: 'ADD_TODO',
id: 0,
text: 'Learn Redux'
};
const stateAfter = [
{
id: 0,
text: 'Learn Redux',
completed: false
}
];
deepFreeze(stateBefore);
deepFreeze(action);
expect(
todos(stateBefore, action)
).toEqual(stateAfter);
};
const testToggleTodo = () => {
const stateBefore = [
{
id: 0,
text: 'Learn Redux',
completed: false
},
{
id: 1,
text: 'Go Shopping',
completed: false
}
];
const action = {
type: 'TOGGLE_TODO',
id: 1,
};
const stateAfter = [
{
id: 0,
text: 'Learn Redux',
completed: false
},
{
id: 1,
text: 'Go Shopping',
completed: true
}
];
deepFreeze(stateBefore);
deepFreeze(action);
expect(
todos(stateBefore, action)
).toEqual(stateAfter);
};
//testAddTodo();
testToggleTodo();
console.log('Tests passed');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment