Skip to content

Commit 0c6889e

Browse files
committed
Dealing with Asynchronous operations (promise)
Test case for service calls with promise - way 1
1 parent 514b9c0 commit 0c6889e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/app/2-todos/todos.component.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ describe('TodosComponent', () => {
2626
component = fixture.componentInstance;
2727
});
2828

29-
it('should load todos from the server', () => {
29+
// Test case for service calls with Observable
30+
xit('should load todos from the server - Observable', () => {
3031
// If TodoService is given as provider in module
3132
let service = TestBed.get(TodoService);
3233
// If TodoService is given as provider in component
@@ -37,4 +38,16 @@ describe('TodosComponent', () => {
3738

3839
expect(component.todos.length).toBe(3);
3940
});
41+
42+
// Test case for service calls with Promise - Way 1
43+
it('should load todos from the server - Promise - Way 1', async(() => {
44+
let service = TestBed.get(TodoService);
45+
spyOn(service, 'getTodosPromise').and.returnValue(Promise.resolve( [1,2,3] ));
46+
47+
fixture.detectChanges();
48+
49+
fixture.whenStable().then(() => {
50+
expect(component.todos.length).toBe(3);
51+
});
52+
}));
4053
});

src/app/2-todos/todos.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export class TodosComponent implements OnInit {
1313
constructor(private service: TodoService) {}
1414

1515
ngOnInit() {
16-
this.service.getTodos().subscribe(t => this.todos = t);
16+
//this.service.getTodos().subscribe(t => this.todos = t);
17+
this.service.getTodosPromise().then(t => this.todos = t);
1718
}
1819

1920
add() {

0 commit comments

Comments
 (0)