Skip to content

initial tests for counter and todomvc examples #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions examples/counter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"description": "Counter example for redux",
"main": "server.js",
"scripts": {
"start": "node server.js"
"start": "node server.js",
"test": "NODE_ENV=test mocha --compilers js:babel/register --recursive",
"test:watch": "NODE_ENV=test mocha --compilers js:babel/register --recursive --watch",
"test:cov": "babel-node ./node_modules/.bin/isparta cover ./node_modules/.bin/_mocha -- --recursive"
},
"repository": {
"type": "git",
Expand All @@ -31,11 +34,15 @@
"redux": "^0.12.0"
},
"devDependencies": {
"babel": "^5.5.8",
"babel-core": "^5.5.8",
"babel-loader": "^5.1.4",
"node-libs-browser": "^0.5.2",
"react-hot-loader": "^1.2.7",
"webpack": "^1.9.11",
"webpack-dev-server": "^1.9.0"
"webpack-dev-server": "^1.9.0",
"expect": "^1.6.0",
"isparta": "^3.0.3",
"mocha": "^2.2.5"
}
}
18 changes: 18 additions & 0 deletions examples/counter/test/actions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import expect from 'expect';
import { increment, decrement } from '../actions/CounterActions';
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../constants/ActionTypes';


describe('actions', () => {
it('returns increment counter action type', () => {
const { type } = increment();

expect(type).toEqual(INCREMENT_COUNTER);
});

it('returns decrement counter action type', () => {
const { type } = decrement();

expect(type).toEqual(DECREMENT_COUNTER);
});
});
40 changes: 40 additions & 0 deletions examples/counter/test/stores.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import expect from 'expect';
import counter from '../stores/counter';
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../constants/ActionTypes';

describe('stores', () => {

describe('counter', () => {

it('increments the counter', () => {
const action = {
type: INCREMENT_COUNTER
};

const state = counter(0, action);

expect(state).toEqual(1);
});

it('decrements the counter', () => {
const action = {
type: DECREMENT_COUNTER
};

const state = counter(0, action);

expect(state).toEqual(-1);
});

it('returns the state by default', () => {
const action = {
type: null
};

const state = counter(2, action);

expect(state).toEqual(2);
});

});
});
11 changes: 9 additions & 2 deletions examples/todomvc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"description": "TodoMVC example for redux",
"main": "server.js",
"scripts": {
"start": "node server.js"
"start": "node server.js",
"test": "NODE_ENV=test mocha --compilers js:babel/register --recursive",
"test:watch": "NODE_ENV=test mocha --compilers js:babel/register --recursive --watch",
"test:cov": "babel-node ./node_modules/.bin/isparta cover ./node_modules/.bin/_mocha -- --recursive"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -33,6 +36,7 @@
"redux": "^0.12.0"
},
"devDependencies": {
"babel": "^5.5.8",
"babel-core": "^5.5.8",
"babel-loader": "^5.1.4",
"node-libs-browser": "^0.5.2",
Expand All @@ -41,6 +45,9 @@
"style-loader": "^0.12.3",
"todomvc-app-css": "^2.0.1",
"webpack": "^1.9.11",
"webpack-dev-server": "^1.9.0"
"webpack-dev-server": "^1.9.0",
"expect": "^1.6.0",
"isparta": "^3.0.3",
"mocha": "^2.2.5"
}
}
52 changes: 52 additions & 0 deletions examples/todomvc/test/actions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import expect from 'expect';
import { addTodo, deleteTodo, editTodo, markTodo, markAll, clearMarked } from '../actions/TodoActions';
import * as types from '../constants/ActionTypes';


describe('actions', () => {
it('returns add todo action type and text', () => {
const todoText = 'My first TODO';
const { type, text } = addTodo(todoText);

expect(type).toEqual(types.ADD_TODO);
expect(text).toEqual(todoText);
});

it('returns delete todo action type and id of deleted todo', () => {
const todoId = 1;
const { type, id } = deleteTodo(todoId);

expect(type).toEqual(types.DELETE_TODO);
expect(id).toEqual(todoId);
});

it('returns edit todo action type, its id and new text', () => {
const todoId = 1;
const editedTodo = 'My edited TODO';
const { type, id, text } = editTodo(todoId, editedTodo);

expect(type).toEqual(types.EDIT_TODO);
expect(id).toEqual(todoId);
expect(text).toEqual(editedTodo);
});

it('returns mark todo action type and the id', () => {
const todoId = 1;
const { type, id } = markTodo(todoId);

expect(type).toEqual(types.MARK_TODO);
expect(id).toEqual(todoId);
});

it('returns mark all action type', () => {
const { type } = markAll();

expect(type).toEqual(types.MARK_ALL);
});

it('returns clear marked action type', () => {
const { type } = clearMarked();

expect(type).toEqual(types.CLEAR_MARKED);
});
});
132 changes: 132 additions & 0 deletions examples/todomvc/test/stores.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import expect from 'expect';
import todos from '../stores/todos';
import { ADD_TODO, DELETE_TODO, EDIT_TODO, MARK_TODO, MARK_ALL, CLEAR_MARKED } from '../constants/ActionTypes';

describe('stores', () => {

describe('todos', () => {

let initialState;

beforeEach(() => {
initialState = [{
text: 'Use Redux',
marked: false,
id: 0
}];
});

it('adds a new todo', () => {
const todoText = 'My TODO';
const action = {
type: ADD_TODO,
text: todoText
};

const state = todos(initialState, action);

expect(state.length).toEqual(2);
expect(state[0].text).toEqual(todoText);
expect(state[0].marked).toEqual(false);
expect(state[0].id).toEqual(state[1].id + 1);
});

it('deletes todo', () => {
const todoId = 0;
const action = {
type: DELETE_TODO,
id: todoId
};

const state = todos(initialState, action);

expect(state.length).toEqual(0);
});

it('edits todo', () => {
const todoId = 0;
const todoText = 'My TODO';
const action = {
type: EDIT_TODO,
id: todoId,
text: todoText
};

const state = todos(initialState, action);

expect(state.length).toEqual(1);
expect(state[0].text).toEqual(todoText);
expect(state[0].marked).toEqual(false);
});

it('marks todo', () => {
const todoId = 0;
const action = {
type: MARK_TODO,
id: todoId
};

const state = todos(initialState, action);

expect(state.length).toEqual(1);
expect(state[0].marked).toEqual(true);
});

it('marks all todos', () => {
const newState = [{
text: 'Use Redux',
marked: false,
id: 1
}, {
text: 'Write tests',
marked: false,
id: 0
}];

const action = {
type: MARK_ALL
};

const state = todos(newState, action);

expect(state.length).toEqual(2);
expect(state[0].marked).toEqual(true);
expect(state[1].marked).toEqual(true);
});

it('clears all marked todos', () => {
const newState = [{
text: 'Use Redux-DevTools',
marked: true,
id: 2
}, {
text: 'Use Redux',
marked: true,
id: 1
}, {
text: 'Write tests',
marked: false,
id: 0
}];

const action = {
type: CLEAR_MARKED
};

const state = todos(newState, action);

expect(state.length).toEqual(1);
expect(state[0].marked).toEqual(false);
});

it('returns the state by default', () => {
const action = {
type: null
};

const state = todos(initialState, action);

expect(state).toEqual(initialState);
});
});
});