Skip to content

Instantly share code, notes, and snippets.

@josser
Last active March 21, 2016 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josser/c705c9aa744370be8189 to your computer and use it in GitHub Desktop.
Save josser/c705c9aa744370be8189 to your computer and use it in GitHub Desktop.
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