Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SET_STATE action, create reducer
- Loading branch information
Nicolas Goutay
committed
Mar 27, 2016
1 parent
aa101fc
commit be48d4d
Showing
5 changed files
with
119 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import {List, Map} from 'immutable'; | ||
import {createStore} from 'redux'; | ||
import {Provider} from 'react-redux'; | ||
import reducer from './reducer'; | ||
import {TodoAppContainer} from './components/TodoApp'; | ||
|
||
import TodoApp from './components/TodoApp'; | ||
|
||
const todos = List.of( | ||
Map({id: 1, text: 'React', status: 'active', editing: false}), | ||
Map({id: 2, text: 'Redux', status: 'active', editing: false}), | ||
Map({id: 3, text: 'Immutable', status: 'completed', editing: false}) | ||
); | ||
|
||
const filter = 'all'; | ||
const store = createStore(reducer); | ||
store.dispatch({ | ||
type: 'SET_STATE', | ||
state: { | ||
todos: [ | ||
{id: 1, text: 'React', status: 'active', editing: false}, | ||
{id: 2, text: 'Redux', status: 'active', editing: false}, | ||
{id: 3, text: 'Immutable', status: 'active', editing: false}, | ||
], | ||
filter: 'all' | ||
} | ||
}); | ||
|
||
require('../node_modules/todomvc-app-css/index.css'); | ||
|
||
ReactDOM.render( | ||
<TodoApp todos={todos} filter={filter} />, | ||
<Provider store={store}> | ||
<TodoAppContainer /> | ||
</Provider>, | ||
document.getElementById('app') | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {Map} from 'immutable'; | ||
|
||
function setState(state, newState) { | ||
return state.merge(newState); | ||
} | ||
|
||
export default function(state = Map(), action) { | ||
switch (action.type) { | ||
case 'SET_STATE': | ||
return setState(state, action.state); | ||
} | ||
return state; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {List, Map, fromJS} from 'immutable'; | ||
import {expect} from 'chai'; | ||
|
||
import reducer from '../src/reducer'; | ||
|
||
describe('reducer', () => { | ||
|
||
it('handles SET_STATE', () => { | ||
const initialState = Map(); | ||
const action = { | ||
type: 'SET_STATE', | ||
state: Map({ | ||
todos: List.of( | ||
Map({id: 1, text: 'React', status: 'active'}), | ||
Map({id: 2, text: 'Redux', status: 'active'}), | ||
Map({id: 3, text: 'Immutable', status: 'completed'}) | ||
) | ||
}) | ||
}; | ||
|
||
const nextState = reducer(initialState, action); | ||
|
||
expect(nextState).to.equal(fromJS({ | ||
todos: [ | ||
{id: 1, text: 'React', status: 'active'}, | ||
{id: 2, text: 'Redux', status: 'active'}, | ||
{id: 3, text: 'Immutable', status: 'completed'} | ||
] | ||
})); | ||
}); | ||
|
||
it('handles SET_STATE with plain JS payload', () => { | ||
const initialState = Map(); | ||
const action = { | ||
type: 'SET_STATE', | ||
state: { | ||
todos: [ | ||
{id: 1, text: 'React', status: 'active'}, | ||
{id: 2, text: 'Redux', status: 'active'}, | ||
{id: 3, text: 'Immutable', status: 'completed'} | ||
] | ||
} | ||
}; | ||
const nextState = reducer(initialState, action); | ||
expect(nextState).to.equal(fromJS({ | ||
todos: [ | ||
{id: 1, text: 'React', status: 'active'}, | ||
{id: 2, text: 'Redux', status: 'active'}, | ||
{id: 3, text: 'Immutable', status: 'completed'} | ||
] | ||
})); | ||
}); | ||
|
||
it('handles SET_STATE without initial state', () => { | ||
const action = { | ||
type: 'SET_STATE', | ||
state: { | ||
todos: [ | ||
{id: 1, text: 'React', status: 'active'}, | ||
{id: 2, text: 'Redux', status: 'active'}, | ||
{id: 3, text: 'Immutable', status: 'completed'} | ||
] | ||
} | ||
}; | ||
const nextState = reducer(undefined, action); | ||
expect(nextState).to.equal(fromJS({ | ||
todos: [ | ||
{id: 1, text: 'React', status: 'active'}, | ||
{id: 2, text: 'Redux', status: 'active'}, | ||
{id: 3, text: 'Immutable', status: 'completed'} | ||
] | ||
})); | ||
}); | ||
}); |