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

Commit 93ede14

Browse files
committed
docs(testing): from Igor Minar feedback
1 parent 1c87bd6 commit 93ede14

File tree

11 files changed

+296
-277
lines changed

11 files changed

+296
-277
lines changed

public/docs/_examples/testing/ts/app/banner.component.spec.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { BannerComponent } from './banner.component';
1111
// #docregion setup
1212
let comp: BannerComponent;
1313
let fixture: ComponentFixture<BannerComponent>;
14-
let el: DebugElement;
14+
let de: DebugElement;
15+
let el: HTMLElement;
1516

1617
describe('BannerComponent', () => {
1718
beforeEach(() => {
@@ -23,25 +24,27 @@ describe('BannerComponent', () => {
2324

2425
comp = fixture.componentInstance; // BannerComponent test instance
2526

26-
// get title DebugElement by element name
27-
el = fixture.debugElement.query(By.css('h1'));
27+
// query for the title <h1> by CSS element selector
28+
de = fixture.debugElement.query(By.css('h1'));
29+
el = de.nativeElement;
30+
2831
});
2932
// #enddocregion setup
3033
// #docregion tests
3134
it('should display original title', () => {
32-
fixture.detectChanges(); // trigger data binding
33-
expect(el.nativeElement.textContent).toContain(comp.title);
35+
fixture.detectChanges();
36+
expect(el.textContent).toContain(comp.title);
3437
});
3538

3639
it('should display a different test title', () => {
3740
comp.title = 'Test Title';
38-
fixture.detectChanges(); // trigger data binding
39-
expect(el.nativeElement.textContent).toContain('Test Title');
41+
fixture.detectChanges();
42+
expect(el.textContent).toContain('Test Title');
4043
});
4144
// #enddocregion tests
4245
// #docregion test-w-o-detect-changes
4346
it('no title in the DOM until manually call `detectChanges`', () => {
44-
expect(el.nativeElement.textContent).toEqual('');
47+
expect(el.textContent).toEqual('');
4548
});
4649
// #enddocregion test-w-o-detect-changes
4750

@@ -59,36 +62,36 @@ describe('BannerComponent with AutoChangeDetect', () => {
5962
fixture = TestBed.configureTestingModule({
6063
declarations: [ BannerComponent ],
6164
providers: [
62-
{ provide: ComponentFixtureAutoDetect,
63-
useValue: true }
65+
{ provide: ComponentFixtureAutoDetect, useValue: true }
6466
]
6567
})
6668
// #enddocregion auto-detect
6769
.createComponent(BannerComponent);
6870

6971
comp = fixture.componentInstance; // BannerComponent test instance
7072

71-
// find title DebugElement by element name
72-
el = fixture.debugElement.query(By.css('h1'));
73+
// query for the title <h1> by CSS element selector
74+
de = fixture.debugElement.query(By.css('h1'));
75+
el = de.nativeElement;
7376
});
7477

7578
// #docregion auto-detect-tests
7679
it('should display original title', () => {
7780
// Hooray! No `fixture.detectChanges()` needed
78-
expect(el.nativeElement.textContent).toContain(comp.title);
81+
expect(el.textContent).toContain(comp.title);
7982
});
8083

8184
it('should still see original title after comp.title change', () => {
8285
const oldTitle = comp.title;
8386
comp.title = 'Test Title';
8487
// Displayed title is old because Angular didn't hear the change :(
85-
expect(el.nativeElement.textContent).toContain(oldTitle);
88+
expect(el.textContent).toContain(oldTitle);
8689
});
8790

8891
it('should display updated title after detectChanges', () => {
8992
comp.title = 'Test Title';
9093
fixture.detectChanges(); // detect changes explicitly
91-
expect(el.nativeElement.textContent).toContain(comp.title);
94+
expect(el.textContent).toContain(comp.title);
9295
});
9396
// #enddocregion auto-detect-tests
9497
});
@@ -114,14 +117,14 @@ describe('BannerComponent (simpified)', () => {
114117
// #docregion simple-example-it
115118
it('should display original title', () => {
116119

117-
// trigger data binding to update the view
120+
// trigger change detection to update the view
118121
fixture.detectChanges();
119122

120-
// find the title element in the DOM using a CSS selector
121-
el = fixture.debugElement.query(By.css('h1'));
123+
// query for the title <h1> by CSS element selector
124+
de = fixture.debugElement.query(By.css('h1'));
122125

123126
// confirm the element's content
124-
expect(el.nativeElement.textContent).toContain(comp.title);
127+
expect(de.nativeElement.textContent).toContain(comp.title);
125128
});
126129
// #enddocregion simple-example-it
127130
});

public/docs/_examples/testing/ts/app/shared/title-case.pipe.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
// #docregion
33
import { TitleCasePipe } from './title-case.pipe';
44

5-
// #docregion excerpt
5+
// #docregion excerpt, mini-excerpt
66
describe('TitleCasePipe', () => {
7-
// This pipe is a pure function so no need for BeforeEach
7+
// This pipe is a pure, stateless function so no need for BeforeEach
88
let pipe = new TitleCasePipe();
99

1010
it('transforms "abc" to "Abc"', () => {
1111
expect(pipe.transform('abc')).toBe('Abc');
1212
});
13+
// #enddocregion mini-excerpt
1314

1415
it('transforms "abc def" to "Abc Def"', () => {
1516
expect(pipe.transform('abc def')).toBe('Abc Def');
@@ -28,6 +29,6 @@ describe('TitleCasePipe', () => {
2829
it('transforms " abc def" to " Abc Def" (preserves spaces) ', () => {
2930
expect(pipe.transform(' abc def')).toBe(' Abc Def');
3031
});
31-
// #docregion excerpt
32+
// #docregion excerpt, mini-excerpt
3233
});
33-
// #enddocregion excerpt
34+
// #enddocregion excerpt, mini-excerpt

public/docs/_examples/testing/ts/app/shared/twain.component.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ describe('TwainComponent', () => {
1212
let fixture: ComponentFixture<TwainComponent>;
1313

1414
let spy: jasmine.Spy;
15-
let twainEl: DebugElement; // the element with the Twain quote
15+
let de: DebugElement;
16+
let el: HTMLElement;
1617
let twainService: TwainService; // the actually injected service
1718

1819
const testQuote = 'Test Quote';
@@ -37,54 +38,53 @@ describe('TwainComponent', () => {
3738
// #enddocregion spy
3839

3940
// Get the Twain quote element by CSS selector (e.g., by class name)
40-
twainEl = fixture.debugElement.query(By.css('.twain'));
41+
de = fixture.debugElement.query(By.css('.twain'));
42+
el = de.nativeElement;
4143
});
4244
// #enddocregion setup
4345

4446
// #docregion tests
45-
function getQuote() { return twainEl.nativeElement.textContent; }
46-
4747
it('should not show quote before OnInit', () => {
48-
expect(getQuote()).toBe('', 'nothing displayed');
48+
expect(el.textContent).toBe('', 'nothing displayed');
4949
expect(spy.calls.any()).toBe(false, 'getQuote not yet called');
5050
});
5151

5252
it('should still not show quote after component initialized', () => {
53-
fixture.detectChanges(); // trigger data binding
53+
fixture.detectChanges();
5454
// getQuote service is async => still has not returned with quote
55-
expect(getQuote()).toBe('...', 'no quote yet');
55+
expect(el.textContent).toBe('...', 'no quote yet');
5656
expect(spy.calls.any()).toBe(true, 'getQuote called');
5757
});
5858

5959
// #docregion async-test
6060
it('should show quote after getQuote promise (async)', async(() => {
61-
fixture.detectChanges(); // trigger data binding
61+
fixture.detectChanges();
6262

6363
fixture.whenStable().then(() => { // wait for async getQuote
6464
fixture.detectChanges(); // update view with quote
65-
expect(getQuote()).toBe(testQuote);
65+
expect(el.textContent).toBe(testQuote);
6666
});
6767
}));
6868
// #enddocregion async-test
6969

7070
// #docregion fake-async-test
7171
it('should show quote after getQuote promise (fakeAsync)', fakeAsync(() => {
72-
fixture.detectChanges(); // trigger data binding
72+
fixture.detectChanges();
7373
tick(); // wait for async getQuote
7474
fixture.detectChanges(); // update view with quote
75-
expect(getQuote()).toBe(testQuote);
75+
expect(el.textContent).toBe(testQuote);
7676
}));
7777
// #enddocregion fake-async-test
7878
// #enddocregion tests
7979

8080
// #docregion done-test
8181
it('should show quote after getQuote promise (done)', done => {
82-
fixture.detectChanges(); // trigger data binding
82+
fixture.detectChanges();
8383

8484
// get the spy promise and wait for it to resolve
8585
spy.calls.mostRecent().returnValue.then(() => {
8686
fixture.detectChanges(); // update view with quote
87-
expect(getQuote()).toBe(testQuote);
87+
expect(el.textContent).toBe(testQuote);
8888
done();
8989
});
9090
});
Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// #docplaster
2-
import { ComponentFixture, TestBed } from '@angular/core/testing';
3-
import { By } from '@angular/platform-browser';
4-
import { DebugElement } from '@angular/core';
2+
import { ComponentFixture, inject, TestBed } from '@angular/core/testing';
3+
import { By } from '@angular/platform-browser';
4+
import { DebugElement } from '@angular/core';
55

66
import { UserService } from './model';
77
import { WelcomeComponent } from './welcome.component';
@@ -10,8 +10,10 @@ describe('WelcomeComponent', () => {
1010

1111
let comp: WelcomeComponent;
1212
let fixture: ComponentFixture<WelcomeComponent>;
13-
let userService: UserService; // the actually injected service
14-
let welcomeEl: DebugElement; // the element with the welcome message
13+
let componentUserService: UserService; // the actually injected service
14+
let userService: UserService; // the TestBed injected service
15+
let de: DebugElement; // the DebugElement with the welcome message
16+
let el: HTMLElement; // the DOM element with the welcome message
1517

1618
let userServiceStub: {
1719
isLoggedIn: boolean;
@@ -32,7 +34,8 @@ describe('WelcomeComponent', () => {
3234
TestBed.configureTestingModule({
3335
declarations: [ WelcomeComponent ],
3436
// #enddocregion setup
35-
// providers: [ UserService ] // a real service would be a problem!
37+
// providers: [ UserService ] // NO! Don't provide the real service!
38+
// Provide a test-double instead
3639
// #docregion setup
3740
providers: [ {provide: UserService, useValue: userServiceStub } ]
3841
});
@@ -42,51 +45,64 @@ describe('WelcomeComponent', () => {
4245
comp = fixture.componentInstance;
4346

4447
// #enddocregion setup
45-
// #docregion inject-from-testbed
46-
// UserService provided to the TestBed
47-
userService = TestBed.get(UserService);
48-
// #enddocregion inject-from-testbed
49-
// #docregion setup
50-
// #docregion injected-service
48+
// #docregion injected-service
5149
// UserService actually injected into the component
5250
userService = fixture.debugElement.injector.get(UserService);
5351
// #enddocregion injected-service
52+
componentUserService = userService;
53+
// #docregion setup
54+
// #docregion inject-from-testbed
55+
// UserService from the root injector
56+
userService = TestBed.get(UserService);
57+
// #enddocregion inject-from-testbed
5458

5559
// get the "welcome" element by CSS selector (e.g., by class name)
56-
welcomeEl = fixture.debugElement.query(By.css('.welcome'));
60+
de = fixture.debugElement.query(By.css('.welcome'));
61+
el = de.nativeElement;
5762
});
5863
// #enddocregion setup
5964

6065
// #docregion tests
6166
it('should welcome the user', () => {
62-
fixture.detectChanges(); // trigger data binding
63-
64-
let content = welcomeEl.nativeElement.textContent;
67+
fixture.detectChanges();
68+
const content = el.textContent;
6569
expect(content).toContain('Welcome', '"Welcome ..."');
6670
expect(content).toContain('Test User', 'expected name');
6771
});
6872

6973
it('should welcome "Bubba"', () => {
7074
userService.user.name = 'Bubba'; // welcome message hasn't been shown yet
71-
72-
fixture.detectChanges(); // trigger data binding
73-
74-
let content = welcomeEl.nativeElement.textContent;
75-
expect(content).toContain('Bubba');
75+
fixture.detectChanges();
76+
expect(el.textContent).toContain('Bubba');
7677
});
7778

7879
it('should request login if not logged in', () => {
7980
userService.isLoggedIn = false; // welcome message hasn't been shown yet
80-
81-
fixture.detectChanges(); // trigger data binding
82-
83-
let content = welcomeEl.nativeElement.textContent;
81+
fixture.detectChanges();
82+
const content = el.textContent;
8483
expect(content).not.toContain('Welcome', 'not welcomed');
8584
expect(content).toMatch(/log in/i, '"log in"');
8685
});
8786
// #enddocregion tests
8887

89-
it('orig stub and injected UserService are not the same object', () => {
88+
// #docregion inject-it
89+
it('should inject the component\'s UserService instance',
90+
inject([UserService], (service: UserService) => {
91+
expect(service).toBe(componentUserService);
92+
}));
93+
// #enddocregion inject-it
94+
95+
it('TestBed and Component UserService should be the same', () => {
96+
expect(userService === componentUserService).toBe(true);
97+
});
98+
99+
// #docregion stub-not-injected
100+
it('stub object and injected UserService should not be the same', () => {
90101
expect(userServiceStub === userService).toBe(false);
102+
103+
// Changing the stub object has no effect on the injected service
104+
userServiceStub.isLoggedIn = false;
105+
expect(userService.isLoggedIn).toBe(true);
91106
});
107+
// #enddocregion stub-not-injected
92108
});

public/docs/dart/latest/cookbook/_data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
"ts-to-js": {
6262
"title": "TypeScript to JavaScript",
63-
"intro": "Convert Angular 2 TypeScript examples into ES5 JavaScript",
63+
"intro": "Convert Angular TypeScript examples into ES5 JavaScript",
6464
"hide": true
6565
},
6666

public/docs/dart/latest/guide/_data.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"architecture": {
1111
"title": "Architecture Overview",
1212
"navTitle": "Architecture",
13-
"intro": "The basic building blocks of Angular 2 applications",
13+
"intro": "The basic building blocks of Angular applications",
1414
"nextable": true,
1515
"basics": true
1616
},
@@ -59,7 +59,7 @@
5959

6060
"style-guide": {
6161
"title": "Style Guide",
62-
"intro": "Write Angular 2 with style.",
62+
"intro": "Write Angular with style.",
6363
"basics": true
6464
},
6565

@@ -92,7 +92,7 @@
9292

9393
"glossary": {
9494
"title": "Glossary",
95-
"intro": "Brief definitions of the most important words in the Angular 2 vocabulary",
95+
"intro": "Brief definitions of the most important words in the Angular vocabulary",
9696
"basics": true
9797
},
9898

@@ -125,7 +125,7 @@
125125

126126
"router": {
127127
"title": "Routing & Navigation",
128-
"intro": "Discover the basics of screen navigation with the Angular 2 Component Router."
128+
"intro": "Discover the basics of screen navigation with the Angular Component Router."
129129
},
130130

131131
"security": {
@@ -140,13 +140,13 @@
140140

141141
"testing": {
142142
"title": "Testing",
143-
"intro": "Techniques and practices for testing an Angular 2 app",
143+
"intro": "Techniques and practices for testing an Angular app",
144144
"hide": true
145145
},
146146

147147
"typescript-configuration": {
148148
"title": "TypeScript Configuration",
149-
"intro": "TypeScript configuration for Angular 2 developers",
149+
"intro": "TypeScript configuration for Angular developers",
150150
"hide": true
151151
},
152152

@@ -158,7 +158,7 @@
158158

159159
"webpack": {
160160
"title": "Webpack: an introduction",
161-
"intro": "Create your Angular 2 applications with a Webpack based tooling",
161+
"intro": "Create your Angular applications with a Webpack based tooling",
162162
"hide": true
163163
}
164164
}

0 commit comments

Comments
 (0)