Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit acfd22c

Browse files
authored
docs(testing): fix hero-detail impl and use done for observable tests (#3270)
1 parent bdac5ca commit acfd22c

File tree

5 files changed

+46
-32
lines changed

5 files changed

+46
-32
lines changed

public/docs/_examples/testing/ts/src/app/app.component.router.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class Page {
174174
}
175175

176176
constructor() {
177-
router.events.forEach(e => this.recordedEvents.push(e));
177+
router.events.subscribe(e => this.recordedEvents.push(e));
178178
const links = fixture.debugElement.queryAll(By.directive(RouterLinkWithHref));
179179
this.aboutLinkDe = links[2];
180180
this.dashboardLinkDe = links[0];

public/docs/_examples/testing/ts/src/app/bag/async-helper.spec.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// tslint:disable-next-line:no-unused-variable
12
import { async, fakeAsync, tick } from '@angular/core/testing';
23

34
import { Observable } from 'rxjs/Observable';
@@ -31,26 +32,37 @@ describe('Angular async helper', () => {
3132
p.catch(() => { actuallyDone = true; });
3233
}));
3334

34-
// Fail message: Cannot use setInterval from within an async zone test
35+
// Use done. Cannot use setInterval with async or fakeAsync
3536
// See https://github.com/angular/angular/issues/10127
36-
xit('should run async test with successful delayed Observable', async(() => {
37+
it('should run async test with successful delayed Observable', done => {
3738
const source = Observable.of(true).delay(10);
3839
source.subscribe(
3940
val => actuallyDone = true,
40-
err => fail(err)
41+
err => fail(err),
42+
done
4143
);
42-
}));
44+
});
4345

44-
// Fail message: Error: 1 periodic timer(s) still in the queue
46+
// Cannot use setInterval from within an async zone test
4547
// See https://github.com/angular/angular/issues/10127
46-
xit('should run async test with successful delayed Observable', fakeAsync(() => {
47-
const source = Observable.of(true).delay(10);
48-
source.subscribe(
49-
val => actuallyDone = true,
50-
err => fail(err)
51-
);
48+
// xit('should run async test with successful delayed Observable', async(() => {
49+
// const source = Observable.of(true).delay(10);
50+
// source.subscribe(
51+
// val => actuallyDone = true,
52+
// err => fail(err)
53+
// );
54+
// }));
5255

53-
tick();
54-
}));
56+
// // Fail message: Error: 1 periodic timer(s) still in the queue
57+
// // See https://github.com/angular/angular/issues/10127
58+
// xit('should run async test with successful delayed Observable', fakeAsync(() => {
59+
// const source = Observable.of(true).delay(10);
60+
// source.subscribe(
61+
// val => actuallyDone = true,
62+
// err => fail(err)
63+
// );
64+
65+
// tick();
66+
// }));
5567

5668
});

public/docs/_examples/testing/ts/src/app/bag/bag.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ describe('use inject helper in beforeEach', () => {
7272
);
7373
}));
7474

75-
// #enddocregion FancyService
76-
// See https://github.com/angular/angular/issues/10127
77-
xit('test should wait for FancyService.getObservableDelayValue', async(() => {
78-
service.getObservableDelayValue().subscribe(
79-
value => expect(value).toBe('observable delay value')
80-
);
81-
}));
82-
// #docregion FancyService
75+
// Must use done. See https://github.com/angular/angular/issues/10127
76+
it('test should wait for FancyService.getObservableDelayValue', done => {
77+
service.getObservableDelayValue().subscribe(value => {
78+
expect(value).toBe('observable delay value');
79+
done();
80+
});
81+
});
82+
8383
it('should allow the use of fakeAsync', fakeAsync(() => {
8484
let value: any;
8585
service.getAsyncValue().then((val: any) => value = val);

public/docs/_examples/testing/ts/src/app/hero/hero-detail.component.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ export class HeroDetailComponent implements OnInit {
3030
// #docregion ng-on-init
3131
ngOnInit(): void {
3232
// get hero when `id` param changes
33-
this.route.params.map(p => p['id'])
34-
.forEach(id => this.getHero(id))
35-
.catch(() => this.hero = new Hero()); // no id; should edit new hero
33+
this.route.params.subscribe(p => this.getHero(p && p['id']));
3634
}
3735
// #enddocregion ng-on-init
3836

3937
private getHero(id: string): void {
38+
// when no id or id===0, create new hero
39+
if (!id) {
40+
this.hero = new Hero();
41+
return;
42+
}
43+
4044
this.heroDetailService.getHero(id).then(hero => {
4145
if (hero) {
4246
this.hero = hero;

public/docs/ts/latest/guide/testing.jade

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,17 +1105,15 @@ a#routed-component-w-param
11051105
Here's the `HeroDetailComponent` constructor:
11061106
+makeExample('testing/ts/src/app/hero/hero-detail.component.ts', 'ctor', 'src/app/hero/hero-detail.component.ts (constructor)')(format='.')
11071107
:marked
1108-
`HeroDetailComponent` listens for changes to the `ActivatedRoute.params` in its `ngOnInit` method.
1108+
`HeroDetailComponent` subscribes to `ActivatedRoute.params` changes in its `ngOnInit` method.
11091109
+makeExample('testing/ts/src/app/hero/hero-detail.component.ts', 'ng-on-init', 'src/app/hero/hero-detail.component.ts (ngOnInit)')(format='.')
11101110
.l-sub-section
11111111
:marked
1112-
The expression after `route.params` chains an _Observable_ operator that _plucks_ the `id` from the `params`
1113-
and then chains a `forEach` operator to subscribes to `id`-changing events.
1114-
The `id` changes every time the user navigates to a different hero.
1115-
1116-
The `forEach` passes the new `id` value to the component's `getHero` method (not shown)
1112+
The subscription confirms that there are `params` and, if there are,
1113+
plucks the `id` from those `params`.
1114+
It passes the `id` to the component's `getHero` method (not shown)
11171115
which fetches a hero and sets the component's `hero` property.
1118-
If the`id` parameter is missing, the `pluck` operator fails and the `catch` treats failure as a request to edit a new hero.
1116+
The `getHero` method treats an undefined `id` or `id===0` as a request to edit a new hero.
11191117

11201118
The [Router](router.html#route-parameters) guide covers `ActivatedRoute.params` in more detail.
11211119
:marked

0 commit comments

Comments
 (0)