Created
December 2, 2019 21:47
-
-
Save raydot/28a489e940151a20a33d7b3c4e2d2da8 to your computer and use it in GitHub Desktop.
Redux Tutorial Video 11 // source https://jsbin.com/neseqom
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 11"> | |
<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"> | |
// Create a reducer to handle todos. | |
/** | |
Reminder: a reducer is a pure function you write to implement the | |
update logic of your application. It's how the next state is | |
calculated given the current state and an action. | |
*/ | |
'use strict'; | |
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
var todos = function todos(state, action) { | |
if (state === undefined) state = []; | |
switch (action.type) { | |
case 'ADD_TODO': | |
return [].concat(_toConsumableArray(state), [{ | |
id: action.id, | |
text: action.text, | |
completed: false | |
}]); | |
default: | |
return state; | |
} | |
}; | |
var testAddTodo = function testAddTodo() { | |
var stateBefore = []; | |
var action = { | |
type: 'ADD_TODO', | |
id: 0, | |
text: 'Learn Redux' | |
}; | |
var stateAfter = [{ | |
id: 0, | |
text: 'Learn Redux', | |
completed: false | |
}]; | |
deepFreeze(stateBefore); | |
deepFreeze(action); | |
expect(todos(stateBefore, action)).toEqual(stateAfter); | |
}; | |
testAddTodo(); | |
console.log("All is well!"); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// Create a reducer to handle todos. | |
/** | |
Reminder: a reducer is a pure function you write to implement the | |
update logic of your application. It's how the next state is | |
calculated given the current state and an action. | |
*/ | |
const todos = (state = [], action) => { | |
switch (action.type) { | |
case 'ADD_TODO': | |
return [ | |
...state, | |
{ | |
id: action.id, | |
text: action.text, | |
completed: false | |
} | |
] | |
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); | |
} | |
testAddTodo(); | |
console.log("All is well!")</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
// Create a reducer to handle todos. | |
/** | |
Reminder: a reducer is a pure function you write to implement the | |
update logic of your application. It's how the next state is | |
calculated given the current state and an action. | |
*/ | |
'use strict'; | |
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
var todos = function todos(state, action) { | |
if (state === undefined) state = []; | |
switch (action.type) { | |
case 'ADD_TODO': | |
return [].concat(_toConsumableArray(state), [{ | |
id: action.id, | |
text: action.text, | |
completed: false | |
}]); | |
default: | |
return state; | |
} | |
}; | |
var testAddTodo = function testAddTodo() { | |
var stateBefore = []; | |
var action = { | |
type: 'ADD_TODO', | |
id: 0, | |
text: 'Learn Redux' | |
}; | |
var stateAfter = [{ | |
id: 0, | |
text: 'Learn Redux', | |
completed: false | |
}]; | |
deepFreeze(stateBefore); | |
deepFreeze(action); | |
expect(todos(stateBefore, action)).toEqual(stateAfter); | |
}; | |
testAddTodo(); | |
console.log("All is well!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment