2016-06-01 16:29:28 -04:00
|
|
|
import {DefaultUrlSerializer} from '../src/url_serializer';
|
2016-06-14 17:55:59 -04:00
|
|
|
import {UrlTree, UrlSegment} from '../src/url_tree';
|
2016-06-02 17:44:57 -04:00
|
|
|
import {TreeNode} from '../src/utils/tree';
|
2016-06-01 16:29:28 -04:00
|
|
|
import {Params, PRIMARY_OUTLET} from '../src/shared';
|
2016-06-02 17:44:57 -04:00
|
|
|
import {ActivatedRoute, RouterState, RouterStateSnapshot, createEmptyState, advanceActivatedRoute} from '../src/router_state';
|
2016-06-01 16:29:28 -04:00
|
|
|
import {createRouterState} from '../src/create_router_state';
|
|
|
|
import {recognize} from '../src/recognize';
|
|
|
|
import {RouterConfig} from '../src/config';
|
|
|
|
|
|
|
|
describe('create router state', () => {
|
2016-06-14 17:55:59 -04:00
|
|
|
const emptyState = () => createEmptyState(new UrlTree(new UrlSegment([], {}), {}, null), RootComponent);
|
2016-06-01 16:29:28 -04:00
|
|
|
|
|
|
|
it('should work create new state', () => {
|
|
|
|
const state = createRouterState(createState([
|
|
|
|
{path: 'a', component: ComponentA},
|
|
|
|
{path: 'b', component: ComponentB, outlet: 'left'},
|
|
|
|
{path: 'c', component: ComponentC, outlet: 'right'}
|
2016-06-01 17:32:15 -04:00
|
|
|
], "a(left:b//right:c)"), emptyState());
|
2016-06-01 16:29:28 -04:00
|
|
|
|
|
|
|
checkActivatedRoute(state.root, RootComponent);
|
|
|
|
|
|
|
|
const c = state.children(state.root);
|
|
|
|
checkActivatedRoute(c[0], ComponentA);
|
|
|
|
checkActivatedRoute(c[1], ComponentB, 'left');
|
|
|
|
checkActivatedRoute(c[2], ComponentC, 'right');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should reuse existing nodes when it can', () => {
|
|
|
|
const config = [
|
|
|
|
{path: 'a', component: ComponentA},
|
|
|
|
{path: 'b', component: ComponentB, outlet: 'left'},
|
|
|
|
{path: 'c', component: ComponentC, outlet: 'left'}
|
|
|
|
];
|
|
|
|
|
2016-06-01 17:32:15 -04:00
|
|
|
const prevState = createRouterState(createState(config, "a(left:b)"), emptyState());
|
2016-06-02 17:44:57 -04:00
|
|
|
advanceState(prevState);
|
2016-06-01 17:32:15 -04:00
|
|
|
const state = createRouterState(createState(config, "a(left:c)"), prevState);
|
2016-06-01 16:29:28 -04:00
|
|
|
|
|
|
|
expect(prevState.root).toBe(state.root);
|
|
|
|
const prevC = prevState.children(prevState.root);
|
|
|
|
const currC = state.children(state.root);
|
|
|
|
|
|
|
|
expect(prevC[0]).toBe(currC[0]);
|
|
|
|
expect(prevC[1]).not.toBe(currC[1]);
|
|
|
|
checkActivatedRoute(currC[1], ComponentC, 'left');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-02 17:44:57 -04:00
|
|
|
function advanceState(state: RouterState): void {
|
|
|
|
advanceNode(state._root);
|
|
|
|
}
|
|
|
|
|
|
|
|
function advanceNode(node: TreeNode<ActivatedRoute>): void {
|
|
|
|
advanceActivatedRoute(node.value);
|
|
|
|
node.children.forEach(advanceNode);
|
|
|
|
}
|
|
|
|
|
2016-06-01 20:55:21 -04:00
|
|
|
function createState(config: RouterConfig, url: string): RouterStateSnapshot {
|
2016-06-15 19:45:19 -04:00
|
|
|
let res: RouterStateSnapshot;
|
2016-06-14 17:55:59 -04:00
|
|
|
recognize(RootComponent, config, tree(url), url).forEach(s => res = s);
|
2016-06-01 16:29:28 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-06-15 12:14:41 -04:00
|
|
|
function checkActivatedRoute(actual: ActivatedRoute, cmp: Function, outlet: string = PRIMARY_OUTLET):void {
|
2016-06-01 16:29:28 -04:00
|
|
|
if (actual === null) {
|
|
|
|
expect(actual).toBeDefined();
|
|
|
|
} else {
|
|
|
|
expect(actual.component).toBe(cmp);
|
|
|
|
expect(actual.outlet).toEqual(outlet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tree(url: string): UrlTree {
|
|
|
|
return new DefaultUrlSerializer().parse(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RootComponent {}
|
|
|
|
class ComponentA {}
|
|
|
|
class ComponentB {}
|
|
|
|
class ComponentC {}
|