Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JoeNoPhoto/932520febcdc349e125f674de084ac24 to your computer and use it in GitHub Desktop.
Save JoeNoPhoto/932520febcdc349e125f674de084ac24 to your computer and use it in GitHub Desktop.
use Object.assign() and the spread operator proposed for ES7 to avoid mutating objects.
const toggleTodo = (todo) => {
return {
...todo, // ES7
completed: !todo.completed,
};
/* ES6
return Object.assign({}, todo, {
completed: !todo.completed,
});
*/
};
const testToggleTodo = () => {
const todoBefore = {
id: 0,
test: 'Learn Redux',
completed: false
};
const todoAfter = {
id: 0,
test: 'Learn Redux',
completed: true
}
deepFreeze(todoBefore);
expect(
toggleTodo(todoBefore)
).toEqual(todoAfter);
};
testToggleTodo();
console.log('all tests passed');
<!--
expect for testing:
<script src="https://wzrd.in/standalone/expect@latest"></script>
deepFreeze for not allowing mutations:
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script>
-->
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment