Skip to content

Commit a0c4104

Browse files
committed
Complete action and reducer test
1 parent f15a508 commit a0c4104

File tree

7 files changed

+181
-7
lines changed

7 files changed

+181
-7
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
jest.dontMock('../../actions/todo');
2+
3+
const Todo = require('../../actions/todo');
4+
5+
describe('todo actions', () => {
6+
7+
it('addTodo should create a ADD_TODO action', () => {
8+
expect(Todo.addTodo('This is a new todo')).toEqual({
9+
type: Todo.Action.ADD_TODO,
10+
payload: { text: 'This is a new todo' }
11+
})
12+
})
13+
14+
it('editTodo should creat a EDIT_TODO action', () => {
15+
expect(Todo.editTodo(1, 'This is a edited todo')).toEqual({
16+
type: Todo.Action.EDIT_TODO,
17+
payload: { todoID: 1, text: 'This is a edited todo' }
18+
})
19+
})
20+
21+
it('toggleTodo should creat a TOGGLE_TODO action', () => {
22+
expect(Todo.toggleTodo(1)).toEqual({
23+
type: Todo.Action.TOGGLE_TODO,
24+
payload: { todoID: 1 }
25+
})
26+
})
27+
28+
it('deleteTodo should creat a DELETE_TODO action', () => {
29+
expect(Todo.deleteTodo(1)).toEqual({
30+
type: Todo.Action.DELETE_TODO,
31+
payload: { todoID: 1 }
32+
})
33+
})
34+
35+
it('deleteCompletedTodos should creat a DELETE_COMPELETED_TODOS action', () => {
36+
expect(Todo.deleteCompletedTodos()).toEqual({
37+
type: Todo.Action.DELETE_COMPELETED_TODOS
38+
})
39+
})
40+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
jest.dontMock('../../actions/visibility-filter');
2+
3+
const Filter = require('../../actions/visibility-filter');
4+
5+
describe('visibility filter actions', () => {
6+
7+
it('setVisibilityFilter should create a SET_VISIBILITY_FILTER action', () => {
8+
expect(Filter.setVisibilityFilter(Filter.VisibilityFilter.SHOW_ALL_TODOS)).toEqual({
9+
type: Filter.Action.SET_VISIBILITY_FILTER,
10+
payload: { filter: Filter.VisibilityFilter.SHOW_ALL_TODOS }
11+
})
12+
})
13+
})
Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,91 @@
1-
/**
2-
* Testing for the todo reducer
3-
*/
1+
jest.dontMock('../../actions/todo');
2+
jest.dontMock('../../reducers/todo');
3+
4+
const _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
5+
const TodoAction = require('../../actions/todo');
6+
const todo = _interopRequire(require('../../reducers/todo'));
7+
8+
describe('todo reducers', () => {
9+
10+
it('should handle initial state', () => {
11+
12+
expect(todo(undefined, {})).toEqual([]);
13+
})
14+
15+
it('should handle ADD_TODO', () => {
16+
17+
const prevState = [
18+
{ id: 1, text: 'todo 1', completed: false }
19+
]
20+
21+
const newState = [
22+
{ id: 1, text: 'todo 1', completed: false },
23+
{ id: 2, text: 'todo 2', completed: false }
24+
]
25+
26+
expect(todo(prevState, TodoAction.addTodo('todo 2'))).toEqual(newState);
27+
})
28+
29+
it('should handle EDIT_TODO', () => {
30+
31+
const prevState = [
32+
{ id: 1, text: 'todo 1', completed: false },
33+
{ id: 2, text: 'todo 2', completed: false }
34+
]
35+
36+
const newState = [
37+
{ id: 1, text: 'todo 1', completed: false },
38+
{ id: 2, text: 'todo edited', completed: false }
39+
]
40+
41+
expect(todo(prevState, TodoAction.editTodo(2, 'todo edited'))).toEqual(newState);
42+
})
43+
44+
it('should handle TOGGLE_TODO', () => {
45+
46+
const prevState = [
47+
{ id: 1, text: 'todo 1', completed: false },
48+
{ id: 2, text: 'todo 2', completed: false }
49+
]
50+
51+
const newState = [
52+
{ id: 1, text: 'todo 1', completed: false },
53+
{ id: 2, text: 'todo 2', completed: true }
54+
]
55+
56+
expect(todo(prevState, TodoAction.toggleTodo(2))).toEqual(newState);
57+
})
58+
59+
it('should handle DELETE_TODO', () => {
60+
61+
const prevState = [
62+
{ id: 1, text: 'todo 1', completed: false },
63+
{ id: 2, text: 'todo 2', completed: false }
64+
]
65+
66+
const newState = [
67+
{ id: 1, text: 'todo 1', completed: false }
68+
]
69+
70+
expect(todo(prevState, TodoAction.deleteTodo(2))).toEqual(newState);
71+
})
72+
73+
it('should handle DELETE_COMPELETED_TODOS', () => {
74+
75+
const prevState = [
76+
{ id: 1, text: 'todo 1', completed: false },
77+
{ id: 2, text: 'todo 2', completed: true },
78+
{ id: 3, text: 'todo 3', completed: false },
79+
{ id: 4, text: 'todo 4', completed: true }
80+
]
81+
82+
const newState = [
83+
{ id: 1, text: 'todo 1', completed: false },
84+
{ id: 3, text: 'todo 3', completed: false }
85+
]
86+
87+
expect(todo(prevState, TodoAction.deleteCompletedTodos())).toEqual(newState);
88+
})
89+
})
90+
91+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
jest.dontMock('../../actions/visibility-filter');
2+
jest.dontMock('../../reducers/visibility-filter');
3+
4+
const _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
5+
const Filter = require('../../actions/visibility-filter');
6+
const visibilityFilter = _interopRequire(require('../../reducers/visibility-filter'));
7+
8+
describe('visibility filter reducers', () => {
9+
10+
it('should handle initial state', () => {
11+
expect(visibilityFilter(undefined, {})).toEqual(Filter.VisibilityFilter.SHOW_ALL_TODOS);
12+
})
13+
14+
it('should set handle SET_VISIBILITY_FILTER', () => {
15+
16+
const prevState = Filter.VisibilityFilter.SHOW_ALL_TODOS;
17+
const newState = Filter.VisibilityFilter.SHOW_COMPLETED_TODOS;
18+
19+
expect(visibilityFilter(prevState, Filter.setVisibilityFilter(Filter.VisibilityFilter.SHOW_COMPLETED_TODOS))).toEqual(newState);
20+
})
21+
})
22+
23+

todomvc/app/actions/todo.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export const Action = {
77
ADD_TODO: 'ADD_TODO',
88
EDIT_TODO: 'EDIT_TODO',
99
TOGGLE_TODO: 'TOGGLE_TODO',
10-
COMPLETE_ALL_TODOS: 'COMPLETE_ALL_TODOS',
1110
DELETE_TODO: 'DELETE_TODO',
1211
DELETE_COMPELETED_TODOS: 'DELETE_COMPELETED_TODOS'
1312
}

todomvc/app/reducers/todo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Specify how todo list's state changes responsed to actions.
33
*/
44

5-
import * as Todo from '../actions/todo'
5+
import * as Todo from '../actions/todo';
66

77
/**
88
* Reducer

todomvc/package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"description": "A practice for building react app using redux design pattern",
55
"main": "server.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1",
7+
"test": "BABEL_JEST_STAGE=0 jest",
88
"build": "webpack --progress --colors --watch",
99
"dev": "webpack-dev-server --devtool eval --progress --colors --content-base build",
10-
"deploy": "NODE_ENV=production webpack -p --config webpack.production.config.js"
10+
"deploy": "NODE_ENV=production webpack -p"
1111
},
1212
"repository": {
1313
"type": "git",
@@ -34,16 +34,27 @@
3434
},
3535
"devDependencies": {
3636
"babel-core": "^6.3.21",
37+
"babel-jest": "^6.0.1",
3738
"babel-loader": "^6.2.0",
3839
"babel-plugin-object-assign": "^1.2.1",
3940
"babel-preset-es2015": "^6.3.13",
4041
"babel-preset-react": "^6.3.13",
4142
"babel-preset-stage-0": "^6.3.13",
4243
"css-loader": "^0.23.1",
4344
"extract-text-webpack-plugin": "^0.9.1",
45+
"jest-cli": "^0.8.2",
4446
"jsx-loader": "^0.13.2",
4547
"style-loader": "^0.13.0",
4648
"webpack": "^1.12.9",
4749
"webpack-dev-server": "^1.14.0"
50+
},
51+
"jest": {
52+
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
53+
"unmockedModulePathPatterns": [
54+
"<rootDir>/node_modules/react"
55+
],
56+
"modulePathIgnorePatterns": [
57+
"<rootDir>/node_modules/"
58+
]
4859
}
4960
}

0 commit comments

Comments
 (0)