Skip to content

Commit cd19722

Browse files
committed
feature(tests, styling): add todo-component tests and integration tests
- add unit tests - add integration tests - add highlight.js directive to fix highlighting in dynamic templates - add angular-ui-boostrap for collapse directive - add angular-animate
1 parent ac2f687 commit cd19722

File tree

8 files changed

+334
-65
lines changed

8 files changed

+334
-65
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
"license": "ISC",
1414
"dependencies": {
1515
"angular": "^1.4.4",
16+
"angular-animate": "^1.5.0-beta.0",
17+
"angular-highlightjs": "^0.4.3",
18+
"angular-ui-bootstrap": "^0.13.4",
1619
"angular-ui-router": "^0.2.15",
1720
"babel-core": "^5.8.20",
1821
"babel-runtime": "^5.8.20",
1922
"bootstrap": "^3.3.5",
23+
"highlight.js": "^8.8.0",
2024
"jquery": "^2.1.4",
2125
"lodash": "^3.10.1"
2226
},
@@ -35,6 +39,7 @@
3539
"mocha": "^2.2.5",
3640
"mocha-babel": "^3.0.0",
3741
"ngtemplate-loader": "^1.3.0",
42+
"sinon": "^1.17.1",
3843
"style-loader": "^0.12.3",
3944
"typescript": "^1.5.3",
4045
"url-loader": "^0.5.6",

src/feature-a/some-component/some-component.tpl.html

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,45 @@ <h2>Feature A is implemented as Component using
1212
<p>The implemented counter functionality is tested in isolation without instantiating of Angular JS context</p>
1313
<br />
1414
<hr />
15+
1516
<button class="btn btn-info pull-right" ui-sref="app.feature-b">Try Feature B</button>
1617
<button class="btn btn-success" ng-click="ctrl.increment()">Increment counter</button>
1718
<a class="btn btn-default" data-toggle="collapse" href="#testCode" aria-expanded="false" aria-controls="collapseExample">Toggle test code</a>
19+
1820
<br />
1921
<br />
20-
<pre class="collapse" id="testCode">
21-
<code class="javascript">
22-
import { assert } from 'chai';
2322

24-
import SomeComponent from './some-component';
23+
<div class="collapse" id="testCode">
24+
<code hljs language="javascript">
25+
import { assert } from 'chai';
2526

26-
let component;
27+
import SomeComponent from './some-component';
2728

28-
describe('some-component', function() {
29-
30-
beforeEach(function() {
31-
component = new SomeComponent();
32-
});
29+
let component;
3330

34-
it('should start with counter value 20', function () {
35-
assert.equal(component.counter, 20);
36-
});
31+
describe('some-component', function() {
3732

38-
it('should accept initial counter value as dependency', function () {
39-
component = new SomeComponent(30);
40-
assert.equal(component.counter, 30);
41-
});
33+
beforeEach(function() {
34+
component = new SomeComponent();
35+
});
4236

43-
it('should increment counter value after increment is called', function () {
44-
assert.equal(component.counter, 20);
45-
component.increment();
46-
assert.equal(component.counter, 21);
47-
});
37+
it('should start with counter value 20', function () {
38+
assert.equal(component.counter, 20);
39+
});
4840

49-
});
50-
</code>
51-
</pre>
41+
it('should accept initial counter value as dependency', function () {
42+
component = new SomeComponent(30);
43+
assert.equal(component.counter, 30);
44+
});
5245

46+
it('should increment counter value after increment is called', function () {
47+
assert.equal(component.counter, 20);
48+
component.increment();
49+
assert.equal(component.counter, 21);
50+
});
5351

52+
});
53+
</code>
54+
</div>
5455

5556
</div>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { assert } from 'chai';
2+
3+
import TodoComponent from './todo-component.js';
4+
import TodoService from '../services/todo.service.js';
5+
6+
let component;
7+
8+
describe('TodoComponent with real service (Integration test)', function() {
9+
10+
beforeEach(function() {
11+
let initialTodos = [];
12+
let todoService = TodoService(initialTodos);
13+
component = new TodoComponent(todoService);
14+
});
15+
16+
it('should contain reference to service\'s todos', function () {
17+
assert.equal(component.todos.length, 0);
18+
});
19+
20+
it('should add todo', function () {
21+
component.label = 'Finish example project';
22+
component.addTodo();
23+
assert.equal(component.label, '');
24+
assert.equal(component.todos.length, 1);
25+
assert.equal(component.todos[0].label, 'Finish example project');
26+
assert.equal(component.todos[0].done, false);
27+
});
28+
29+
it('should toggle todo', function () {
30+
component.label = 'Finish example project';
31+
component.addTodo();
32+
assert.equal(component.todos[0].done, false);
33+
34+
component.toggleTodo(component.todos[0]);
35+
assert.equal(component.todos[0].done, true);
36+
37+
component.toggleTodo(component.todos[0]);
38+
assert.equal(component.todos[0].done, false);
39+
});
40+
41+
it('should remove done todos', function () {
42+
component.label = 'Todo1';
43+
component.addTodo();
44+
45+
component.label = 'Todo2';
46+
component.addTodo();
47+
48+
component.label = 'Todo2';
49+
component.addTodo();
50+
51+
assert.equal(component.todos.length, 3);
52+
53+
component.toggleTodo(component.todos[0]);
54+
component.removeDoneTodos();
55+
assert.equal(component.todos.length, 2);
56+
});
57+
58+
});

src/feature-b/todo-component/todo-component.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export default class TodoComponent {
44
this.TodoService = TodoService;
55
this.todos = TodoService.todos;
66
this.label = '';
7+
8+
this.collapse = [true, true, true];
79
}
810

911
addTodo() {
@@ -19,4 +21,12 @@ export default class TodoComponent {
1921
this.TodoService.removeDoneTodos();
2022
}
2123

24+
toggleCollapse(index) {
25+
let originalValue = this.collapse[index];
26+
this.collapse.forEach((item, i) => {
27+
this.collapse[i] = true;
28+
});
29+
this.collapse[index] = !originalValue;
30+
}
31+
2232
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { assert } from 'chai';
2+
import * as sinon from 'sinon';
3+
4+
import TodoComponent from './todo-component.js';
5+
import TodoService from '../services/todo.service.js';
6+
7+
let component;
8+
let mockTodoService;
9+
10+
describe('TodoComponent with mocked service (unit test)', function() {
11+
12+
beforeEach(function() {
13+
let initialTodos = [];
14+
let TodoServiceInstance = TodoService(initialTodos);
15+
mockTodoService = sinon.mock(TodoServiceInstance);
16+
component = new TodoComponent(TodoServiceInstance);
17+
});
18+
19+
afterEach(function() {
20+
mockTodoService.restore();
21+
});
22+
23+
it('should add todo', function () {
24+
mockTodoService
25+
.expects('addTodo')
26+
.once()
27+
.withArgs('Finish example project');
28+
29+
component.label = 'Finish example project';
30+
component.addTodo();
31+
32+
mockTodoService.verify();
33+
});
34+
35+
it('should toggle todo', function () {
36+
let mockTodo = {label: 'Add unit tests', done: false};
37+
mockTodoService
38+
.expects('toggleTodo')
39+
.twice()
40+
.withArgs(mockTodo.label);
41+
42+
component.toggleTodo(mockTodo);
43+
component.toggleTodo(mockTodo);
44+
45+
mockTodoService.verify();
46+
});
47+
48+
it('should remove done todos', function () {
49+
mockTodoService
50+
.expects('removeDoneTodos')
51+
.once();
52+
53+
component.removeDoneTodos();
54+
55+
mockTodoService.verify();
56+
});
57+
58+
});

0 commit comments

Comments
 (0)