Skip to content

Instantly share code, notes, and snippets.

@RocketPuppy
Created June 4, 2016 04:53
Show Gist options
  • Save RocketPuppy/8cc2d4355f67746440684913ba8064c2 to your computer and use it in GitHub Desktop.
Save RocketPuppy/8cc2d4355f67746440684913ba8064c2 to your computer and use it in GitHub Desktop.
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