X Tutup
Skip to content

Commit 929abb9

Browse files
committed
chore(router): add tests for interaction between router-outer and
@ViewChild. Closes angular#5164
1 parent 837efc2 commit 929abb9

File tree

1 file changed

+94
-43
lines changed

1 file changed

+94
-43
lines changed

modules/angular2/test/router/integration/router_integration_spec.ts

Lines changed: 94 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
AsyncTestCompleter,
33
beforeEach,
4-
beforeEachBindings,
4+
beforeEachProviders,
55
ddescribe,
66
describe,
77
expect,
@@ -17,9 +17,9 @@ import {
1717
import {bootstrap} from 'angular2/bootstrap';
1818
import {Component, Directive, View} from 'angular2/src/core/metadata';
1919
import {DOM} from 'angular2/src/core/dom/dom_adapter';
20-
import {provide} from 'angular2/core';
20+
import {provide, ViewChild, AfterViewInit} from 'angular2/core';
2121
import {DOCUMENT} from 'angular2/src/platform/dom/dom_tokens';
22-
import {RouteConfig, Route, Redirect} from 'angular2/src/router/route_config_decorator';
22+
import {RouteConfig, Route, Redirect, AuxRoute} from 'angular2/src/router/route_config_decorator';
2323
import {PromiseWrapper} from 'angular2/src/facade/async';
2424
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
2525
import {
@@ -39,7 +39,7 @@ import {MockApplicationRef} from 'angular2/src/mock/mock_application_ref';
3939

4040
export function main() {
4141
describe('router injectables', () => {
42-
beforeEachBindings(() => {
42+
beforeEachProviders(() => {
4343
return [
4444
ROUTER_PROVIDERS,
4545
provide(LocationStrategy, {useClass: MockLocationStrategy}),
@@ -74,7 +74,7 @@ export function main() {
7474
});
7575

7676
describe('broken app', () => {
77-
beforeEachBindings(
77+
beforeEachProviders(
7878
() => { return [provide(ROUTER_PRIMARY_COMPONENT, {useValue: BrokenAppCmp})]; });
7979

8080
it('should rethrow exceptions from component constructors',
@@ -91,7 +91,7 @@ export function main() {
9191
});
9292

9393
describe('back button app', () => {
94-
beforeEachBindings(
94+
beforeEachProviders(
9595
() => { return [provide(ROUTER_PRIMARY_COMPONENT, {useValue: HierarchyAppCmp})]; });
9696

9797
it('should change the url without pushing a new history state for back navigations',
@@ -102,48 +102,47 @@ export function main() {
102102
var router = fixture.debugElement.componentInstance.router;
103103
var position = 0;
104104
var flipped = false;
105-
var history =
106-
[
107-
['/parent/child', 'root { parent { hello } }', '/super-parent/child'],
108-
['/super-parent/child', 'root { super-parent { hello2 } }', '/parent/child'],
109-
['/parent/child', 'root { parent { hello } }', false]
110-
]
111-
112-
router.subscribe((_) => {
113-
var location = fixture.debugElement.componentInstance.location;
114-
var element = fixture.debugElement.nativeElement;
115-
var path = location.path();
116-
117-
var entry = history[position];
118-
119-
expect(path).toEqual(entry[0]);
120-
expect(element).toHaveText(entry[1]);
121-
122-
var nextUrl = entry[2];
123-
if (nextUrl == false) {
124-
flipped = true;
125-
}
126-
127-
if (flipped && position == 0) {
128-
async.done();
129-
return;
130-
}
131-
132-
position = position + (flipped ? -1 : 1);
133-
if (flipped) {
134-
location.back();
135-
} else {
136-
router.navigateByUrl(nextUrl);
137-
}
138-
});
105+
var history = [
106+
['/parent/child', 'root { parent { hello } }', '/super-parent/child'],
107+
['/super-parent/child', 'root { super-parent { hello2 } }', '/parent/child'],
108+
['/parent/child', 'root { parent { hello } }', false]
109+
];
110+
111+
router.subscribe((_) => {
112+
var location = fixture.debugElement.componentInstance.location;
113+
var element = fixture.debugElement.nativeElement;
114+
var path = location.path();
115+
116+
var entry = history[position];
117+
118+
expect(path).toEqual(entry[0]);
119+
expect(element).toHaveText(entry[1]);
120+
121+
var nextUrl = entry[2];
122+
if (nextUrl == false) {
123+
flipped = true;
124+
}
125+
126+
if (flipped && position == 0) {
127+
async.done();
128+
return;
129+
}
130+
131+
position = position + (flipped ? -1 : 1);
132+
if (flipped) {
133+
location.back();
134+
} else {
135+
router.navigateByUrl(nextUrl);
136+
}
137+
});
139138

140139
router.navigateByUrl(history[0][0]);
141140
});
142141
}), 1000);
143142
});
144143

145144
describe('hierarchical app', () => {
146-
beforeEachBindings(
145+
beforeEachProviders(
147146
() => { return [provide(ROUTER_PRIMARY_COMPONENT, {useValue: HierarchyAppCmp})]; });
148147

149148
it('should bootstrap an app with a hierarchy',
@@ -165,7 +164,7 @@ export function main() {
165164

166165
// TODO(btford): mock out level lower than LocationStrategy once that level exists
167166
xdescribe('custom app base ref', () => {
168-
beforeEachBindings(() => { return [provide(APP_BASE_HREF, {useValue: '/my/app'})]; });
167+
beforeEachProviders(() => { return [provide(APP_BASE_HREF, {useValue: '/my/app'})]; });
169168
it('should bootstrap',
170169
inject([AsyncTestCompleter, TestComponentBuilder],
171170
(async, tcb: TestComponentBuilder) => {
@@ -188,7 +187,7 @@ export function main() {
188187
// TODO: add a test in which the child component has bindings
189188

190189
describe('querystring params app', () => {
191-
beforeEachBindings(
190+
beforeEachProviders(
192191
() => { return [provide(ROUTER_PRIMARY_COMPONENT, {useValue: QueryStringAppCmp})]; });
193192

194193
it('should recognize and return querystring params with the injected RouteParams',
@@ -211,18 +210,49 @@ export function main() {
211210
});
212211
}));
213212
});
213+
214+
describe('retrieving components loaded via outlet via @ViewChild', () => {
215+
let tcb: TestComponentBuilder = null;
216+
217+
beforeEachProviders(() => [provide(ROUTER_PRIMARY_COMPONENT, {useValue: AppCmp})]);
218+
219+
beforeEach(inject([TestComponentBuilder],
220+
(testComponentBuilder) => { tcb = testComponentBuilder; }));
221+
222+
it('should get a reference and pass data to components loaded inside of outlets',
223+
inject([AsyncTestCompleter], (async) => {
224+
tcb.createAsync(AppWithViewChildren)
225+
.then(fixture => {
226+
let appInstance = fixture.debugElement.componentInstance;
227+
let router = appInstance.router;
228+
229+
router.subscribe((_) => {
230+
fixture.detectChanges();
231+
232+
expect(appInstance.helloCmp).toBeAnInstanceOf(HelloCmp);
233+
expect(appInstance.helloCmp.message).toBe('Ahoy');
234+
235+
async.done();
236+
});
237+
238+
router.navigateByUrl('/rainbow(pony)');
239+
});
240+
}));
241+
});
214242
});
215243
}
216244

217245

218246
@Component({selector: 'hello-cmp'})
219247
@View({template: 'hello'})
220248
class HelloCmp {
249+
public message: string;
221250
}
222251

223252
@Component({selector: 'hello2-cmp'})
224253
@View({template: 'hello2'})
225254
class Hello2Cmp {
255+
public greeting: string;
226256
}
227257

228258
@Component({selector: 'app-cmp'})
@@ -232,6 +262,27 @@ class AppCmp {
232262
constructor(public router: Router, public location: LocationStrategy) {}
233263
}
234264

265+
@Component({
266+
selector: 'app-cmp',
267+
template: `
268+
Hello routing!
269+
<router-outlet></router-outlet>
270+
<router-outlet name="pony"></router-outlet>`,
271+
directives: ROUTER_DIRECTIVES
272+
})
273+
@RouteConfig([
274+
new Route({path: '/rainbow', component: HelloCmp}),
275+
new AuxRoute({name: 'pony', path: 'pony', component: Hello2Cmp})
276+
])
277+
class AppWithViewChildren implements AfterViewInit {
278+
@ViewChild(HelloCmp) helloCmp: HelloCmp;
279+
@ViewChild(Hello2Cmp) hello2Cmp: Hello2Cmp;
280+
281+
constructor(public router: Router, public location: LocationStrategy) {}
282+
283+
afterViewInit() { this.helloCmp.message = 'Ahoy'; }
284+
}
285+
235286
@Component({selector: 'parent-cmp'})
236287
@View({template: `parent { <router-outlet></router-outlet> }`, directives: ROUTER_DIRECTIVES})
237288
@RouteConfig([new Route({path: '/child', component: HelloCmp})])

0 commit comments

Comments
 (0)
X Tutup