Created
December 2, 2019 21:30
-
-
Save raydot/501534bef0ec6a6814932befa11cdda4 to your computer and use it in GitHub Desktop.
Redux Tutorial Video 10 // source https://jsbin.com/carezix
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Video 10"> | |
<meta charset="utf-8"> | |
<title>Redux Tutorial</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"> | |
'use strict'; | |
var toggleTodo = function toggleTodo(todo) { | |
//ANTIPATTERN: Mutation | |
//todo.completed = !todo.completed; | |
//return todo; | |
// This works, but is hard to maintain | |
//return { | |
// id: todo.id, | |
// text: todo.text, | |
// completed: !todo.completed | |
//} | |
// let's use Object.assign! | |
return Object.assign({}, todo, { | |
completed: !todo.completed | |
}); | |
// can also use spread, although not part of ES6. | |
//return { | |
// ...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('Everything shipshape!'); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const toggleTodo = (todo) => { | |
//ANTIPATTERN: Mutation | |
//todo.completed = !todo.completed; | |
//return todo; | |
// This works, but is hard to maintain | |
//return { | |
// id: todo.id, | |
// text: todo.text, | |
// completed: !todo.completed | |
//} | |
// let's use Object.assign! | |
return Object.assign({}, todo, { | |
completed: !todo.completed | |
}); | |
// can also use spread, although not part of ES6. | |
//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('Everything shipshape!')</script></body> | |
</html> |
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
'use strict'; | |
var toggleTodo = function toggleTodo(todo) { | |
//ANTIPATTERN: Mutation | |
//todo.completed = !todo.completed; | |
//return todo; | |
// This works, but is hard to maintain | |
//return { | |
// id: todo.id, | |
// text: todo.text, | |
// completed: !todo.completed | |
//} | |
// let's use Object.assign! | |
return Object.assign({}, todo, { | |
completed: !todo.completed | |
}); | |
// can also use spread, although not part of ES6. | |
//return { | |
// ...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('Everything shipshape!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment