Skip to content

Instantly share code, notes, and snippets.

Created July 5, 2016 18:23
Show Gist options
  • Save anonymous/a3be77c0a5dc55fada22a1eb5efe3979 to your computer and use it in GitHub Desktop.
Save anonymous/a3be77c0a5dc55fada22a1eb5efe3979 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/mexenebaku
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.5.2/redux.min.js"></script>
<script src="https://wzrd.in/standalone/deep-freeze"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect/1.20.2/expect.min.js"></script>
</head>
<body>
<script id="jsbin-javascript">
/* jshint esnext: true */
"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 _Redux = Redux;
var createStore = _Redux.createStore;
var combineReducers = _Redux.combineReducers;
var _foo$bar = { foo: "bar", bar: "qux" };
var foo = _foo$bar.foo;
var bar = _foo$bar.bar;
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), [{
id: action.id,
text: action.text,
completed: false
}]);
case 'TOGGLE_TODO':
return state.map(function (t) {
return todo(t, action);
});
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;
}
};
var todoApp = combineReducers({
todos: todos,
visibilityFilter: visibilityFilter
});
var store = createStore(todos);
var store2 = createStore(todoApp);
console.log('statebefore', store.getState());
store.dispatch({
type: 'ADD_TODO',
id: 0,
text: 'Learn Redux'
});
console.log('state', store.getState());
console.log('state2', store2.getState());
//----------------
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">/* jshint esnext: true */
const { createStore, combineReducers } = Redux;
var { foo, bar } = { foo: "bar" , bar : "qux"};
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,
{
id: action.id,
text: action.text,
completed: false
}
];
case 'TOGGLE_TODO':
return state.map(t => todo(t, action));
default:
return state;
}
}
const visibilityFilter = (state = 'SHOW_ALL', action) => {
switch (action.type) {
case 'SET_VISIBILITY_FILTER':
return action.filter;
default:
return state;
}
}
const todoApp = combineReducers({
todos,
visibilityFilter
});
const store = createStore(todos);
const store2 = createStore(todoApp);
console.log('statebefore', store.getState());
store.dispatch({
type: 'ADD_TODO',
id:0,
text: 'Learn Redux'
});
console.log('state', store.getState());
console.log('state2', store2.getState());
//----------------
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>
/* jshint esnext: true */
"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 _Redux = Redux;
var createStore = _Redux.createStore;
var combineReducers = _Redux.combineReducers;
var _foo$bar = { foo: "bar", bar: "qux" };
var foo = _foo$bar.foo;
var bar = _foo$bar.bar;
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), [{
id: action.id,
text: action.text,
completed: false
}]);
case 'TOGGLE_TODO':
return state.map(function (t) {
return todo(t, action);
});
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;
}
};
var todoApp = combineReducers({
todos: todos,
visibilityFilter: visibilityFilter
});
var store = createStore(todos);
var store2 = createStore(todoApp);
console.log('statebefore', store.getState());
store.dispatch({
type: 'ADD_TODO',
id: 0,
text: 'Learn Redux'
});
console.log('state', store.getState());
console.log('state2', store2.getState());
//----------------
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