2016-08-02 17:34:00 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-10-28 18:17:00 -04:00
|
|
|
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, equalParamsAndUrlSegments} from '../src/router_state';
|
|
|
|
import {Params} from '../src/shared';
|
|
|
|
import {UrlSegment} from '../src/url_tree';
|
2016-08-02 17:34:00 -04:00
|
|
|
import {TreeNode} from '../src/utils/tree';
|
|
|
|
|
|
|
|
describe('RouterState & Snapshot', () => {
|
|
|
|
describe('RouterStateSnapshot', () => {
|
|
|
|
let state: RouterStateSnapshot;
|
|
|
|
let a: ActivatedRouteSnapshot;
|
|
|
|
let b: ActivatedRouteSnapshot;
|
|
|
|
let c: ActivatedRouteSnapshot;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
a = createActivatedRouteSnapshot('a');
|
|
|
|
b = createActivatedRouteSnapshot('b');
|
|
|
|
c = createActivatedRouteSnapshot('c');
|
|
|
|
|
|
|
|
const root = new TreeNode(a, [new TreeNode(b, []), new TreeNode(c, [])]);
|
|
|
|
|
2016-08-02 18:31:56 -04:00
|
|
|
state = new RouterStateSnapshot('url', root);
|
2016-08-02 17:34:00 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return first child', () => { expect(state.root.firstChild).toBe(b); });
|
|
|
|
|
|
|
|
it('should return children', () => {
|
|
|
|
const cc = state.root.children;
|
|
|
|
expect(cc[0]).toBe(b);
|
|
|
|
expect(cc[1]).toBe(c);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return root', () => {
|
2016-08-07 23:09:04 -04:00
|
|
|
const b = state.root.firstChild;
|
|
|
|
expect(b.root).toBe(state.root);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return parent', () => {
|
2016-08-02 17:34:00 -04:00
|
|
|
const b = state.root.firstChild;
|
|
|
|
expect(b.parent).toBe(state.root);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return path from root', () => {
|
|
|
|
const b = state.root.firstChild;
|
|
|
|
const p = b.pathFromRoot;
|
|
|
|
expect(p[0]).toBe(state.root);
|
|
|
|
expect(p[1]).toBe(b);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('RouterState', () => {
|
|
|
|
let state: RouterState;
|
|
|
|
let a: ActivatedRoute;
|
|
|
|
let b: ActivatedRoute;
|
|
|
|
let c: ActivatedRoute;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
a = createActivatedRoute('a');
|
|
|
|
b = createActivatedRoute('b');
|
|
|
|
c = createActivatedRoute('c');
|
|
|
|
|
|
|
|
const root = new TreeNode(a, [new TreeNode(b, []), new TreeNode(c, [])]);
|
|
|
|
|
2016-08-02 18:31:56 -04:00
|
|
|
state = new RouterState(root, <any>null);
|
2016-08-02 17:34:00 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return first child', () => { expect(state.root.firstChild).toBe(b); });
|
|
|
|
|
|
|
|
it('should return children', () => {
|
|
|
|
const cc = state.root.children;
|
|
|
|
expect(cc[0]).toBe(b);
|
|
|
|
expect(cc[1]).toBe(c);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return root', () => {
|
2016-08-07 23:09:04 -04:00
|
|
|
const b = state.root.firstChild;
|
|
|
|
expect(b.root).toBe(state.root);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return parent', () => {
|
2016-08-02 17:34:00 -04:00
|
|
|
const b = state.root.firstChild;
|
|
|
|
expect(b.parent).toBe(state.root);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return path from root', () => {
|
|
|
|
const b = state.root.firstChild;
|
|
|
|
const p = b.pathFromRoot;
|
|
|
|
expect(p[0]).toBe(state.root);
|
|
|
|
expect(p[1]).toBe(b);
|
|
|
|
});
|
|
|
|
});
|
2016-10-28 18:17:00 -04:00
|
|
|
|
|
|
|
describe('equalParamsAndUrlSegments', () => {
|
|
|
|
function createSnapshot(params: Params, url: UrlSegment[]): ActivatedRouteSnapshot {
|
|
|
|
return new ActivatedRouteSnapshot(
|
|
|
|
url, params, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null,
|
|
|
|
-1, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should return false when params are different', () => {
|
|
|
|
expect(equalParamsAndUrlSegments(createSnapshot({a: 1}, []), createSnapshot({a: 2}, [])))
|
|
|
|
.toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false when urls are different', () => {
|
|
|
|
expect(equalParamsAndUrlSegments(
|
|
|
|
createSnapshot({a: 1}, [new UrlSegment('a', {})]),
|
|
|
|
createSnapshot({a: 1}, [new UrlSegment('b', {})])))
|
|
|
|
.toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true othewise', () => {
|
|
|
|
expect(equalParamsAndUrlSegments(
|
|
|
|
createSnapshot({a: 1}, [new UrlSegment('a', {})]),
|
|
|
|
createSnapshot({a: 1}, [new UrlSegment('a', {})])))
|
|
|
|
.toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
2016-08-02 17:34:00 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
function createActivatedRouteSnapshot(cmp: string) {
|
|
|
|
return new ActivatedRouteSnapshot(
|
2016-08-02 18:31:56 -04:00
|
|
|
<any>null, <any>null, <any>null, <any>null, <any>null, <any>null, <any>cmp, <any>null,
|
|
|
|
<any>null, -1, null);
|
2016-08-02 17:34:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function createActivatedRoute(cmp: string) {
|
2016-08-02 18:31:56 -04:00
|
|
|
return new ActivatedRoute(
|
|
|
|
<any>null, <any>null, <any>null, <any>null, <any>null, <any>null, <any>cmp, <any>null);
|
2016-08-02 17:34:00 -04:00
|
|
|
}
|