Skip to content

Instantly share code, notes, and snippets.

@raydot
Created December 2, 2019 22:03
Show Gist options
  • Save raydot/ef1ae61e0c5a3cad91df931f41ce36d3 to your computer and use it in GitHub Desktop.
Save raydot/ef1ae61e0c5a3cad91df931f41ce36d3 to your computer and use it in GitHub Desktop.
Redux Tutorial Video 12 // source https://jsbin.com/neseqom
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Video 12">
<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';
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; };
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
}]);
case 'TOGGLE_TODO':
return state.map(function (todo) {
if (todo.id !== action.id) {
return todo;
}
return _extends({}, todo, {
completed: !todo.completed
});
});
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);
};
var testToggleTodo = function testToggleTodo() {
var stateBefore = [{
id: 0,
text: 'Walk the dog',
completed: false
}, {
id: 1,
text: 'Feed the cat',
completed: false
}];
var action = {
type: 'TOGGLE_TODO',
id: 1
};
var stateAfter = [{
id: 0,
text: 'Walk the dog',
completed: false
}, {
id: 1,
text: 'Feed the cat',
completed: true
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todos(stateBefore, action)).toEqual(stateAfter);
};
testAddTodo();
testToggleTodo();
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
}
]
case 'TOGGLE_TODO':
return state.map(todo => {
if (todo.id !== action.id) {
return todo
}
return {
...todo,
completed: !todo.completed
}
})
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);
}
const testToggleTodo = () => {
const stateBefore = [
{
id: 0,
text: 'Walk the dog',
completed: false
},
{
id: 1,
text: 'Feed the cat',
completed: false
}
]
const action= {
type: 'TOGGLE_TODO',
id: 1
};
const stateAfter = [
{
id: 0,
text: 'Walk the dog',
completed: false
},
{
id: 1,
text: 'Feed the cat',
completed: true
}
]
deepFreeze(stateBefore)
deepFreeze(action)
expect(
todos(stateBefore, action)
).toEqual(stateAfter)
}
testAddTodo();
testToggleTodo();
console.log("All is well!")</script></body>
</html>
// 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';
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; };
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
}]);
case 'TOGGLE_TODO':
return state.map(function (todo) {
if (todo.id !== action.id) {
return todo;
}
return _extends({}, todo, {
completed: !todo.completed
});
});
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);
};
var testToggleTodo = function testToggleTodo() {
var stateBefore = [{
id: 0,
text: 'Walk the dog',
completed: false
}, {
id: 1,
text: 'Feed the cat',
completed: false
}];
var action = {
type: 'TOGGLE_TODO',
id: 1
};
var stateAfter = [{
id: 0,
text: 'Walk the dog',
completed: false
}, {
id: 1,
text: 'Feed the cat',
completed: true
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todos(stateBefore, action)).toEqual(stateAfter);
};
testAddTodo();
testToggleTodo();
console.log("All is well!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment