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

Commit 2951183

Browse files
committed
docs(server-communication): heavily refactored (TS & Dart)
1 parent a4bc455 commit 2951183

27 files changed

+388
-304
lines changed

public/docs/_examples/server-communication/dart/example-config.json

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// #docplaster
2+
// #docregion
3+
import "package:angular2/core.dart" show Component;
4+
5+
import "toh/hero_list_component.dart" show HeroListComponent;
6+
import "wiki/wiki_component.dart" show WikiComponent;
7+
import "wiki/wiki_smart_component.dart" show WikiSmartComponent;
8+
9+
@Component(
10+
selector: "my-app",
11+
template: '''
12+
<hero-list></hero-list>
13+
<my-wiki></my-wiki>
14+
<my-wiki-smart></my-wiki-smart>
15+
''',
16+
// #enddocregion
17+
/*
18+
// #docregion http-providers
19+
providers: const [
20+
// in-memory web api provider
21+
const Provider(BrowserClient,
22+
useFactory: HttpClientBackendServiceFactory, deps: const [])],
23+
// #enddocregion http-providers
24+
*/
25+
// #docregion
26+
directives: const [
27+
HeroListComponent,
28+
WikiComponent,
29+
WikiSmartComponent
30+
])
31+
class AppComponent {}

public/docs/_examples/server-communication/dart/lib/hero_data.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import 'package:http/browser_client.dart';
33
import 'package:http_in_memory_web_api/http_in_memory_web_api.dart';
44

5-
CreateDb createDb = () => {
5+
CreateDb _createDb = () => {
66
'heroes': [
77
{"id": "1", "name": "Windstorm"},
88
{"id": "2", "name": "Bombasto"},
@@ -12,4 +12,4 @@ CreateDb createDb = () => {
1212
};
1313

1414
BrowserClient HttpClientBackendServiceFactory() =>
15-
new HttpClientInMemoryBackendService(createDb);
15+
new HttpClientInMemoryBackendService(_createDb);

public/docs/_examples/server-communication/dart/lib/toh/hero_list_component.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'hero_service.dart';
99
@Component(
1010
selector: 'hero-list',
1111
templateUrl: 'hero_list_component.html',
12-
styles: const ['.error {color:red;}'])
12+
providers: const [HeroService])
1313
// #docregion component
1414
class HeroListComponent implements OnInit {
1515
final HeroService _heroService;

public/docs/_examples/server-communication/dart/lib/toh/hero_list_component.html

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<!-- #docregion -->
2+
<h1>Tour of Heroes</h1>
23
<h3>Heroes:</h3>
34
<ul>
45
<li *ngFor="let hero of heroes">

public/docs/_examples/server-communication/dart/lib/toh/hero_service.dart

+3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class HeroService {
1515
// #enddocregion endpoint, http-get
1616
final BrowserClient _http;
1717

18+
// #docregion ctor
1819
HeroService(this._http);
20+
// #enddocregion ctor
1921

2022
// #docregion methods, error-handling, http-get
2123
Future<List<Hero>> getHeroes() async {
@@ -57,6 +59,7 @@ class HeroService {
5759

5860
Exception _handleError(dynamic e) {
5961
// In a real world app, we might use a remote logging infrastructure
62+
// We'd also dig deeper into the error to get a better message
6063
print(e); // log to console instead
6164
return new Exception('Server error; cause: $e');
6265
}

public/docs/_examples/server-communication/dart/lib/toh/toh_component.dart

-36
This file was deleted.

public/docs/_examples/server-communication/dart/pubspec.yaml

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ dependencies:
1212
jsonpadding: ^0.1.0
1313
stream_transformers: ^0.3.0+3
1414
http_in_memory_web_api: ^0.0.1
15+
# #docregion transformers
1516
transformers:
1617
- angular2:
17-
platform_directives: 'package:angular2/common.dart#CORE_DIRECTIVES'
18-
platform_pipes: 'package:angular2/common.dart#COMMON_PIPES'
18+
platform_directives:
19+
- 'package:angular2/common.dart#CORE_DIRECTIVES'
20+
platform_pipes:
21+
- 'package:angular2/common.dart#COMMON_PIPES'
1922
entry_points: 'web/main.dart'
2023
resolved_identifiers:
2124
BrowserClient: 'package:http/browser_client.dart'
2225
- dart_to_js_script_rewriter
26+
# #enddocregion transformers

public/docs/_examples/server-communication/dart/web/index.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
<meta charset="UTF-8">
77
<meta name="viewport" content="width=device-width, initial-scale=1">
88
<link rel="stylesheet" href="styles.css">
9+
<link rel="stylesheet" href="sample.css">
910

1011
<script defer src="main.dart" type="application/dart"></script>
1112
<script defer src="packages/browser/dart.js"></script>
1213
</head>
1314

1415
<body>
15-
<my-toh>ToH Loading...</my-toh>
16-
<my-wiki>Wiki Loading...</my-wiki>
17-
<my-wiki-smart>WikiSmart Loading...</my-wiki-smart>
16+
<my-app>Loading...</my-app>
1817
</body>
1918

2019
</html>
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1-
// #docregion
2-
import 'package:angular2/platform/browser.dart';
1+
// #docplaster
2+
// #docregion final
3+
import 'package:angular2/core.dart' show Provider;
4+
// #docregion v1
5+
import 'package:angular2/platform/browser.dart' show bootstrap;
6+
// #docregion http-providers
7+
import 'package:http/browser_client.dart' show BrowserClient;
8+
// #enddocregion http-providers
39

4-
import 'package:server_communication/toh/toh_component.dart';
5-
import 'package:server_communication/wiki/wiki_component.dart';
6-
import 'package:server_communication/wiki/wiki_smart_component.dart';
10+
import 'package:server_communication/app_component.dart';
11+
// #enddocregion v1
12+
// #docregion in-mem-web-api-imports
13+
import "package:server_communication/hero_data.dart";
714

8-
main() {
9-
bootstrap(TohComponent);
10-
bootstrap(WikiComponent);
11-
bootstrap(WikiSmartComponent);
15+
// #enddocregion in-mem-web-api-imports
16+
// #docregion in-mem-web-api-providers
17+
void main() {
18+
bootstrap(AppComponent, const [
19+
// in-memory web api provider
20+
const Provider(BrowserClient,
21+
useFactory: HttpClientBackendServiceFactory, deps: const [])
22+
// TODO: drop `deps` once fix lands for
23+
// https://github.com/angular/angular/issues/5266
24+
]);
1225
}
26+
// #enddocregion final, in-mem-web-api-providers
27+
/*
28+
// #docregion v1
29+
30+
void main() {
31+
bootstrap(AppComponent, const [BrowserClient]);
32+
}
33+
// #enddocregion v1
34+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.error {color:red;}

public/docs/_examples/server-communication/e2e-spec.js

+40-47
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,62 @@ describe('Server Communication', function () {
44
browser.get('');
55
});
66

7-
describe('Tour of Heroes e2e tests', function () {
8-
9-
var _initialHeroCount = 4;
10-
var _newHeroName = 'Mr. IQ';
11-
var _heroCountAfterAdd = 5;
12-
13-
it('should display ' + _initialHeroCount + ' heroes after init', function () {
14-
var myTohComp = element(by.tagName('my-toh'));
15-
expect(myTohComp).toBeDefined('<my-toh> must exist');
16-
var heroListComp = myTohComp.element(by.tagName('hero-list'));
7+
describe('Tour of Heroes (Observable)', function () {
8+
9+
var initialHeroCount = 4;
10+
var newHeroName = 'Mr. IQ';
11+
var heroCountAfterAdd = 5;
12+
13+
var heroListComp = element(by.tagName('hero-list'));
14+
var addButton = heroListComp.element(by.tagName('button'));
15+
var heroTags = heroListComp.all(by.tagName('li'));
16+
var heroNameInput = heroListComp.element(by.tagName('input'));
17+
18+
it('should exist', function() {
1719
expect(heroListComp).toBeDefined('<hero-list> must exist');
18-
var heroTags = heroListComp.all(by.tagName('li'));
19-
expect(heroTags.count()).toBe(_initialHeroCount);
2020
});
21-
21+
22+
it('should display ' + initialHeroCount + ' heroes after init', function () {
23+
expect(heroTags.count()).toBe(initialHeroCount);
24+
});
25+
2226
it('should not add hero with empty name', function () {
23-
var myTohComp = element(by.tagName('my-toh'));
24-
expect(myTohComp).toBeDefined('<my-toh> must exist');
25-
var addButton = myTohComp.element(by.tagName('button'));
2627
expect(addButton).toBeDefined('"Add Hero" button must be defined');
2728
addButton.click().then(function() {
28-
var heroListComp = myTohComp.element(by.tagName('hero-list'));
29-
var heroTags = heroListComp.all(by.tagName('li'));
30-
expect(heroTags.count()).toBe(_initialHeroCount, 'No new hero should be added');
29+
expect(heroTags.count()).toBe(initialHeroCount, 'No new hero should be added');
3130
});
3231
})
3332

3433
it('should add a new hero to the list', function () {
35-
var myTohComp = element(by.tagName('my-toh'));
36-
expect(myTohComp).toBeDefined('<my-toh> must exist');
37-
var heroNameInput = myTohComp.element(by.tagName('input'));
3834
expect(heroNameInput).toBeDefined('<input> for hero name must exist');
39-
var addButton = myTohComp.element(by.tagName('button'));
4035
expect(addButton).toBeDefined('"Add Hero" button must be defined');
41-
sendKeys(heroNameInput, _newHeroName);
36+
sendKeys(heroNameInput, newHeroName);
4237
addButton.click().then(function() {
43-
var heroListComp = myTohComp.element(by.tagName('hero-list'));
44-
var heroTags = heroListComp.all(by.tagName('li'));
45-
expect(heroTags.count()).toBe(_heroCountAfterAdd, 'A new hero should be added');
46-
var newHeroInList = heroTags.get(_heroCountAfterAdd - 1).getText();
47-
expect(newHeroInList).toBe(_newHeroName, 'The hero should be added to the end of the list');
38+
expect(heroTags.count()).toBe(heroCountAfterAdd, 'A new hero should be added');
39+
var newHeroInList = heroTags.get(heroCountAfterAdd - 1).getText();
40+
expect(newHeroInList).toBe(newHeroName, 'The hero should be added to the end of the list');
4841
});
4942
})
5043
});
51-
52-
describe('Wikipedia Demo e2e tests', function () {
53-
44+
45+
describe('Wikipedia Demo', function () {
46+
5447
it('should initialize the demo with empty result list', function () {
5548
var myWikiComp = element(by.tagName('my-wiki'));
5649
expect(myWikiComp).toBeDefined('<my-wiki> must exist');
5750
var resultList = myWikiComp.all(by.tagName('li'));
5851
expect(resultList.count()).toBe(0, 'result list must be empty');
5952
});
60-
53+
6154
describe('Fetches after each keystroke', function () {
6255
it('should fetch results after "B"', function(done) {
6356
testForRefreshedResult('B', done);
6457
});
65-
58+
6659
it('should fetch results after "Ba"', function(done) {
6760
testForRefreshedResult('a', done);
6861
});
69-
62+
7063
it('should fetch results after "Bas"', function(done) {
7164
testForRefreshedResult('s', done);
7265
});
@@ -75,13 +68,13 @@ describe('Server Communication', function () {
7568
testForRefreshedResult('ic', done);
7669
});
7770
});
78-
71+
7972
function testForRefreshedResult(keyPressed, done) {
8073
testForResult('my-wiki', keyPressed, false, done)
8174
}
8275
});
83-
84-
describe('Smarter Wikipedia Demo e2e tests', function () {
76+
77+
describe('Smarter Wikipedia Demo', function () {
8578

8679
it('should initialize the demo with empty result list', function () {
8780
var myWikiSmartComp = element(by.tagName('my-wiki-smart'));
@@ -93,40 +86,40 @@ describe('Server Communication', function () {
9386
it('should fetch results after "Java"', function(done) {
9487
testForNewResult('Java', done);
9588
});
96-
89+
9790
it('should fetch results after "JavaS"', function(done) {
9891
testForStaleResult('S', done);
9992
});
100-
93+
10194
it('should fetch results after "JavaSc"', function(done) {
10295
testForStaleResult('c', done);
10396
});
104-
97+
10598
it('should fetch results after "JavaScript"', function(done) {
10699
testForStaleResult('ript', done);
107100
});
108101

109-
102+
110103
function testForNewResult(keyPressed, done) {
111104
testForResult('my-wiki-smart', keyPressed, false, done)
112105
}
113106

114107
function testForStaleResult(keyPressed, done) {
115-
testForResult('my-wiki-smart', keyPressed, true, done)
108+
testForResult('my-wiki-smart', keyPressed, true, done)
116109
}
117110

118111
});
119-
112+
120113
function testForResult(componentTagName, keyPressed, hasListBeforeSearch, done) {
121114
var searchWait = 1000; // Wait for wikipedia but not so long that tests timeout
122115
var wikiComponent = element(by.tagName(componentTagName));
123116
expect(wikiComponent).toBeDefined('<' + componentTagName + '> must exist');
124117
var searchBox = wikiComponent.element(by.tagName('input'));
125118
expect(searchBox).toBeDefined('<input> for search must exist');
126-
119+
127120
searchBox.sendKeys(keyPressed).then(function () {
128121
var resultList = wikiComponent.all(by.tagName('li'));
129-
122+
130123
if (hasListBeforeSearch) {
131124
expect(resultList.count()).toBeGreaterThan(0, 'result list should not be empty before search');
132125
}
@@ -137,5 +130,5 @@ describe('Server Communication', function () {
137130
}, searchWait);
138131
});
139132
}
140-
133+
141134
});

public/docs/_examples/server-communication/ts/app/add-rxjs-operators.ts

-9
This file was deleted.

0 commit comments

Comments
 (0)