From 37c6ab7f69790a047684f6ff9fe59a9c411c3fbb Mon Sep 17 00:00:00 2001 From: Mateusz Zatorski Date: Fri, 24 Jul 2015 09:12:08 +0100 Subject: [PATCH] initial test for the counter and todomvc examples --- examples/counter/package.json | 11 ++- examples/counter/test/actions.spec.js | 18 ++++ examples/counter/test/reducers.spec.js | 40 ++++++++ examples/todomvc/package.json | 11 ++- examples/todomvc/test/actions.spec.js | 52 ++++++++++ examples/todomvc/test/reducers.spec.js | 132 +++++++++++++++++++++++++ 6 files changed, 260 insertions(+), 4 deletions(-) create mode 100644 examples/counter/test/actions.spec.js create mode 100644 examples/counter/test/reducers.spec.js create mode 100644 examples/todomvc/test/actions.spec.js create mode 100644 examples/todomvc/test/reducers.spec.js diff --git a/examples/counter/package.json b/examples/counter/package.json index 293a2f72..5c611ca9 100644 --- a/examples/counter/package.json +++ b/examples/counter/package.json @@ -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", @@ -22,11 +25,15 @@ "redux-thunk": "^0.1.0" }, "devDependencies": { + "babel": "^5.5.8", "babel-core": "^5.6.18", "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" } } diff --git a/examples/counter/test/actions.spec.js b/examples/counter/test/actions.spec.js new file mode 100644 index 00000000..b67e9d75 --- /dev/null +++ b/examples/counter/test/actions.spec.js @@ -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); + }); +}); diff --git a/examples/counter/test/reducers.spec.js b/examples/counter/test/reducers.spec.js new file mode 100644 index 00000000..ae679c72 --- /dev/null +++ b/examples/counter/test/reducers.spec.js @@ -0,0 +1,40 @@ +import expect from 'expect'; +import counter from '../reducers/counter'; +import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../constants/ActionTypes'; + +describe('reducers', () => { + + 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); + }); + + }); +}); diff --git a/examples/todomvc/package.json b/examples/todomvc/package.json index de4ad8d4..2cf159e5 100644 --- a/examples/todomvc/package.json +++ b/examples/todomvc/package.json @@ -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", @@ -34,6 +37,7 @@ "redux": "^1.0.0-rc" }, "devDependencies": { + "babel": "^5.5.8", "babel-core": "^5.6.18", "babel-loader": "^5.1.4", "node-libs-browser": "^0.5.2", @@ -42,6 +46,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" } } diff --git a/examples/todomvc/test/actions.spec.js b/examples/todomvc/test/actions.spec.js new file mode 100644 index 00000000..40109d82 --- /dev/null +++ b/examples/todomvc/test/actions.spec.js @@ -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); + }); +}); diff --git a/examples/todomvc/test/reducers.spec.js b/examples/todomvc/test/reducers.spec.js new file mode 100644 index 00000000..21cb598d --- /dev/null +++ b/examples/todomvc/test/reducers.spec.js @@ -0,0 +1,132 @@ +import expect from 'expect'; +import todos from '../reducers/todos'; +import { ADD_TODO, DELETE_TODO, EDIT_TODO, MARK_TODO, MARK_ALL, CLEAR_MARKED } from '../constants/ActionTypes'; + +describe('reducers', () => { + + 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); + }); + }); +});