Skip to content

Instantly share code, notes, and snippets.

@codeangler
Created May 2, 2017 14:30
Show Gist options
  • Save codeangler/96e7deba35ba98326ca01c92a40e350e to your computer and use it in GitHub Desktop.
Save codeangler/96e7deba35ba98326ca01c92a40e350e to your computer and use it in GitHub Desktop.
Redux: Avoiding Object Mutation // source https://jsbin.com/huqawa
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Redux: Avoiding Object Mutation</title>
<script src="https://wzrd.in/standalone/expect@latest"></script>
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script>
</head>
<body>
<script id="jsbin-javascript">
/*
* Open the console to see
* that the tests have passed.
*/
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var toggleTodo = function toggleTodo(todo) {
return _extends({}, todo, {
completed: !todo.completed
});
};
var testToggleTodo = function testToggleTodo() {
var todoBefore = {
id: 0,
text: 'Learn Redux',
completed: false
};
var todoAfter = {
id: 0,
text: 'Learn Redux',
completed: true
};
deepFreeze(todoBefore);
expect(toggleTodo(todoBefore)).toEqual(todoAfter);
};
testToggleTodo();
console.log('All tests passed.');
</script>
<script id="jsbin-source-javascript" type="text/javascript">/*
* Open the console to see
* that the tests have passed.
*/
const toggleTodo = (todo) => {
return {
...todo,
completed: !todo.completed
};
};
const testToggleTodo = () => {
const todoBefore = {
id: 0,
text: 'Learn Redux',
completed: false
};
const todoAfter = {
id: 0,
text: 'Learn Redux',
completed: true
};
deepFreeze(todoBefore);
expect(
toggleTodo(todoBefore)
).toEqual(todoAfter);
};
testToggleTodo();
console.log('All tests passed.');</script></body>
</html>
/*
* Open the console to see
* that the tests have passed.
*/
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var toggleTodo = function toggleTodo(todo) {
return _extends({}, todo, {
completed: !todo.completed
});
};
var testToggleTodo = function testToggleTodo() {
var todoBefore = {
id: 0,
text: 'Learn Redux',
completed: false
};
var todoAfter = {
id: 0,
text: 'Learn Redux',
completed: true
};
deepFreeze(todoBefore);
expect(toggleTodo(todoBefore)).toEqual(todoAfter);
};
testToggleTodo();
console.log('All tests passed.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment