Created
June 4, 2016 04:53
-
-
Save RocketPuppy/8cc2d4355f67746440684913ba8064c2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function deleteTodo(text) { | |
| return { | |
| type: DELETE_TODO, | |
| payload: { | |
| text | |
| } | |
| }; | |
| } | |
| const initialState = [] | |
| function todoReducer(state = initialState, action) { | |
| switch(action.type) { | |
| case(DELETE_TODO): | |
| return todos.filter((todo) => todo.text = action.payload.text); | |
| } | |
| } | |
| describe('deleting a todo', () => { | |
| describe('when there are no todos', () => { | |
| const state = []; | |
| it('returns the state unchanged', () => { | |
| assertEqual(todoReducer(state, deleteTodo('foo')), state) | |
| }); | |
| }); | |
| describe('when the todo exists alone', () => { | |
| const theTodo = { text: 'foo' }; | |
| const state = [theTodo]; | |
| it('removes the todo', () => { | |
| const newState = todoReducer(state, deleteTodo('foo')); | |
| assert(!newState.includes(theTodo)); | |
| }); | |
| }); | |
| describe('when the todo exists with other todos', () => { | |
| const firstTodo = { text: 'foo' }; | |
| const secondTodo = { text: 'bar' }; | |
| const state = [firstTodo, secondTodo]; | |
| it('removes the target todo', () => { | |
| const newState = todoReducer(state, deleteTodo('foo')); | |
| assert(!newState.includes(firstTodo)); | |
| }); | |
| it('leaves the other todos', () => { | |
| const newState = todoReducer(state, deleteTodo('foo')); | |
| assert(newState.includes(secondTodo)); | |
| }); | |
| }); | |
| }); | |
| deleteTodoActionGenerator = jsv.record({ | |
| type: jsv.constant(DELETE_TODO), | |
| payload: jsv.string | |
| }); | |
| todoGenerator = jsv.record({ | |
| text: jsv.string | |
| }); | |
| initialStateGenerator = jsv.array(todoGenerator); | |
| initialStateWithTodo = jsv.pair(deleteTodoGenerator, initialStateGenerator).smap( | |
| (todo, initialState) => [todo, initialState.concat(todo)], | |
| (todo, initialState) => [todo, initialState.slice(0, -1)] | |
| ); | |
| initialStateWithoutTodo = jsv.pair(deleteTodoGenerator, initialStateGenerator).smap( | |
| (todo, initialState) => [todo, initialState.filter((t) => t.text === todo.text)], | |
| (todo, initialState) => [todo, initialState.concat(todo)] | |
| ); | |
| describe('deleting a todo', () => { | |
| describe('when deleting a todo that exists', () => { | |
| jsc.property('it removes the todo', | |
| initialStateWithTodo, | |
| ([todo, initialState]) => { | |
| const newState = todoReducer(initialState, deleteTodo(todo.text)); | |
| assert(!newState.include(todo)); | |
| } | |
| ); | |
| jsc.property('it only removes a single todo', () => { | |
| initialStateWithTodo, | |
| ([todo, initialState]) => { | |
| const newState = todoReducer(initialState, deleteTodo(todo.text)); | |
| assert(newState.length === initialState.length - 1); | |
| } | |
| }); | |
| }); | |
| describe('when deleting a todo that does not exist', () => { | |
| jsc.property('it does not modify the state', | |
| initialStateWithoutTodo, | |
| ([todo, initialState]) => { | |
| const newState = todoReducer(initialState, deleteTodo(todo.text)); | |
| assert(_.equals(initialState, newState)); | |
| } | |
| ); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment