Skip to content

Commit 8c49d5c

Browse files
committed
added mobx testing
1 parent d0752f6 commit 8c49d5c

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { shallow } from 'enzyme'
2+
import React from "react"
3+
4+
import TodoList from "../src/js/TodoList"
5+
import { TodoStore } from "../src/js/TodoStore"
6+
7+
describe("TodoList.functional", () => {
8+
9+
it("filters todos", () => {
10+
const store = new TodoStore
11+
12+
store.createTodo("todo1")
13+
store.createTodo("todo2")
14+
store.createTodo("todo3")
15+
store.filter = "2"
16+
17+
const wrapper = shallow(<TodoList store={store} />)
18+
19+
expect(wrapper.find("li").length).toBe(1)
20+
expect(wrapper.find("li span").at(0).text()).toBe("todo2")
21+
})
22+
23+
it("clears completed todos when 'clear completed' is clicked", () => {
24+
const store = new TodoStore
25+
26+
store.createTodo("todo1")
27+
store.createTodo("todo2")
28+
store.createTodo("todo3")
29+
store.todos[0].complete = true
30+
store.todos[1].complete = true
31+
32+
const wrapper = shallow(<TodoList store={store} />)
33+
34+
wrapper.find("a").simulate("click")
35+
36+
expect(wrapper.find("li").length).toBe(1)
37+
expect(wrapper.find("li span").at(0).text()).toBe("todo3")
38+
expect(store.todos.length).toBe(1)
39+
})
40+
})
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { shallow } from 'enzyme'
2+
import React from "react"
3+
4+
import TodoList from "../src/js/TodoList"
5+
6+
describe("TodoList", function() {
7+
//don't use an arrow function...preserve the value of "this"
8+
beforeEach(function() {
9+
this.store = {
10+
filteredTodos: [
11+
{value: "todo1", id: 111, complete: false},
12+
{value: "todo2", id: 222, complete: false},
13+
{value: "todo3", id: 333, complete: false},
14+
],
15+
filter: "test",
16+
createTodo: (val) => {
17+
this.createTodoCalled = true
18+
this.todoValue = val
19+
},
20+
}
21+
})
22+
23+
//don't use an arrow function, preserve the value of "this"
24+
it("renders filtered todos", function() {
25+
const wrapper = shallow(<TodoList store={this.store} />)
26+
27+
expect(wrapper.find("li span").at(0).text()).toBe("todo1")
28+
expect(wrapper.find("li span").at(1).text()).toBe("todo2")
29+
expect(wrapper.find("li span").at(2).text()).toBe("todo3")
30+
})
31+
32+
it("calls createTodo on enter", function() {
33+
const wrapper = shallow(<TodoList store={this.store} />)
34+
35+
wrapper.find("input.new").at(0)
36+
.simulate("keypress", {which: 13, target: {value: 'newTodo'}})
37+
38+
expect(this.createTodoCalled).toBe(true)
39+
expect(this.todoValue).toBe("newTodo")
40+
})
41+
42+
it("updates store filter", function() {
43+
const wrapper = shallow(<TodoList store={this.store} />)
44+
45+
wrapper.find("input.filter").at(0)
46+
.simulate('change', {target: {value: 'filter'}})
47+
48+
expect(this.store.filter).toBe("filter")
49+
})
50+
51+
})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { TodoStore } from "../src/js/TodoStore"
2+
3+
describe("TodoStore", () => {
4+
it("creates new todos", () => {
5+
const store = new TodoStore
6+
store.createTodo("todo1")
7+
store.createTodo("todo2")
8+
expect(store.todos.length).toBe(2)
9+
expect(store.todos[0].value).toBe("todo1")
10+
expect(store.todos[1].value).toBe("todo2")
11+
})
12+
13+
it("clears checked todos", () => {
14+
const store = new TodoStore
15+
store.createTodo("todo1")
16+
store.createTodo("todo2")
17+
store.createTodo("todo3")
18+
store.todos[1].complete = true;
19+
store.todos[2].complete = true;
20+
store.clearComplete()
21+
22+
expect(store.todos.length).toBe(1)
23+
expect(store.todos[0].value).toBe("todo1")
24+
})
25+
})

6-mobx-react/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@
1616
"react-dom": "^15.3.0"
1717
},
1818
"devDependencies": {
19+
"babel-jest": "^14.1.0",
1920
"babel-loader": "^6.2.4",
2021
"babel-plugin-transform-class-properties": "^6.10.2",
2122
"babel-plugin-transform-decorators-legacy": "^1.3.4",
2223
"babel-preset-es2015": "^6.9.0",
2324
"babel-preset-react": "^6.11.1",
2425
"css-loader": "^0.23.1",
26+
"enzyme": "^2.4.1",
27+
"jest": "^14.1.0",
2528
"react-addons-test-utils": "^15.3.0",
2629
"style-loader": "^0.13.1",
2730
"webpack": "^1.13.1",
2831
"webpack-dev-server": "^1.14.1"
32+
},
33+
"jest": {
34+
"automock": false,
35+
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest"
2936
}
3037
}

0 commit comments

Comments
 (0)