Skip to content

Commit 0f1b370

Browse files
juliemrmprobst
authored andcommitted
feat(tests): add ROUTER_FAKE_PROVIDERS to angular2/alt_router/router_testing_providers
This change adds providers for fake router dependecies. This allows TestComponentBuilder to create components with RouterLink and RouterOutlet directives without the test writer needing to override them.
1 parent e589f99 commit 0f1b370

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {SpyLocation} from 'angular2/src/mock/location_mock';
2+
import {Location} from 'angular2/platform/common';
3+
import {Router, RouterOutletMap} from './router';
4+
import {RouterUrlSerializer, DefaultRouterUrlSerializer} from './router_url_serializer';
5+
import {Component, ComponentResolver} from 'angular2/core';
6+
7+
@Component({selector: 'fake-app-root-comp', template: `<span></span>`})
8+
class FakeAppRootCmp {
9+
}
10+
11+
function routerFactory(componentResolver: ComponentResolver, urlSerializer: RouterUrlSerializer,
12+
routerOutletMap: RouterOutletMap, location: Location): Router {
13+
return new Router(null, FakeAppRootCmp, componentResolver, urlSerializer, routerOutletMap,
14+
location);
15+
}
16+
17+
export const ROUTER_FAKE_PROVIDERS: any[] = /*@ts2dart_const*/ [
18+
RouterOutletMap,
19+
/* @ts2dart_Provider */ {provide: Location, useClass: SpyLocation},
20+
/* @ts2dart_Provider */ {provide: RouterUrlSerializer, useClass: DefaultRouterUrlSerializer},
21+
/* @ts2dart_Provider */ {
22+
provide: Router,
23+
useFactory: routerFactory,
24+
deps: /*@ts2dart_const*/
25+
[ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location]
26+
},
27+
];

modules/angular2/test/testing/testing_public_browser_spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import {
1515
tick
1616
} from 'angular2/testing';
1717

18+
import {ROUTER_FAKE_PROVIDERS} from 'angular2/src/alt_router/router_testing_providers';
19+
import {ROUTER_DIRECTIVES, Routes, Route} from 'angular2/alt_router';
20+
21+
1822
import {Injectable, bind, Directive, Component, ViewMetadata} from 'angular2/core';
1923
import {PromiseWrapper} from 'angular2/src/facade/promise';
2024
import {XHR} from 'angular2/src/compiler/xhr';
@@ -40,6 +44,18 @@ class ExternalTemplateComp {
4044
class BadTemplateUrl {
4145
}
4246

47+
@Component({
48+
selector: 'test-router-cmp',
49+
template: `<a [routerLink]="['One']">one</a> <a [routerLink]="['Two']">two</a><router-outlet></router-outlet>`,
50+
directives: [ROUTER_DIRECTIVES]
51+
})
52+
@Routes([
53+
new Route({path: '/One', component: BadTemplateUrl}),
54+
new Route({path: '/Two', component: ExternalTemplateComp}),
55+
])
56+
class TestRouterComponent {
57+
}
58+
4359
// Tests for angular2/testing bundle specific to the browser environment.
4460
// For general tests, see test/testing/testing_public_spec.ts.
4561
export function main() {
@@ -161,4 +177,14 @@ export function main() {
161177
10000); // Long timeout here because this test makes an actual XHR, and is slow on Edge.
162178
});
163179
});
180+
181+
describe('apps with router components', () => {
182+
beforeEachProviders(() => [ROUTER_FAKE_PROVIDERS]);
183+
184+
it('should build without a problem',
185+
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
186+
tcb.createAsync(TestRouterComponent)
187+
.then((fixture) => { expect(fixture.nativeElement).toHaveText('one two'); });
188+
})));
189+
});
164190
}

0 commit comments

Comments
 (0)