Skip to content

Instantly share code, notes, and snippets.

@yashikagarg13
Created March 18, 2017 07:54
Show Gist options
  • Save yashikagarg13/e3242f4adc08a4d1f8f92742342d72e6 to your computer and use it in GitHub Desktop.
Save yashikagarg13/e3242f4adc08a4d1f8f92742342d72e6 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/keqexaj
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://npmcdn.com/expect/umd/expect.min.js"></script>
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script>
</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 todoReducer = function todoReducer(state, action) {
if (state === undefined) state = {};
switch (action.type) {
case "ADD_TODO":
return _extends({}, action.todo, { completed: false });
case "TOGGLE_TODO":
if (state.id !== action.id) {
return state;
}
return _extends({}, state, {
completed: !state.completed
});
default:
return state;
}
};
var todosReducer = function todosReducer(state, action) {
if (state === undefined) state = [];
switch (action.type) {
case "ADD_TODO":
return [].concat(_toConsumableArray(state), [todoReducer(undefined, action)]);
case "TOGGLE_TODO":
return state.map(function (todo) {
return todoReducer(todo, action);
});
default:
return state;
}
};
var testAddTodo = function testAddTodo() {
var stateBefore = [];
var action = {
todo: {
id: 1,
text: "item 1"
},
type: "ADD_TODO"
};
var stateAfter = [{
id: 1,
text: "item 1",
completed: false
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todosReducer(stateBefore, action)).toEqual(stateAfter);
};
var testToggleTodo = function testToggleTodo() {
var stateBefore = [{
id: 1,
text: "item 1",
completed: false
}, {
id: 2,
text: "item 2",
completed: false
}];
var action = {
id: 1,
type: "TOGGLE_TODO"
};
var stateAfter = [{
id: 1,
text: "item 1",
completed: true
}, {
id: 2,
text: "item 2",
completed: false
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todosReducer(stateBefore, action)).toEqual(stateAfter);
};
testAddTodo();
testToggleTodo();
console.log("Tests Passed!!");
</script>
<script id="jsbin-source-javascript" type="text/javascript">const todoReducer = (state = {}, action) => {
switch(action.type) {
case "ADD_TODO":
return {...action.todo, completed: false};
case "TOGGLE_TODO":
if (state.id !== action.id) {
return state;
}
return {
...state,
completed: !state.completed
}
default:
return state;
}
}
const todosReducer = (state = [], action) => {
switch(action.type) {
case "ADD_TODO":
return [...state, todoReducer(undefined, action)];
case "TOGGLE_TODO":
return state.map(todo => todoReducer(todo, action));
default:
return state;
}
}
const testAddTodo = () => {
const stateBefore = [];
const action = {
todo: {
id: 1,
text: "item 1",
},
type: "ADD_TODO",
};
const stateAfter = [{
id: 1,
text: "item 1",
completed: false,
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todosReducer(stateBefore, action)).toEqual(stateAfter);
}
const testToggleTodo = () => {
const stateBefore = [{
id: 1,
text: "item 1",
completed: false,
}, {
id: 2,
text: "item 2",
completed: false,
}];
const action = {
id: 1,
type: "TOGGLE_TODO",
};
const stateAfter = [{
id: 1,
text: "item 1",
completed: true,
}, {
id: 2,
text: "item 2",
completed: false,
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todosReducer(stateBefore, action)).toEqual(stateAfter);
}
testAddTodo();
testToggleTodo();
console.log("Tests Passed!!");</script></body>
</html>
"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 todoReducer = function todoReducer(state, action) {
if (state === undefined) state = {};
switch (action.type) {
case "ADD_TODO":
return _extends({}, action.todo, { completed: false });
case "TOGGLE_TODO":
if (state.id !== action.id) {
return state;
}
return _extends({}, state, {
completed: !state.completed
});
default:
return state;
}
};
var todosReducer = function todosReducer(state, action) {
if (state === undefined) state = [];
switch (action.type) {
case "ADD_TODO":
return [].concat(_toConsumableArray(state), [todoReducer(undefined, action)]);
case "TOGGLE_TODO":
return state.map(function (todo) {
return todoReducer(todo, action);
});
default:
return state;
}
};
var testAddTodo = function testAddTodo() {
var stateBefore = [];
var action = {
todo: {
id: 1,
text: "item 1"
},
type: "ADD_TODO"
};
var stateAfter = [{
id: 1,
text: "item 1",
completed: false
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todosReducer(stateBefore, action)).toEqual(stateAfter);
};
var testToggleTodo = function testToggleTodo() {
var stateBefore = [{
id: 1,
text: "item 1",
completed: false
}, {
id: 2,
text: "item 2",
completed: false
}];
var action = {
id: 1,
type: "TOGGLE_TODO"
};
var stateAfter = [{
id: 1,
text: "item 1",
completed: true
}, {
id: 2,
text: "item 2",
completed: false
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todosReducer(stateBefore, action)).toEqual(stateAfter);
};
testAddTodo();
testToggleTodo();
console.log("Tests Passed!!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment