Created
August 24, 2016 20:08
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
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