Skip to content

Instantly share code, notes, and snippets.

@RocketPuppy
Created June 10, 2016 20:01
Show Gist options
  • Save RocketPuppy/86a790f9a002c38687a81dc1e7473c31 to your computer and use it in GitHub Desktop.
Save RocketPuppy/86a790f9a002c38687a81dc1e7473c31 to your computer and use it in GitHub Desktop.
import { todoReducer, deleteTodo } from '../src';
import { assert } from 'chai';
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), 'newState includes todo');
});
});
describe('when there are no todos', () => {
const state = [];
it('returns the state unchanged', () => {
assert.deepEqual(todoReducer(state, deleteTodo('foo')), state)
});
});
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));
});
});
import 'babel-polyfill'
export const DELETE_TODO = 'DELETE_TODO';
export function deleteTodo(text) {
return {
type: DELETE_TODO,
text: text
};
}
const initialState = [];
export function todoReducer(todos = initialState, action) {
switch(action.type) {
case(DELETE_TODO):
return todos.filter((todo) => todo.text !== action.text);
}
}
import { todoReducer, deleteTodo, DELETE_TODO } from '../src';
import { assert } from 'chai';
import jsv from 'jsverify';
import deepEqual from 'deep-equal';
const todoGenerator = jsv.record({
text: jsv.asciinestring
});
const initialStateGenerator = jsv.array(todoGenerator);
const initialStateWithTodo = jsv.pair(
todoGenerator,
initialStateGenerator
).smap(
([todo, initialState]) => [todo, initialState.concat(todo)],
([todo, initialState]) => [todo, initialState.slice(0, -1)]
);
const initialStateWithoutTodo = jsv.pair(
todoGenerator,
initialStateGenerator
).smap(
([todo, initialState]) => [todo, initialState.filter((t) => t.text === todo.text)],
([todo, initialState]) => [todo, initialState.concat(todo)]
);
describe('when deleting a todo that exists', () => {
jsv.property('it removes the todo', initialStateWithTodo,
([todo, initialState]) => {
const newState = todoReducer(initialState, deleteTodo(todo.text));
return !newState.includes(todo);
}
);
it('it only removes a single todo', () => {
jsv.assert(jsv.forall(initialStateWithTodo,
([todo, initialState]) => {
const newState = todoReducer(initialState, deleteTodo(todo.text));
return newState.length === initialState.length - 1;
}
), { tests: 1000 })}
);
});
describe('when deleting a todo that does not exist', () => {
jsv.property('it does not modify the state', initialStateWithoutTodo,
([todo, initialState]) => {
const newState = todoReducer(initialState, deleteTodo(todo.text));
return deepEqual(initialState, newState);
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment