angular-cn/modules/@angular/router/test/resolve.spec.ts

49 lines
1.5 KiB
TypeScript
Raw Normal View History

2016-06-21 14:49:42 -04:00
import {RouterConfig} from '../src/config';
import {recognize} from '../src/recognize';
import {resolve} from '../src/resolve';
2016-06-21 14:49:42 -04:00
import {RouterStateSnapshot} from '../src/router_state';
import {DefaultUrlSerializer, UrlSegment, UrlTree} from '../src/url_tree';
describe('resolve', () => {
it('should resolve components', () => {
2016-06-21 14:49:42 -04:00
checkResolve(
[{path: 'a', component: 'ComponentA'}], 'a', {ComponentA: 'ResolvedComponentA'},
(resolved: RouterStateSnapshot) => {
expect(resolved.firstChild(resolved.root)._resolvedComponentFactory)
.toEqual('ResolvedComponentA');
});
});
it('should not resolve componentless routes', () => {
2016-06-21 14:49:42 -04:00
checkResolve([{path: 'a', children: []}], 'a', {}, (resolved: RouterStateSnapshot) => {
expect(resolved.firstChild(resolved.root)._resolvedComponentFactory).toEqual(null);
});
});
});
2016-06-21 14:49:42 -04:00
function checkResolve(
config: RouterConfig, url: string, resolved: {[k: string]: string}, callback: any): void {
const resolver = {
2016-06-21 14:49:42 -04:00
resolveComponent: (component: string): Promise<any> => {
if (resolved[component]) {
return Promise.resolve(resolved[component]);
} else {
2016-06-21 14:49:42 -04:00
return Promise.reject('unknown component');
}
}
};
2016-06-21 14:49:42 -04:00
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 {}