Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.3.1/redux.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/expect/1.14.0/expect.js"></script>
  <script src="https://wzrd.in/standalone/deep-freeze@latest"></script>
  <title>Redux</title>
</head>
<body>
</body>
</html>
 
/* jshint esnext: true */
const oneTodo = ( state = {}, action ) => {
    switch( action.type ) {
        case 'updateTodo' :
            return {
                id : action.id,
                text : action.text,
                completed : true
            };
        case 'toggleTodo' :
            if( state.id !== action.id ) {
                        return state;
                };
                return {
                    ...state,
                    completed : !state.completed
                }
        default : 
            return state;
    }
};
const todoReducer = ( state = [], action ) => {
    switch( action.type ) {
        case 'updateTodo' :
            return [
                ...state,
                oneTodo( undefined, action )
            ];
        case 'toggleTodo' :
            return (
                state.map( (one) => {
                    oneTodo( one, action )
                })
            );
        default : 
            return state;
    }
};
const testTodo = () => {
    const beforeState = [];
    const afterState = [
        {
            id : 1,
            text : 'redux',
            completed : true
        }
    ];
    const action = {
        type : 'updateTodo',
        id : 1,
        text : 'redux'
    };
    deepFreeze( beforeState );
    let newState = todoReducer( beforeState, action );
    expect( newState ).toEqual( afterState );
};
const testAddTodo = () => {
    const beforeState = [
        {
            id : 1,
            text : 'redux',
            completed : true
        },
        {
            id : 2,
            text : 'redux',
            completed : false
        }
    ];
    const action = {
        type : 'toggleTodo',
        id : 2
    };
    const afterState = [
        {
            id : 1,
            text : 'redux',
            completed : true
        },
        {
            id : 2,
            text : 'redux',
            completed : true
        }
    ];
    deepFreeze( beforeState );
    var newState = todoReducer( beforeState, action );
    expect( newState ).toEqual( afterState );
};
testTodo();
testAddTodo();
console.log( 'All tests passed!' );
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
Kay2danpro
0viewers