Skip to content

Instantly share code, notes, and snippets.

@piq9117
Last active August 25, 2016 07:18
Show Gist options
  • Save piq9117/54cab79b83a8f5afad210bfe3c42756f to your computer and use it in GitHub Desktop.
Save piq9117/54cab79b83a8f5afad210bfe3c42756f to your computer and use it in GitHub Desktop.
const person = {
name: 'ken',
favoriteFood: []
}
function addFood (state, action) {
return Object.assign({}, state, {
favorite: state.favoriteFood.concat(action.data.food)
})
}
function reducer (state = person, action) {
switch (action.type) {
case 'ADD_FOOD',
return addFood(state, action)
// if it doesnt match any action type it will
// just return the state
default:
return state
}
}
describe('reducer', () => {
it('handles ADD_FOOD', () => {
const action = {
type: 'ADD_FOOD',
data: {
food: 'apple'
}
}
const result = reducer(person, action)
expect(result).to.deep.equal({
name: 'ken',
favoriteFood: ['apple']
})
})
// when I test the person object here it will come
// out the same
it('will not have a side effect', () => {
expect(person).to.deep.equal({
name: 'ken',
favoriteFood: []
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment