Skip to content

Commit

Permalink
feat(tests): add ROUTER_FAKE_PROVIDERS to angular2/alt_router/router_…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
juliemr authored and mprobst committed Apr 30, 2016
1 parent e589f99 commit 0f1b370
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
27 changes: 27 additions & 0 deletions modules/angular2/src/alt_router/router_testing_providers.ts
Original file line number Diff line number Diff line change
@@ -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]
},
];
26 changes: 26 additions & 0 deletions modules/angular2/test/testing/testing_public_browser_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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() {
Expand Down Expand Up @@ -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'); });
})));
});
}

0 comments on commit 0f1b370

Please sign in to comment.