Skip to content

Instantly share code, notes, and snippets.

Created May 27, 2016 16:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/377385e5723480e3c84fa2a6d41a454d to your computer and use it in GitHub Desktop.
Save anonymous/377385e5723480e3c84fa2a6d41a454d to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/legeyo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<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">
/*
* Open the console
* to see that the tests pass.
*/
'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 addTodo(state, action);
case 'TOGGLE_TODO':
return toggleTodo(state, action);
default:
return state;
}
};
var addTodo = function addTodo(state, action) {
return [].concat(_toConsumableArray(state), [{
id: action.id,
text: action.text,
completed: false
}]);
};
var toggleTodo = function toggleTodo(state, action) {
return state.map(function (todo) {
if (todo.id !== action.id) {
return todo;
}
return _extends({}, todo, {
completed: !todo.completed
});
});
};
// ------- Tests: -------
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: 'Go shopping',
completed: false
}];
var action = {
type: 'TOGGLE_TODO',
id: 1
};
var stateAfter = [{
id: 0,
text: 'Learn Redux',
completed: false
}, {
id: 1,
text: 'Go shopping',
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">/*
* Open the console
* to see that the tests pass.
*/
const todos = (state = [], action) => {
switch (action.type) {
case 'ADD_TODO':
return addTodo(state, action);
case 'TOGGLE_TODO':
return toggleTodo(state, action);
default:
return state;
}
};
const addTodo = (state, action) => {
return [
...state,
{
id: action.id,
text: action.text,
completed: false,
}
];
}
const toggleTodo = (state, action) => {
return state.map((todo) => {
if (todo.id !== action.id) {
return todo;
}
return {
...todo,
completed: !todo.completed
};
});
}
// ------- Tests: -------
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: 'Go shopping',
completed: false
}
];
const action = {
type: 'TOGGLE_TODO',
id: 1
};
const stateAfter = [
{
id: 0,
text: 'Learn Redux',
completed: false
},
{
id: 1,
text: 'Go shopping',
completed: true
}
];
deepFreeze(stateBefore);
deepFreeze(action);
expect(
todos(stateBefore, action)
).toEqual(stateAfter);
};
testAddTodo();
testToggleTodo();
console.log('All tests passed.');</script></body>
</html>
/*
* Open the console
* to see that the tests pass.
*/
'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 addTodo(state, action);
case 'TOGGLE_TODO':
return toggleTodo(state, action);
default:
return state;
}
};
var addTodo = function addTodo(state, action) {
return [].concat(_toConsumableArray(state), [{
id: action.id,
text: action.text,
completed: false
}]);
};
var toggleTodo = function toggleTodo(state, action) {
return state.map(function (todo) {
if (todo.id !== action.id) {
return todo;
}
return _extends({}, todo, {
completed: !todo.completed
});
});
};
// ------- Tests: -------
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: 'Go shopping',
completed: false
}];
var action = {
type: 'TOGGLE_TODO',
id: 1
};
var stateAfter = [{
id: 0,
text: 'Learn Redux',
completed: false
}, {
id: 1,
text: 'Go shopping',
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