Skip to content

Instantly share code, notes, and snippets.

Created June 14, 2016 11:29
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/c6d4773a7f9f859ca262a27a26787a12 to your computer and use it in GitHub Desktop.
Save anonymous/c6d4773a7f9f859ca262a27a26787a12 to your computer and use it in GitHub Desktop.
Simple TODO (Reducer + Tests) // source http://jsbin.com/sixuzoy
<!DOCTYPE html>
<html>
<head>
<title>Simple TODO (Reducer + Tests)</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.5.2/redux.js"></script>
<script src="https://fb.me/react-15.1.0.js"></script>
<script src="https://fb.me/react-dom-15.1.0.js"></script>
<script src="http://wzrd.in/standalone/expect@latest"></script>
<script src="http://wzrd.in/standalone/deep-freeze@latest"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="root">
<script id="jsbin-javascript">
// State is INDIVIDUAL todo item, Not an array
"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 visibilityFilter = function visibilityFilter(state, action) {
if (state === undefined) state = "SHOW_ALL";
switch (action.type) {
case "SET_VISIBILITY_FILTER":
return action.filter;
default:
return state;
}
};
// Reducer function
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;
}
};
/* const todoApp = (state = {}, action) => {
return {
todos: todos(state.todos,action),
visibilityFilter: visibilityFilter(state.visibilityFilter,action)
};
}; */
// Same as code above
var _Redux = Redux;
var combineReducers = _Redux.combineReducers;
var todoApp = combineReducers({
todos: todos,
visibilityFilter: visibilityFilter
});
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);
//console.log(todos (stateBefore, action));
};
/****** TESTS *********/
var testToggleTodo = function testToggleTodo() {
var stateBefore = [{
id: 0,
text: "Learn Redux",
completed: false
}, {
id: 1,
text: "Epic Redux",
completed: false
}];
var action = {
type: "TOGGLE_TODO",
id: 1
};
var stateAfter = [{
id: 0,
text: "Learn Redux",
completed: false
}, {
id: 1,
text: "Epic Redux",
completed: true
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todos(stateBefore, action)).toEqual(stateAfter);
};
testAddTodo();
testToggleTodo();
console.log("Test Passed");
/****** TESTS *********/
var _Redux2 = Redux;
var createStore = _Redux2.createStore;
var store = createStore(todoApp);
console.log('Initial state:');
console.log(store.getState());
console.log('--------------');
console.log('Dispatching ADD_TODO.'); // first todo
store.dispatch({
type: 'ADD_TODO',
id: 0,
text: 'Learn Redux'
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------');
console.log('Dispatching ADD_TODO.'); // second todo
store.dispatch({
type: 'ADD_TODO',
id: 1,
text: 'Go shopping'
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------');
console.log('Dispatching TOGGLE_TODO.'); // toggle first todo
store.dispatch({
type: 'TOGGLE_TODO',
id: 0
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------');
</script>
<script id="jsbin-source-javascript" type="text/javascript">
// State is INDIVIDUAL todo item, Not an array
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 visibilityFilter = (state = "SHOW_ALL", action) => {
switch (action.type){
case "SET_VISIBILITY_FILTER":
return action.filter;
default:
return state;
}
}
// Reducer function
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 todoApp = (state = {}, action) => {
return {
todos: todos(state.todos,action),
visibilityFilter: visibilityFilter(state.visibilityFilter,action)
};
}; */
// Same as code above
const {combineReducers} = Redux;
const todoApp = combineReducers({
todos,
visibilityFilter
});
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);
//console.log(todos (stateBefore, action));
};
/****** TESTS *********/
const testToggleTodo = () => {
const stateBefore = [
{
id: 0,
text: "Learn Redux",
completed:false
},
{
id: 1,
text: "Epic Redux",
completed:false
}
];
const action = {
type: "TOGGLE_TODO",
id: 1
};
const stateAfter = [
{
id: 0,
text: "Learn Redux",
completed:false
},
{
id: 1,
text: "Epic Redux",
completed:true
}
];
deepFreeze(stateBefore);
deepFreeze(action);
expect(
todos (stateBefore, action)
).toEqual(stateAfter);
};
testAddTodo();
testToggleTodo();
console.log("Test Passed");
/****** TESTS *********/
const { createStore } = Redux;
const store = createStore(todoApp);
console.log('Initial state:');
console.log(store.getState());
console.log('--------------');
console.log('Dispatching ADD_TODO.'); // first todo
store.dispatch({
type: 'ADD_TODO',
id: 0,
text: 'Learn Redux'
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------');
console.log('Dispatching ADD_TODO.'); // second todo
store.dispatch({
type: 'ADD_TODO',
id: 1,
text: 'Go shopping'
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------');
console.log('Dispatching TOGGLE_TODO.'); // toggle first todo
store.dispatch({
type: 'TOGGLE_TODO',
id: 0
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------');</script></body>
</html>
// State is INDIVIDUAL todo item, Not an array
"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 visibilityFilter = function visibilityFilter(state, action) {
if (state === undefined) state = "SHOW_ALL";
switch (action.type) {
case "SET_VISIBILITY_FILTER":
return action.filter;
default:
return state;
}
};
// Reducer function
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;
}
};
/* const todoApp = (state = {}, action) => {
return {
todos: todos(state.todos,action),
visibilityFilter: visibilityFilter(state.visibilityFilter,action)
};
}; */
// Same as code above
var _Redux = Redux;
var combineReducers = _Redux.combineReducers;
var todoApp = combineReducers({
todos: todos,
visibilityFilter: visibilityFilter
});
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);
//console.log(todos (stateBefore, action));
};
/****** TESTS *********/
var testToggleTodo = function testToggleTodo() {
var stateBefore = [{
id: 0,
text: "Learn Redux",
completed: false
}, {
id: 1,
text: "Epic Redux",
completed: false
}];
var action = {
type: "TOGGLE_TODO",
id: 1
};
var stateAfter = [{
id: 0,
text: "Learn Redux",
completed: false
}, {
id: 1,
text: "Epic Redux",
completed: true
}];
deepFreeze(stateBefore);
deepFreeze(action);
expect(todos(stateBefore, action)).toEqual(stateAfter);
};
testAddTodo();
testToggleTodo();
console.log("Test Passed");
/****** TESTS *********/
var _Redux2 = Redux;
var createStore = _Redux2.createStore;
var store = createStore(todoApp);
console.log('Initial state:');
console.log(store.getState());
console.log('--------------');
console.log('Dispatching ADD_TODO.'); // first todo
store.dispatch({
type: 'ADD_TODO',
id: 0,
text: 'Learn Redux'
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------');
console.log('Dispatching ADD_TODO.'); // second todo
store.dispatch({
type: 'ADD_TODO',
id: 1,
text: 'Go shopping'
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------');
console.log('Dispatching TOGGLE_TODO.'); // toggle first todo
store.dispatch({
type: 'TOGGLE_TODO',
id: 0
});
console.log('Current state:');
console.log(store.getState());
console.log('--------------');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment