Skip to content

Instantly share code, notes, and snippets.

@josser

josser/reducers_todos.js

Last active Mar 21, 2016
Embed
What would you like to do?
import { ADD_TODO } from 'constants/ActionTypes'
export const initialState = [
{
text: 'Use Redux',
completed: false,
id: 0
}
]
export default function todos(state = initialState, action) {
switch (action.type) {
case ADD_TODO:
return [
{
id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,
completed: false,
text: action.text
},
...state
]
default:
return state
}
}
import expect from 'expect'
import reducer from 'reducers_todos'
import { initialState as stub } from 'reducers_todos'
import * as types from 'constants/ActionTypes'
var expectedState = { ...stub }
expectedState.push({
text: 'Run the tests',
completed: false,
id: 0
})
it('should handle ADD_TODO', () => {
expect(
reducer(initialState, {
type: types.ADD_TODO,
text: 'Run the tests'
})
).toEqual(expectedState)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment