Skip to content

Commit 0806bbc

Browse files
committed
using async helper function
1 parent 95a66b0 commit 0806bbc

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

src/app/hero-detail/hero-detail.component.spec.ts

+33-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
ComponentFixture,
44
fakeAsync,
55
tick,
6-
flush
6+
flush,
7+
async
78
} from "@angular/core/testing"; // for integration test
89
import { HeroDetailComponent } from "./hero-detail.component";
910
import { ActivatedRoute } from "@angular/router";
@@ -52,33 +53,47 @@ describe(`Hero Detail Component`, () => {
5253
); //assert;
5354
});
5455

55-
it(`should call updateHero when save is called`, done => {
56-
//now the test will wait until the async code its called
57-
mockHeroService.updateHero.and.returnValue(of({}));
56+
// it(`should call updateHero when save is called`, done => {
57+
// //now the test will wait until the async code its called
58+
// mockHeroService.updateHero.and.returnValue(of({}));
5859

59-
fixture.detectChanges(); //at
60+
// fixture.detectChanges(); //at
6061

61-
fixture.componentInstance.save();
62+
// fixture.componentInstance.save();
6263

63-
setTimeout(() => {
64-
//bacause we set 250 on the heroDeatails deboucen cal function
65-
expect(mockHeroService.updateHero).toHaveBeenCalled();
66-
done();
67-
}, 300);
68-
});
64+
// setTimeout(() => {
65+
// //bacause we set 250 on the heroDeatails deboucen cal function
66+
// expect(mockHeroService.updateHero).toHaveBeenCalled();
67+
// done();
68+
// }, 300);
69+
// });
70+
71+
// it("should call updateHero when save is called", fakeAsync(() => {
72+
// //now the test will wait until the async code its called
73+
// mockHeroService.updateHero.and.returnValue(of({}));
6974

70-
it("should call updateHero when save is called", fakeAsync(() => {
75+
// fixture.detectChanges(); //at
76+
77+
// fixture.componentInstance.save();
78+
// //bacause we set 250 on the heroDeatails deboucen cal function
79+
80+
// flush(); //look at the zone to see if there any task waiting
81+
82+
// //tick(250); //control of zone.js can be manipulate the time inside of it thanks to thick
83+
// expect(mockHeroService.updateHero).toHaveBeenCalled();
84+
// }));
85+
86+
it("should call updateHero when save is called", async(() => {
7187
//now the test will wait until the async code its called
7288
mockHeroService.updateHero.and.returnValue(of({}));
7389

7490
fixture.detectChanges(); //at
7591

7692
fixture.componentInstance.save();
77-
//bacause we set 250 on the heroDeatails deboucen cal function
7893

79-
flush(); //look at the zone to see if there any task waiting
80-
81-
//tick(250); //control of zone.js can be manipulate the time inside of it thanks to thick
82-
expect(mockHeroService.updateHero).toHaveBeenCalled();
94+
fixture.whenStable().then(() => {
95+
//checks for promises if they are all resolve
96+
expect(mockHeroService.updateHero).toHaveBeenCalled();
97+
});
8398
}));
8499
});

src/app/hero-detail/hero-detail.component.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,22 @@ export class HeroDetailComponent implements OnInit {
3232
this.location.back();
3333
}
3434

35+
// save(): void {
36+
// debounce(
37+
// //this makes our code async
38+
// () => {
39+
// this.heroService.updateHero(this.hero).subscribe(() => this.goBack());
40+
// },
41+
// 250,
42+
// false
43+
// )();
44+
// }
45+
3546
save(): void {
36-
debounce(
37-
//this makes our code async
38-
() => {
39-
this.heroService.updateHero(this.hero).subscribe(() => this.goBack());
40-
},
41-
250,
42-
false
43-
)();
47+
const p = new Promise(resolve => {
48+
this.heroService.updateHero(this.hero).subscribe(() => this.goBack());
49+
resolve();
50+
});
4451
}
4552
}
4653

0 commit comments

Comments
 (0)