Skip to content

Instantly share code, notes, and snippets.

@sebabouche
Created February 22, 2017 21:54
Show Gist options
  • Save sebabouche/8d89291c71a5401d4cddca0591bb7656 to your computer and use it in GitHub Desktop.
Save sebabouche/8d89291c71a5401d4cddca0591bb7656 to your computer and use it in GitHub Desktop.
JS Bin Writing a Todo List Reducer // source http://jsbin.com/rikozel
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Writing a Todo List Reducer">
<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>
<script id="jsbin-javascript">
'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); } }
console.clear();
console.log('Running...');
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('Passed');
</script>
<script id="jsbin-source-javascript" type="text/javascript">console.clear()
console.log('Running...')
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('Passed')</script></body>
</html>
'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); } }
console.clear();
console.log('Running...');
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('Passed');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment