Skip to content

Commit d57180c

Browse files
committed
add test for TodoListComponent
1 parent fe07734 commit d57180c

File tree

5 files changed

+293
-24
lines changed

5 files changed

+293
-24
lines changed

src/app/new-todo/new-todo.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { NewTodoComponent } from './new-todo.component';
1717
export class BlankCmp {
1818
}
1919

20-
fdescribe('NewTodoComponent', () => {
20+
describe('NewTodoComponent', () => {
2121
let component: NewTodoComponent;
2222
let fixture: ComponentFixture<NewTodoComponent>;
2323
let store: Store<AppState>;
Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,59 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { Component } from '@angular/core';
3+
import { RouterModule, Routes, ActivatedRoute } from '@angular/router';
4+
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
5+
import { StoreModule, Store } from '@ngrx/store';
6+
import { RouterTestingModule } from '@angular/router/testing';
7+
import { Observable } from 'rxjs';
8+
import 'rxjs/add/observable/of';
9+
10+
import { rootReducer, AppState } from './../../redux/app.reducer';
11+
import * as TodoActions from './../../redux/todo/todo.actions';
12+
import * as FilterActions from './../../redux/filter/filter.actions';
213

314
import { TodoListComponent } from './todo-list.component';
15+
import { TodoComponent } from './../todo/todo.component';
16+
17+
@Component({
18+
selector: 'blank-cmp',
19+
template: ``
20+
})
21+
export class BlankCmp {
22+
}
423

5-
describe('TodoListComponent', () => {
24+
25+
fdescribe('TodoListComponent', () => {
626
let component: TodoListComponent;
727
let fixture: ComponentFixture<TodoListComponent>;
28+
let store: Store<AppState>;
29+
let route: ActivatedRoute;
830

931
beforeEach(async(() => {
1032
TestBed.configureTestingModule({
11-
declarations: [ TodoListComponent ]
33+
declarations: [
34+
TodoListComponent,
35+
TodoComponent,
36+
BlankCmp
37+
],
38+
imports: [
39+
ReactiveFormsModule,
40+
FormsModule,
41+
RouterTestingModule.withRoutes([
42+
{path: '', component: BlankCmp}
43+
]),
44+
StoreModule.forRoot(rootReducer),
45+
]
1246
})
1347
.compileComponents();
1448
}));
1549

1650
beforeEach(() => {
51+
52+
store = TestBed.get(Store);
53+
spyOn(store, 'dispatch').and.callThrough();
54+
55+
route = TestBed.get(ActivatedRoute);
56+
1757
fixture = TestBed.createComponent(TodoListComponent);
1858
component = fixture.componentInstance;
1959
fixture.detectChanges();
@@ -22,4 +62,108 @@ describe('TodoListComponent', () => {
2262
it('should be created', () => {
2363
expect(component).toBeTruthy();
2464
});
65+
66+
describe("Test for checkField", ()=>{
67+
68+
it("should checkField be defined", ()=>{
69+
expect(component.checkField).toBeDefined();
70+
});
71+
72+
it("should checkField be true with new stateCompleted", ()=>{
73+
const todos = [
74+
{ id: 1, text: 'todo', completed: true },
75+
{ id: 2, text: 'todo', completed: true },
76+
{ id: 3, text: 'todo', completed: true },
77+
];
78+
const action = new TodoActions.PopulateTodosAction(todos);
79+
store.dispatch(action);
80+
expect(component.checkField.value).toBeTruthy();
81+
});
82+
83+
it("should checkField be false with new readStateCompleted", ()=>{
84+
const todos = [
85+
{ id: 1, text: 'todo', completed: true },
86+
{ id: 2, text: 'todo', completed: false },
87+
{ id: 3, text: 'todo', completed: true },
88+
];
89+
const action = new TodoActions.PopulateTodosAction(todos);
90+
store.dispatch(action);
91+
expect(component.checkField.value).toBeFalsy();
92+
});
93+
94+
});
95+
96+
describe("Test for toggleAll", ()=>{
97+
98+
it("should dispatch an action", ()=>{
99+
component.toggleAll();
100+
const action = new TodoActions.CompletedAllAction();
101+
expect(store.dispatch).toHaveBeenCalledWith(action);
102+
});
103+
104+
});
105+
106+
describe("Test for readTodosState", ()=>{
107+
108+
it("should todos be equal that new todos", ()=>{
109+
const todos = [
110+
{ id: 1, text: 'todo', completed: true },
111+
{ id: 2, text: 'todo', completed: false },
112+
{ id: 3, text: 'todo', completed: true },
113+
];
114+
const action = new TodoActions.PopulateTodosAction(todos);
115+
store.dispatch(action);
116+
expect(component.todos).toEqual(todos);
117+
});
118+
119+
});
120+
121+
describe("Test for readParams", ()=>{
122+
123+
it("should dispatch an action when filter is unknown", ()=> {
124+
125+
route = TestBed.get(ActivatedRoute);
126+
route.params = Observable.of({
127+
'filter': 'what'
128+
});
129+
130+
fixture = TestBed.createComponent(TodoListComponent);
131+
component = fixture.componentInstance;
132+
fixture.detectChanges();
133+
134+
const action = new FilterActions.SetFilterAction('SHOW_ALL');
135+
expect(store.dispatch).toHaveBeenCalledWith(action);
136+
});
137+
138+
it('should dispatch an action when filter is "active"', ()=>{
139+
140+
route = TestBed.get(ActivatedRoute);
141+
route.params = Observable.of({
142+
'filter': 'active'
143+
});
144+
145+
fixture = TestBed.createComponent(TodoListComponent);
146+
component = fixture.componentInstance;
147+
fixture.detectChanges();
148+
149+
const action = new FilterActions.SetFilterAction('SHOW_ACTIVE');
150+
expect(store.dispatch).toHaveBeenCalledWith(action);
151+
});
152+
153+
it('should dispatch an action when filter is "completed"', ()=>{
154+
155+
route = TestBed.get(ActivatedRoute);
156+
route.params = Observable.of({
157+
'filter': 'completed'
158+
});
159+
160+
fixture = TestBed.createComponent(TodoListComponent);
161+
component = fixture.componentInstance;
162+
fixture.detectChanges();
163+
164+
const action = new FilterActions.SetFilterAction('SHOW_COMPLETED');
165+
expect(store.dispatch).toHaveBeenCalledWith(action);
166+
});
167+
168+
});
25169
});

src/app/todo-list/todo-list.component.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,15 @@ export class TodoListComponent implements OnInit {
2222
private store: Store<AppState>,
2323
private route: ActivatedRoute
2424
) {
25-
this.route.params
26-
.subscribe(params => {
27-
this.setFilter(params.filter);
28-
});
2925
this.checkField = new FormControl();
30-
this.store.select(getStateCompleted)
31-
.subscribe(status => {
32-
this.checkField.setValue(status);
33-
});
26+
this.readParams();
27+
this.readStateCompleted();
3428
this.readTodosState();
3529
}
3630

3731
ngOnInit() {
3832
}
3933

40-
readTodosState() {
41-
this.store.select(getVisibleTodos)
42-
.subscribe(todos => {
43-
this.todos = todos;
44-
});
45-
}
46-
4734
toggleAll() {
4835
this.store.dispatch(new TodoActions.CompletedAllAction());
4936
}
@@ -64,4 +51,25 @@ export class TodoListComponent implements OnInit {
6451
}
6552
}
6653
}
54+
55+
private readTodosState() {
56+
this.store.select(getVisibleTodos)
57+
.subscribe(todos => {
58+
this.todos = todos;
59+
});
60+
}
61+
62+
private readStateCompleted() {
63+
this.store.select(getStateCompleted)
64+
.subscribe(status => {
65+
this.checkField.setValue(status);
66+
});
67+
}
68+
69+
private readParams() {
70+
this.route.params
71+
.subscribe(params => {
72+
this.setFilter(params.filter);
73+
});
74+
}
6775
}

src/app/todo/todo.component.spec.ts

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,139 @@
1-
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { async, ComponentFixture, TestBed, tick, fakeAsync } from '@angular/core/testing';
2+
import { Component } from '@angular/core';
3+
import { RouterModule, Routes } from '@angular/router';
4+
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
5+
import { StoreModule, Store } from '@ngrx/store';
6+
import { RouterTestingModule } from '@angular/router/testing';
7+
8+
import { rootReducer, AppState } from './../../redux/app.reducer';
9+
import * as TodoActions from './../../redux/todo/todo.actions';
210

311
import { TodoComponent } from './todo.component';
412

13+
@Component({
14+
selector: 'blank-cmp',
15+
template: ``
16+
})
17+
export class BlankCmp {
18+
}
19+
20+
521
describe('TodoComponent', () => {
622
let component: TodoComponent;
723
let fixture: ComponentFixture<TodoComponent>;
24+
let store: Store<AppState>;
825

926
beforeEach(async(() => {
1027
TestBed.configureTestingModule({
11-
declarations: [ TodoComponent ]
28+
declarations: [
29+
TodoComponent,
30+
BlankCmp
31+
],
32+
imports: [
33+
ReactiveFormsModule,
34+
FormsModule,
35+
RouterTestingModule.withRoutes([
36+
{path: '', component: BlankCmp}
37+
]),
38+
StoreModule.forRoot(rootReducer),
39+
]
1240
})
1341
.compileComponents();
1442
}));
1543

1644
beforeEach(() => {
45+
46+
store = TestBed.get(Store);
47+
spyOn(store, 'dispatch').and.callThrough();
48+
1749
fixture = TestBed.createComponent(TodoComponent);
1850
component = fixture.componentInstance;
51+
component.todo = {
52+
id: 1,
53+
text: 'test todo',
54+
completed: true
55+
};
1956
fixture.detectChanges();
2057
});
2158

2259
it('should be created', () => {
2360
expect(component).toBeTruthy();
2461
});
62+
63+
describe("Test for textField", ()=>{
64+
65+
it("should textField be defined", ()=>{
66+
expect(component.textField).toBeDefined();
67+
});
68+
69+
it("should textField be valid", ()=>{
70+
component.textField.setValue('new todo');
71+
expect(component.textField.valid).toBeTruthy();
72+
});
73+
74+
it("should textField be invalid", ()=>{
75+
component.textField.setValue('');
76+
expect(component.textField.invalid).toBeTruthy();
77+
});
78+
79+
});
80+
81+
describe("Test for checkField", ()=>{
82+
83+
it("should checkField be defined", ()=>{
84+
expect(component.checkField).toBeDefined();
85+
});
86+
87+
it("should dispatch an action when checkField change state", ()=>{
88+
component.checkField.setValue(true);
89+
const action = new TodoActions.ToggleAction(component.todo.id);
90+
expect(store.dispatch).toHaveBeenCalledWith(action);
91+
});
92+
93+
});
94+
95+
describe("Test for updateText", ()=>{
96+
97+
it("should dispatch an action if textField is valid and editing is true", ()=>{
98+
component.editing = true;
99+
component.textField.setValue('update todo', { emitEvent: false });
100+
component.updateText();
101+
const action = new TodoActions.UpdateAction(component.todo.id, component.textField.value);
102+
expect(store.dispatch).toHaveBeenCalledWith(action);
103+
});
104+
105+
it("should editing be false after send dispatch", ()=>{
106+
component.editing = true;
107+
component.textField.setValue('update todo', { emitEvent: false });
108+
component.updateText();
109+
expect(component.editing).toBeFalsy();
110+
});
111+
112+
});
113+
114+
describe("Test for activeEditMode", ()=>{
115+
116+
it("should editing be true", ()=>{
117+
component.activeEditMode();
118+
expect(component.editing).toBeTruthy();
119+
});
120+
121+
it("should textInput call focus", fakeAsync(()=>{
122+
spyOn(component.textInput.nativeElement, 'focus').and.callThrough();
123+
component.activeEditMode();
124+
tick(1000);
125+
expect(component.textInput.nativeElement.focus).toHaveBeenCalled();
126+
}));
127+
128+
});
129+
130+
describe("Test for deleteTodo", ()=>{
131+
132+
it("should dispatch an action", ()=>{
133+
component.deleteTodo();
134+
const action = new TodoActions.DeleteTodoAction(component.todo.id);
135+
expect(store.dispatch).toHaveBeenCalledWith(action);
136+
});
137+
138+
});
25139
});

0 commit comments

Comments
 (0)