feat(router): implement CandDeactivate
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
import {DefaultUrlSerializer} from '../src/url_serializer';
|
||||
import {UrlTree} from '../src/url_tree';
|
||||
import {TreeNode} from '../src/utils/tree';
|
||||
import {Params, PRIMARY_OUTLET} from '../src/shared';
|
||||
import {ActivatedRoute, ActivatedRouteSnapshot, RouterStateSnapshot, createEmptyState} from '../src/router_state';
|
||||
import {ActivatedRoute, RouterState, RouterStateSnapshot, createEmptyState, advanceActivatedRoute} from '../src/router_state';
|
||||
import {createRouterState} from '../src/create_router_state';
|
||||
import {recognize} from '../src/recognize';
|
||||
import {RouterConfig} from '../src/config';
|
||||
@ -32,6 +33,7 @@ describe('create router state', () => {
|
||||
];
|
||||
|
||||
const prevState = createRouterState(createState(config, "a(left:b)"), emptyState());
|
||||
advanceState(prevState);
|
||||
const state = createRouterState(createState(config, "a(left:c)"), prevState);
|
||||
|
||||
expect(prevState.root).toBe(state.root);
|
||||
@ -44,6 +46,15 @@ describe('create router state', () => {
|
||||
});
|
||||
});
|
||||
|
||||
function advanceState(state: RouterState): void {
|
||||
advanceNode(state._root);
|
||||
}
|
||||
|
||||
function advanceNode(node: TreeNode<ActivatedRoute>): void {
|
||||
advanceActivatedRoute(node.value);
|
||||
node.children.forEach(advanceNode);
|
||||
}
|
||||
|
||||
function createState(config: RouterConfig, url: string): RouterStateSnapshot {
|
||||
let res;
|
||||
recognize(RootComponent, config, tree(url)).forEach(s => res = s);
|
||||
|
@ -1,8 +1,9 @@
|
||||
import {DefaultUrlSerializer} from '../src/url_serializer';
|
||||
import {UrlTree, UrlSegment} from '../src/url_tree';
|
||||
import {ActivatedRoute, ActivatedRouteSnapshot} from '../src/router_state';
|
||||
import {ActivatedRoute, ActivatedRouteSnapshot, advanceActivatedRoute} from '../src/router_state';
|
||||
import {PRIMARY_OUTLET, Params} from '../src/shared';
|
||||
import {createUrlTree} from '../src/create_url_tree';
|
||||
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
|
||||
|
||||
describe('createUrlTree', () => {
|
||||
const serializer = new DefaultUrlSerializer();
|
||||
@ -174,7 +175,8 @@ function create(start: UrlSegment | null, tree: UrlTree, commands: any[], queryP
|
||||
if (!start) {
|
||||
expect(start).toBeDefined();
|
||||
}
|
||||
const s = new ActivatedRouteSnapshot([], <any>null, PRIMARY_OUTLET, "someComponent", null, <any>start);
|
||||
const a = new ActivatedRoute(<any>null, <any>null, PRIMARY_OUTLET, "someComponent", s);
|
||||
const s = new ActivatedRouteSnapshot([], <any>{}, PRIMARY_OUTLET, "someComponent", null, <any>start);
|
||||
const a = new ActivatedRoute(new BehaviorSubject(null), new BehaviorSubject(null), PRIMARY_OUTLET, "someComponent", s);
|
||||
advanceActivatedRoute(a);
|
||||
return createUrlTree(a, tree, commands, queryParameters, fragment);
|
||||
}
|
@ -2,6 +2,7 @@ import {Component, Injector} from '@angular/core';
|
||||
import {
|
||||
describe,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
it,
|
||||
iit,
|
||||
xit,
|
||||
@ -16,7 +17,8 @@ import {
|
||||
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
||||
import { ComponentResolver } from '@angular/core';
|
||||
import { SpyLocation } from '@angular/common/testing';
|
||||
import { UrlSerializer, DefaultUrlSerializer, RouterOutletMap, Router, ActivatedRoute, ROUTER_DIRECTIVES, Params } from '../src/index';
|
||||
import { UrlSerializer, DefaultUrlSerializer, RouterOutletMap, Router, ActivatedRoute, ROUTER_DIRECTIVES, Params,
|
||||
RouterStateSnapshot, ActivatedRouteSnapshot } from '../src/index';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import 'rxjs/add/operator/map';
|
||||
|
||||
@ -328,7 +330,7 @@ describe("Integration", () => {
|
||||
|
||||
describe("should activate a route when CanActivate returns true", () => {
|
||||
beforeEachProviders(() => [
|
||||
{provide: 'alwaysFalse', useValue: (a, b) => true}
|
||||
{provide: 'alwaysFalse', useValue: (a:ActivatedRouteSnapshot, s:RouterStateSnapshot) => true}
|
||||
]);
|
||||
|
||||
it('works',
|
||||
@ -347,6 +349,42 @@ describe("Integration", () => {
|
||||
})));
|
||||
});
|
||||
});
|
||||
|
||||
describe("CanDeactivate", () => {
|
||||
describe("should not deactivate a route when CanDeactivate returns false", () => {
|
||||
beforeEachProviders(() => [
|
||||
{provide: 'CanDeactivate', useValue: (c:TeamCmp, a:ActivatedRouteSnapshot, b:RouterStateSnapshot) => {
|
||||
return c.route.snapshot.params['id'] === "22";
|
||||
}}
|
||||
]);
|
||||
|
||||
|
||||
it('works',
|
||||
fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
|
||||
router.resetConfig([
|
||||
{ path: 'team/:id', component: TeamCmp, canDeactivate: ["CanDeactivate"] }
|
||||
]);
|
||||
|
||||
const fixture = tcb.createFakeAsync(RootCmp);
|
||||
advance(fixture);
|
||||
|
||||
router.navigateByUrl('/team/22');
|
||||
advance(fixture);
|
||||
|
||||
expect(location.path()).toEqual('/team/22');
|
||||
|
||||
router.navigateByUrl('/team/33');
|
||||
advance(fixture);
|
||||
|
||||
expect(location.path()).toEqual('/team/33');
|
||||
|
||||
router.navigateByUrl('/team/44');
|
||||
advance(fixture);
|
||||
|
||||
expect(location.path()).toEqual('/team/33');
|
||||
})));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -388,7 +426,7 @@ class TeamCmp {
|
||||
id: Observable<string>;
|
||||
recordedParams: Params[] = [];
|
||||
|
||||
constructor(route: ActivatedRoute) {
|
||||
constructor(public route: ActivatedRoute) {
|
||||
this.id = route.params.map(p => p['id']);
|
||||
route.params.forEach(_ => this.recordedParams.push(_));
|
||||
}
|
||||
|
Reference in New Issue
Block a user