Configure router with provided routes

Closes #9
This commit is contained in:
Brandon Roberts 2016-06-02 18:34:04 -05:00 committed by vsavkin
parent 9b094e42a3
commit 793ac3f6b4
3 changed files with 33 additions and 18 deletions

View File

@ -37,8 +37,7 @@ export function provideRouter(config: RouterConfig):any[] {
throw new Error("Bootstrap at least one component before injecting Router."); throw new Error("Bootstrap at least one component before injecting Router.");
} }
const componentType = ref.componentTypes[0]; const componentType = ref.componentTypes[0];
const r = new Router(componentType, resolver, urlSerializer, outletMap, location, injector); const r = new Router(componentType, resolver, urlSerializer, outletMap, location, injector, config);
r.resetConfig(config);
ref.registerDisposeListener(() => r.dispose()); ref.registerDisposeListener(() => r.dispose());
return r; return r;
}, },

View File

@ -54,7 +54,6 @@ export type Event = NavigationStart | NavigationEnd | NavigationCancel | Navigat
export class Router { export class Router {
private currentUrlTree: UrlTree; private currentUrlTree: UrlTree;
private currentRouterState: RouterState; private currentRouterState: RouterState;
private config: RouterConfig;
private locationSubscription: Subscription; private locationSubscription: Subscription;
private routerEvents: Subject<Event>; private routerEvents: Subject<Event>;
private navigationId: number = 0; private navigationId: number = 0;
@ -62,7 +61,7 @@ export class Router {
/** /**
* @internal * @internal
*/ */
constructor(private rootComponentType:Type, private resolver: ComponentResolver, private urlSerializer: UrlSerializer, private outletMap: RouterOutletMap, private location: Location, private injector: Injector) { constructor(private rootComponentType:Type, private resolver: ComponentResolver, private urlSerializer: UrlSerializer, private outletMap: RouterOutletMap, private location: Location, private injector: Injector, private config: RouterConfig) {
this.routerEvents = new Subject<Event>(); this.routerEvents = new Subject<Event>();
this.currentUrlTree = createEmptyUrlTree(); this.currentUrlTree = createEmptyUrlTree();
this.currentRouterState = createEmptyState(rootComponentType); this.currentRouterState = createEmptyState(rootComponentType);

View File

@ -18,23 +18,40 @@ import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing'
import { ComponentResolver } from '@angular/core'; import { ComponentResolver } from '@angular/core';
import { SpyLocation } from '@angular/common/testing'; import { SpyLocation } from '@angular/common/testing';
import { UrlSerializer, DefaultUrlSerializer, RouterOutletMap, Router, ActivatedRoute, ROUTER_DIRECTIVES, Params, import { UrlSerializer, DefaultUrlSerializer, RouterOutletMap, Router, ActivatedRoute, ROUTER_DIRECTIVES, Params,
RouterStateSnapshot, ActivatedRouteSnapshot, CanActivate, CanDeactivate, Event, NavigationStart, NavigationEnd, NavigationCancel, NavigationError } from '../src/index'; RouterStateSnapshot, ActivatedRouteSnapshot, CanActivate, CanDeactivate, Event, NavigationStart, NavigationEnd, NavigationCancel, NavigationError, RouterConfig } from '../src/index';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map'; import 'rxjs/add/operator/map';
describe("Integration", () => { describe("Integration", () => {
beforeEachProviders(() => [
RouterOutletMap, beforeEachProviders(() => {
{provide: UrlSerializer, useClass: DefaultUrlSerializer}, let config: RouterConfig = [
{provide: Location, useClass: SpyLocation}, { path: 'simple', component: SimpleCmp }
{ ];
provide: Router,
useFactory: (resolver, urlSerializer, outletMap, location, injector) => return [
new Router(RootCmp, resolver, urlSerializer, outletMap, location, injector), RouterOutletMap,
deps: [ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector] {provide: UrlSerializer, useClass: DefaultUrlSerializer},
}, {provide: Location, useClass: SpyLocation},
{provide: ActivatedRoute, useFactory: (r) => r.routerState.root, deps: [Router]}, {
]); provide: Router,
useFactory: (resolver, urlSerializer, outletMap, location, injector) =>
new Router(RootCmp, resolver, urlSerializer, outletMap, location, injector, config),
deps: [ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector]
},
{provide: ActivatedRoute, useFactory: (r) => r.routerState.root, deps: [Router]},
];
});
it('should navigate with a provided config',
fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
const fixture = tcb.createFakeAsync(RootCmp);
advance(fixture);
router.navigateByUrl('/simple');
advance(fixture);
expect(location.path()).toEqual('/simple');
})));
it('should update location when navigating', it('should update location when navigating',