-
-
Save getnamo/96d332f0bdfa95b9cfe7ad72ee97a370 to your computer and use it in GitHub Desktop.
Partay // source https://jsbin.com/jirahi
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> | |
<script src="https://wzrd.in/standalone/expect@latest"></script> | |
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script> | |
<script src="https://fb.me/react-15.1.0.js"></script> | |
<script src="https://fb.me/react-dom-15.1.0.js"></script> | |
<meta charset="utf-8"> | |
<title>Partay</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
'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 todo = function todo(state, action) { | |
switch (action.type) { | |
case 'ADD_TODO': | |
return { | |
id: action.id, | |
text: action.text, | |
completed: false | |
}; | |
case 'TOGGLE_TODO': | |
if (state.id !== action.id) { | |
return state; | |
} | |
return _extends({}, state, { | |
completed: !state.completed | |
}); | |
default: | |
return state; | |
} | |
}; | |
var todos = function todos(state, action) { | |
if (state === undefined) state = []; | |
switch (action.type) { | |
case 'ADD_TODO': | |
return [].concat(_toConsumableArray(state), [todo(undefined, action)]); | |
case 'TOGGLE_TODO': | |
return state.map(function (t) { | |
return todo(t, action); | |
}); | |
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: 'Learn Redux', | |
completed: false | |
}, { | |
id: 1, | |
text: 'Learn moar', | |
completed: false | |
}]; | |
var action = { | |
type: 'TOGGLE_TODO', | |
id: 1 | |
}; | |
var stateAfter = [{ | |
id: 0, | |
text: 'Learn Redux', | |
completed: false | |
}, { | |
id: 1, | |
text: 'Learn moar', | |
completed: true | |
}]; | |
deepFreeze(stateBefore); | |
deepFreeze(action); | |
expect(todos(stateBefore, action)).toEqual(stateAfter); | |
}; | |
testAddTodo(); | |
testToggleTodo(); | |
console.log('All tests passed!'); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const todo = (state, action) => { | |
switch (action.type){ | |
case 'ADD_TODO': | |
return { | |
id: action.id, | |
text: action.text, | |
completed: false | |
}; | |
case 'TOGGLE_TODO': | |
if(state.id !== action.id){ | |
return state; | |
} | |
return { | |
...state, | |
completed:!state.completed | |
} | |
default: | |
return state; | |
} | |
} | |
const todos = (state = [], action) => { | |
switch(action.type){ | |
case 'ADD_TODO': | |
return [ | |
...state, | |
todo(undefined, action) | |
]; | |
case 'TOGGLE_TODO': | |
return state.map(t => todo(t, action)); | |
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:'Learn Redux', | |
completed:false | |
}, | |
{ | |
id:1, | |
text:'Learn moar', | |
completed:false | |
} | |
]; | |
const action = { | |
type: 'TOGGLE_TODO', | |
id:1 | |
}; | |
const stateAfter = [ | |
{ | |
id:0, | |
text:'Learn Redux', | |
completed:false | |
}, | |
{ | |
id:1, | |
text:'Learn moar', | |
completed:true | |
} | |
]; | |
deepFreeze(stateBefore); | |
deepFreeze(action); | |
expect( | |
todos(stateBefore, action) | |
).toEqual(stateAfter); | |
}; | |
testAddTodo(); | |
testToggleTodo(); | |
console.log('All tests passed!');</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 _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 todo = function todo(state, action) { | |
switch (action.type) { | |
case 'ADD_TODO': | |
return { | |
id: action.id, | |
text: action.text, | |
completed: false | |
}; | |
case 'TOGGLE_TODO': | |
if (state.id !== action.id) { | |
return state; | |
} | |
return _extends({}, state, { | |
completed: !state.completed | |
}); | |
default: | |
return state; | |
} | |
}; | |
var todos = function todos(state, action) { | |
if (state === undefined) state = []; | |
switch (action.type) { | |
case 'ADD_TODO': | |
return [].concat(_toConsumableArray(state), [todo(undefined, action)]); | |
case 'TOGGLE_TODO': | |
return state.map(function (t) { | |
return todo(t, action); | |
}); | |
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: 'Learn Redux', | |
completed: false | |
}, { | |
id: 1, | |
text: 'Learn moar', | |
completed: false | |
}]; | |
var action = { | |
type: 'TOGGLE_TODO', | |
id: 1 | |
}; | |
var stateAfter = [{ | |
id: 0, | |
text: 'Learn Redux', | |
completed: false | |
}, { | |
id: 1, | |
text: 'Learn moar', | |
completed: true | |
}]; | |
deepFreeze(stateBefore); | |
deepFreeze(action); | |
expect(todos(stateBefore, action)).toEqual(stateAfter); | |
}; | |
testAddTodo(); | |
testToggleTodo(); | |
console.log('All tests passed!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lesson 13 https://egghead.io/lessons/javascript-redux-reducer-composition-with-arrays