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.
This commit is contained in:
Julie Ralph 2016-04-26 20:51:10 -07:00 committed by Martin Probst
parent e589f9949b
commit 0f1b370117
2 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,27 @@
import {SpyLocation} from 'angular2/src/mock/location_mock';
import {Location} from 'angular2/platform/common';
import {Router, RouterOutletMap} from './router';
import {RouterUrlSerializer, DefaultRouterUrlSerializer} from './router_url_serializer';
import {Component, ComponentResolver} from 'angular2/core';
@Component({selector: 'fake-app-root-comp', template: `<span></span>`})
class FakeAppRootCmp {
}
function routerFactory(componentResolver: ComponentResolver, urlSerializer: RouterUrlSerializer,
routerOutletMap: RouterOutletMap, location: Location): Router {
return new Router(null, FakeAppRootCmp, componentResolver, urlSerializer, routerOutletMap,
location);
}
export const ROUTER_FAKE_PROVIDERS: any[] = /*@ts2dart_const*/ [
RouterOutletMap,
/* @ts2dart_Provider */ {provide: Location, useClass: SpyLocation},
/* @ts2dart_Provider */ {provide: RouterUrlSerializer, useClass: DefaultRouterUrlSerializer},
/* @ts2dart_Provider */ {
provide: Router,
useFactory: routerFactory,
deps: /*@ts2dart_const*/
[ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location]
},
];

View File

@ -15,6 +15,10 @@ import {
tick
} from 'angular2/testing';
import {ROUTER_FAKE_PROVIDERS} from 'angular2/src/alt_router/router_testing_providers';
import {ROUTER_DIRECTIVES, Routes, Route} from 'angular2/alt_router';
import {Injectable, bind, Directive, Component, ViewMetadata} from 'angular2/core';
import {PromiseWrapper} from 'angular2/src/facade/promise';
import {XHR} from 'angular2/src/compiler/xhr';
@ -40,6 +44,18 @@ class ExternalTemplateComp {
class BadTemplateUrl {
}
@Component({
selector: 'test-router-cmp',
template: `<a [routerLink]="['One']">one</a> <a [routerLink]="['Two']">two</a><router-outlet></router-outlet>`,
directives: [ROUTER_DIRECTIVES]
})
@Routes([
new Route({path: '/One', component: BadTemplateUrl}),
new Route({path: '/Two', component: ExternalTemplateComp}),
])
class TestRouterComponent {
}
// Tests for angular2/testing bundle specific to the browser environment.
// For general tests, see test/testing/testing_public_spec.ts.
export function main() {
@ -161,4 +177,14 @@ export function main() {
10000); // Long timeout here because this test makes an actual XHR, and is slow on Edge.
});
});
describe('apps with router components', () => {
beforeEachProviders(() => [ROUTER_FAKE_PROVIDERS]);
it('should build without a problem',
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
tcb.createAsync(TestRouterComponent)
.then((fixture) => { expect(fixture.nativeElement).toHaveText('one two'); });
})));
});
}