fix(resolve): change resolve not to resolve root activate route

This commit is contained in:
vsavkin 2016-06-19 13:59:14 -07:00
parent 0c50bc6449
commit bd2281e32d
2 changed files with 63 additions and 6 deletions

View File

@ -17,17 +17,25 @@ export function resolve(
function resolveNode(
resolver: ComponentResolver, node: TreeNode<ActivatedRouteSnapshot>): Observable<any> {
if (node.children.length === 0) {
return fromPromise(resolver.resolveComponent(<any>node.value.component).then(factory => {
return fromPromise(resolveComponent(resolver, <any>node.value).then(factory => {
node.value._resolvedComponentFactory = factory;
return node.value;
}));
} else {
const c = node.children.map(c => resolveNode(resolver, c).toPromise());
return forkJoin(c).map(
_ => resolver.resolveComponent(<any>node.value.component).then(factory => {
node.value._resolvedComponentFactory = factory;
return node.value;
}));
return forkJoin(c).map(_ => resolveComponent(resolver, <any>node.value).then(factory => {
node.value._resolvedComponentFactory = factory;
return node.value;
}));
}
}
function resolveComponent(
resolver: ComponentResolver, snapshot: ActivatedRouteSnapshot): Promise<any> {
if (snapshot.component && snapshot._routeConfig) {
return resolver.resolveComponent(<any>snapshot.component);
} else {
return Promise.resolve(null);
}
}

View File

@ -0,0 +1,49 @@
import {DefaultUrlSerializer} from '../src/url_serializer';
import {UrlTree, UrlSegment} from '../src/url_tree';
import {RouterStateSnapshot} from '../src/router_state';
import {recognize} from '../src/recognize';
import {resolve} from '../src/resolve';
import {RouterConfig} from '../src/config';
describe('resolve', () => {
it('should resolve components', () => {
checkResolve([
{path: 'a', component: "ComponentA"}
], "a", {ComponentA: 'ResolvedComponentA'}, (resolved:RouterStateSnapshot) => {
expect(resolved.firstChild(resolved.root)._resolvedComponentFactory).toEqual('ResolvedComponentA');
});
});
it('should not resolve componentless routes', () => {
checkResolve([
{path: 'a', children: []}
], "a", {}, (resolved:RouterStateSnapshot) => {
expect(resolved.firstChild(resolved.root)._resolvedComponentFactory).toEqual(null);
});
});
});
function checkResolve(config: RouterConfig, url: string, resolved: {[k:string]:string}, callback: any): void {
const resolver = {
resolveComponent: (component:string):Promise<any> => {
if (resolved[component]) {
return Promise.resolve(resolved[component]);
} else {
return Promise.reject("unknown component");
}
}
};
recognize(RootComponent, config, tree(url), url).mergeMap(s => resolve(<any>resolver, s)).subscribe(callback, e => {
throw e;
});
}
function tree(url: string): UrlTree {
return new DefaultUrlSerializer().parse(url);
}
class RootComponent {}
class ComponentA {}
class ComponentB {}
class ComponentC {}