diff --git a/packages/router/src/recognize.ts b/packages/router/src/recognize.ts index 42ffac58e9..0cb2da65ca 100644 --- a/packages/router/src/recognize.ts +++ b/packages/router/src/recognize.ts @@ -12,54 +12,62 @@ import {Observable, Observer, of} from 'rxjs'; import {Data, ResolveData, Route, Routes} from './config'; import {ActivatedRouteSnapshot, inheritedParamsDataResolve, ParamsInheritanceStrategy, RouterStateSnapshot} from './router_state'; import {defaultUrlMatcher, PRIMARY_OUTLET} from './shared'; -import {mapChildrenIntoArray, UrlSegment, UrlSegmentGroup, UrlTree} from './url_tree'; +import {UrlSegment, UrlSegmentGroup, UrlTree} from './url_tree'; import {forEach, last} from './utils/collection'; import {getOutlet} from './utils/config'; import {TreeNode} from './utils/tree'; class NoMatch {} +function newObservableError(e: unknown): Observable { + // TODO(atscott): This pattern is used throughout the router code and can be `throwError` instead. + return new Observable((obs: Observer) => obs.error(e)); +} + export function recognize( rootComponentType: Type|null, config: Routes, urlTree: UrlTree, url: string, paramsInheritanceStrategy: ParamsInheritanceStrategy = 'emptyOnly', relativeLinkResolution: 'legacy'|'corrected' = 'legacy'): Observable { - return new Recognizer( - rootComponentType, config, urlTree, url, paramsInheritanceStrategy, - relativeLinkResolution) - .recognize(); + try { + const result = new Recognizer( + rootComponentType, config, urlTree, url, paramsInheritanceStrategy, + relativeLinkResolution) + .recognize(); + if (result === null) { + return newObservableError(new NoMatch()); + } else { + return of(result); + } + } catch (e) { + // Catch the potential error from recognize due to duplicate outlet matches and return as an + // `Observable` error instead. + return newObservableError(e); + } } -class Recognizer { +export class Recognizer { constructor( private rootComponentType: Type|null, private config: Routes, private urlTree: UrlTree, private url: string, private paramsInheritanceStrategy: ParamsInheritanceStrategy, private relativeLinkResolution: 'legacy'|'corrected') {} - recognize(): Observable { - try { - const rootSegmentGroup = - split(this.urlTree.root, [], [], this.config, this.relativeLinkResolution).segmentGroup; + recognize(): RouterStateSnapshot|null { + const rootSegmentGroup = + split(this.urlTree.root, [], [], this.config, this.relativeLinkResolution).segmentGroup; - const children = this.processSegmentGroup(this.config, rootSegmentGroup, PRIMARY_OUTLET); - if (children === null) { - return new Observable( - (obs: Observer) => obs.error(new NoMatch())); - } - - const root = new ActivatedRouteSnapshot( - [], Object.freeze({}), Object.freeze({...this.urlTree.queryParams}), - this.urlTree.fragment!, {}, PRIMARY_OUTLET, this.rootComponentType, null, - this.urlTree.root, -1, {}); - - const rootNode = new TreeNode(root, children); - const routeState = new RouterStateSnapshot(this.url, rootNode); - this.inheritParamsAndData(routeState._root); - return of(routeState); - - } catch (e) { - return new Observable( - (obs: Observer) => obs.error(e)); + const children = this.processSegmentGroup(this.config, rootSegmentGroup, PRIMARY_OUTLET); + if (children === null) { + return null; } + + const root = new ActivatedRouteSnapshot( + [], Object.freeze({}), Object.freeze({...this.urlTree.queryParams}), this.urlTree.fragment!, + {}, PRIMARY_OUTLET, this.rootComponentType, null, this.urlTree.root, -1, {}); + + const rootNode = new TreeNode(root, children); + const routeState = new RouterStateSnapshot(this.url, rootNode); + this.inheritParamsAndData(routeState._root); + return routeState; } inheritParamsAndData(routeNode: TreeNode): void { diff --git a/packages/router/test/recognize.spec.ts b/packages/router/test/recognize.spec.ts index 778319b625..e8e1819816 100644 --- a/packages/router/test/recognize.spec.ts +++ b/packages/router/test/recognize.spec.ts @@ -7,148 +7,133 @@ */ import {Routes} from '../src/config'; -import {recognize} from '../src/recognize'; +import {Recognizer} from '../src/recognize'; import {ActivatedRouteSnapshot, RouterStateSnapshot} from '../src/router_state'; import {Params, PRIMARY_OUTLET} from '../src/shared'; import {DefaultUrlSerializer, UrlTree} from '../src/url_tree'; describe('recognize', () => { it('should work', () => { - checkRecognize([{path: 'a', component: ComponentA}], 'a', (s: any) => { - checkActivatedRoute(s.root, '', {}, RootComponent); - checkActivatedRoute(s.firstChild(s.root)!, 'a', {}, ComponentA); - }); + const s = recognize([{path: 'a', component: ComponentA}], 'a'); + checkActivatedRoute(s.root, '', {}, RootComponent); + checkActivatedRoute(s.root.firstChild!, 'a', {}, ComponentA); }); it('should freeze params object', () => { - checkRecognize([{path: 'a/:id', component: ComponentA}], 'a/10', (s: RouterStateSnapshot) => { - checkActivatedRoute(s.root, '', {}, RootComponent); - const child = (s as any).firstChild(s.root)!; - expect(Object.isFrozen(child.params)).toBeTruthy(); - }); + const s: RouterStateSnapshot = recognize([{path: 'a/:id', component: ComponentA}], 'a/10'); + checkActivatedRoute(s.root, '', {}, RootComponent); + const child = s.root.firstChild!; + expect(Object.isFrozen(child.params)).toBeTruthy(); }); it('should support secondary routes', () => { - checkRecognize( + const s: RouterStateSnapshot = recognize( [ {path: 'a', component: ComponentA}, {path: 'b', component: ComponentB, outlet: 'left'}, {path: 'c', component: ComponentC, outlet: 'right'} ], - 'a(left:b//right:c)', (s: RouterStateSnapshot) => { - const c = (s as any).children(s.root); - checkActivatedRoute(c[0], 'a', {}, ComponentA); - checkActivatedRoute(c[1], 'b', {}, ComponentB, 'left'); - checkActivatedRoute(c[2], 'c', {}, ComponentC, 'right'); - }); + 'a(left:b//right:c)'); + const c = s.root.children; + checkActivatedRoute(c[0], 'a', {}, ComponentA); + checkActivatedRoute(c[1], 'b', {}, ComponentB, 'left'); + checkActivatedRoute(c[2], 'c', {}, ComponentC, 'right'); }); it('should set url segment and index properly', () => { const url = tree('a(left:b//right:c)'); - recognize( - RootComponent, + const s = recognize( [ {path: 'a', component: ComponentA}, {path: 'b', component: ComponentB, outlet: 'left'}, {path: 'c', component: ComponentC, outlet: 'right'} ], - url, 'a(left:b//right:c)') - .subscribe((s: any) => { - expect(s.root._urlSegment).toBe(url.root); - expect(s.root._lastPathIndex).toBe(-1); + 'a(left:b//right:c)'); + expect(s.root.url.toString()).toEqual(url.root.toString()); + expect((s.root as any)._lastPathIndex).toBe(-1); - const c = (s as any).children(s.root); - expect(c[0]._urlSegment).toBe((url.root as any).children[PRIMARY_OUTLET]); - expect(c[0]._lastPathIndex).toBe(0); + const c = s.root.children; + expect(c[0].url.toString()).toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((c[0] as any)._lastPathIndex).toBe(0); - expect(c[1]._urlSegment).toBe((url.root as any).children['left']); - expect(c[1]._lastPathIndex).toBe(0); + expect(c[1].url.toString()).toEqual(url.root.children['left'].toString()); + expect((c[1] as any)._lastPathIndex).toBe(0); - expect(c[2]._urlSegment).toBe((url.root as any).children['right']); - expect(c[2]._lastPathIndex).toBe(0); - }); + expect(c[2].url.toString()).toEqual(url.root.children['right'].toString()); + expect((c[2] as any)._lastPathIndex).toBe(0); }); it('should set url segment and index properly (nested case)', () => { const url = tree('a/b/c'); - recognize( - RootComponent, + const s = recognize( [ {path: 'a/b', component: ComponentA, children: [{path: 'c', component: ComponentC}]}, ], - url, 'a/b/c') - .subscribe((s: RouterStateSnapshot) => { - expect((s.root as any)._urlSegment).toBe(url.root); - expect((s.root as any)._lastPathIndex).toBe(-1); + 'a/b/c'); + expect((s.root as any)._urlSegment.toString()).toEqual(url.root.toString()); + expect((s.root as any)._lastPathIndex).toBe(-1); - const compA = (s as any).firstChild(s.root)!; - expect(compA._urlSegment).toBe((url.root as any).children[PRIMARY_OUTLET]); - expect(compA._lastPathIndex).toBe(1); + const compA = s.root.firstChild!; + expect((compA as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((compA as any)._lastPathIndex).toBe(1); - const compC = (s as any).firstChild(compA)!; - expect(compC._urlSegment).toBe((url.root as any).children[PRIMARY_OUTLET]); - expect(compC._lastPathIndex).toBe(2); - }); + const compC = compA.firstChild!; + expect((compC as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((compC as any)._lastPathIndex).toBe(2); }); it('should set url segment and index properly (wildcard)', () => { const url = tree('a/b/c'); - recognize( - RootComponent, + const s = recognize( [ {path: 'a', component: ComponentA, children: [{path: '**', component: ComponentB}]}, ], - url, 'a/b/c') - .subscribe((s: any) => { - expect(s.root._urlSegment).toBe(url.root); - expect(s.root._lastPathIndex).toBe(-1); + 'a/b/c'); + expect((s.root as any)._urlSegment.toString()).toEqual(url.root.toString()); + expect((s.root as any)._lastPathIndex).toBe(-1); - const compA = (s as any).firstChild(s.root)!; - expect(compA._urlSegment).toBe((url as any).root.children[PRIMARY_OUTLET]); - expect(compA._lastPathIndex).toBe(0); + const compA = s.root.firstChild!; + expect((compA as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((compA as any)._lastPathIndex).toBe(0); - const compC = (s as any).firstChild(compA)!; - expect(compC._urlSegment).toBe((url as any).root.children[PRIMARY_OUTLET]); - expect(compC._lastPathIndex).toBe(2); - }); + const compC = compA.firstChild!; + expect((compC as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((compC as any)._lastPathIndex).toBe(2); }); it('should match routes in the depth first order', () => { - checkRecognize( + const s = recognize( [ {path: 'a', component: ComponentA, children: [{path: ':id', component: ComponentB}]}, {path: 'a/:id', component: ComponentC} ], - 'a/paramA', (s: RouterStateSnapshot) => { - checkActivatedRoute(s.root, '', {}, RootComponent); - checkActivatedRoute((s as any).firstChild(s.root)!, 'a', {}, ComponentA); - checkActivatedRoute( - (s as any).firstChild((s as any).firstChild(s.root))!, 'paramA', {id: 'paramA'}, - ComponentB); - }); + 'a/paramA'); + checkActivatedRoute(s.root, '', {}, RootComponent); + checkActivatedRoute(s.root.firstChild!, 'a', {}, ComponentA); + checkActivatedRoute(s.root.firstChild!.firstChild!, 'paramA', {id: 'paramA'}, ComponentB); - checkRecognize( - [{path: 'a', component: ComponentA}, {path: 'a/:id', component: ComponentC}], 'a/paramA', - (s: RouterStateSnapshot) => { - checkActivatedRoute(s.root, '', {}, RootComponent); - checkActivatedRoute( - (s as any).firstChild(s.root)!, 'a/paramA', {id: 'paramA'}, ComponentC); - }); + const s2 = recognize( + [{path: 'a', component: ComponentA}, {path: 'a/:id', component: ComponentC}], 'a/paramA'); + checkActivatedRoute(s2.root, '', {}, RootComponent); + checkActivatedRoute(s2.root.firstChild!, 'a/paramA', {id: 'paramA'}, ComponentC); }); it('should use outlet name when matching secondary routes', () => { - checkRecognize( + const s = recognize( [ {path: 'a', component: ComponentA}, {path: 'b', component: ComponentB, outlet: 'left'}, {path: 'b', component: ComponentC, outlet: 'right'} ], - 'a(right:b)', (s: RouterStateSnapshot) => { - const c = (s as any).children(s.root); - checkActivatedRoute(c[0], 'a', {}, ComponentA); - checkActivatedRoute(c[1], 'b', {}, ComponentC, 'right'); - }); + 'a(right:b)'); + const c = s.root.children; + checkActivatedRoute(c[0], 'a', {}, ComponentA); + checkActivatedRoute(c[1], 'b', {}, ComponentC, 'right'); }); it('should handle non top-level secondary routes', () => { - checkRecognize( + const s = recognize( [ { path: 'a', @@ -158,170 +143,143 @@ describe('recognize', () => { ] }, ], - 'a/(b//left:c)', (s: RouterStateSnapshot) => { - const c = (s as any).children((s as any).firstChild(s.root)); - checkActivatedRoute(c[0], 'b', {}, ComponentB, PRIMARY_OUTLET); - checkActivatedRoute(c[1], 'c', {}, ComponentC, 'left'); - }); + 'a/(b//left:c)'); + const c = s.root.firstChild!.children; + checkActivatedRoute(c[0], 'b', {}, ComponentB, PRIMARY_OUTLET); + checkActivatedRoute(c[1], 'c', {}, ComponentC, 'left'); }); it('should sort routes by outlet name', () => { - checkRecognize( + const s = recognize( [ {path: 'a', component: ComponentA}, {path: 'c', component: ComponentC, outlet: 'c'}, {path: 'b', component: ComponentB, outlet: 'b'} ], - 'a(c:c//b:b)', (s: RouterStateSnapshot) => { - const c = (s as any).children(s.root); - checkActivatedRoute(c[0], 'a', {}, ComponentA); - checkActivatedRoute(c[1], 'b', {}, ComponentB, 'b'); - checkActivatedRoute(c[2], 'c', {}, ComponentC, 'c'); - }); + 'a(c:c//b:b)'); + const c = s.root.children; + checkActivatedRoute(c[0], 'a', {}, ComponentA); + checkActivatedRoute(c[1], 'b', {}, ComponentB, 'b'); + checkActivatedRoute(c[2], 'c', {}, ComponentC, 'c'); }); it('should support matrix parameters', () => { - checkRecognize( + const s = recognize( [ {path: 'a', component: ComponentA, children: [{path: 'b', component: ComponentB}]}, {path: 'c', component: ComponentC, outlet: 'left'} ], - 'a;a1=11;a2=22/b;b1=111;b2=222(left:c;c1=1111;c2=2222)', (s: RouterStateSnapshot) => { - const c = (s as any).children(s.root); - checkActivatedRoute(c[0], 'a', {a1: '11', a2: '22'}, ComponentA); - checkActivatedRoute( - (s as any).firstChild(c[0])!, 'b', {b1: '111', b2: '222'}, ComponentB); - checkActivatedRoute(c[1], 'c', {c1: '1111', c2: '2222'}, ComponentC, 'left'); - }); + 'a;a1=11;a2=22/b;b1=111;b2=222(left:c;c1=1111;c2=2222)'); + const c = s.root.children; + checkActivatedRoute(c[0], 'a', {a1: '11', a2: '22'}, ComponentA); + checkActivatedRoute(c[0].firstChild!, 'b', {b1: '111', b2: '222'}, ComponentB); + checkActivatedRoute(c[1], 'c', {c1: '1111', c2: '2222'}, ComponentC, 'left'); }); describe('data', () => { it('should set static data', () => { - checkRecognize( - [{path: 'a', data: {one: 1}, component: ComponentA}], 'a', (s: RouterStateSnapshot) => { - const r: ActivatedRouteSnapshot = (s as any).firstChild(s.root)!; - expect(r.data).toEqual({one: 1}); - }); + const s = recognize([{path: 'a', data: {one: 1}, component: ComponentA}], 'a'); + const r: ActivatedRouteSnapshot = s.root.firstChild!; + expect(r.data).toEqual({one: 1}); }); it('should inherit componentless route\'s data', () => { - checkRecognize( + const s = recognize( [{ path: 'a', data: {one: 1}, children: [{path: 'b', data: {two: 2}, component: ComponentB}] }], - 'a/b', (s: RouterStateSnapshot) => { - const r: ActivatedRouteSnapshot = - (s as any).firstChild((s as any).firstChild(s.root))!; - expect(r.data).toEqual({one: 1, two: 2}); - }); + 'a/b'); + const r: ActivatedRouteSnapshot = s.root.firstChild!.firstChild!; + expect(r.data).toEqual({one: 1, two: 2}); }); it('should not inherit route\'s data if it has component', () => { - checkRecognize( + const s = recognize( [{ path: 'a', component: ComponentA, data: {one: 1}, children: [{path: 'b', data: {two: 2}, component: ComponentB}] }], - 'a/b', (s: any /* RouterStateSnapshot */) => { - const r: ActivatedRouteSnapshot = s.firstChild(s.firstChild(s.root))!; - expect(r.data).toEqual({two: 2}); - }); + 'a/b'); + const r: ActivatedRouteSnapshot = s.root.firstChild!.firstChild!; + expect(r.data).toEqual({two: 2}); }); it('should inherit route\'s data if paramsInheritanceStrategy is \'always\'', () => { - checkRecognize( + const s = recognize( [{ path: 'a', component: ComponentA, data: {one: 1}, children: [{path: 'b', data: {two: 2}, component: ComponentB}] }], - 'a/b', (s: any /* RouterStateSnapshot */) => { - const r: ActivatedRouteSnapshot = s.firstChild(s.firstChild(s.root))!; - expect(r.data).toEqual({one: 1, two: 2}); - }, 'always'); + 'a/b', 'always'); + const r: ActivatedRouteSnapshot = s.root.firstChild!.firstChild!; + expect(r.data).toEqual({one: 1, two: 2}); }); it('should set resolved data', () => { - checkRecognize( - [{path: 'a', resolve: {one: 'some-token'}, component: ComponentA}], 'a', (s: any) => { - const r: any = s.firstChild(s.root)!; - expect(r._resolve).toEqual({one: 'some-token'}); - }); + const s = recognize([{path: 'a', resolve: {one: 'some-token'}, component: ComponentA}], 'a'); + const r: any = s.root.firstChild!; + expect(r._resolve).toEqual({one: 'some-token'}); }); }); describe('empty path', () => { describe('root', () => { it('should work', () => { - checkRecognize([{path: '', component: ComponentA}], '', (s: RouterStateSnapshot) => { - checkActivatedRoute((s as any).firstChild(s.root)!, '', {}, ComponentA); - }); + const s = recognize([{path: '', component: ComponentA}], ''); + checkActivatedRoute(s.root.firstChild!, '', {}, ComponentA); }); it('should match when terminal', () => { - checkRecognize( - [{path: '', pathMatch: 'full', component: ComponentA}], '', - (s: RouterStateSnapshot) => { - checkActivatedRoute((s as any).firstChild(s.root)!, '', {}, ComponentA); - }); + const s = recognize([{path: '', pathMatch: 'full', component: ComponentA}], ''); + checkActivatedRoute(s.root.firstChild!, '', {}, ComponentA); }); it('should work (nested case)', () => { - checkRecognize( - [{path: '', component: ComponentA, children: [{path: '', component: ComponentB}]}], '', - (s: RouterStateSnapshot) => { - checkActivatedRoute((s as any).firstChild(s.root)!, '', {}, ComponentA); - checkActivatedRoute( - (s as any).firstChild((s as any).firstChild(s.root))!, '', {}, ComponentB); - }); + const s = recognize( + [{path: '', component: ComponentA, children: [{path: '', component: ComponentB}]}], ''); + checkActivatedRoute(s.root.firstChild!, '', {}, ComponentA); + checkActivatedRoute(s.root.firstChild!.firstChild!, '', {}, ComponentB); }); it('should set url segment and index properly', () => { const url = tree('') as any; - recognize( - RootComponent, - [{path: '', component: ComponentA, children: [{path: '', component: ComponentB}]}], url, - '') - .forEach((s: any) => { - expect(s.root._urlSegment).toBe(url.root); - expect(s.root._lastPathIndex).toBe(-1); + const s = recognize( + [{path: '', component: ComponentA, children: [{path: '', component: ComponentB}]}], ''); + expect((s.root as any)._urlSegment.toString()).toEqual(url.root.toString()); + expect((s.root as any)._lastPathIndex).toBe(-1); - const c = s.firstChild(s.root)!; - expect(c._urlSegment).toBe(url.root); - expect(c._lastPathIndex).toBe(-1); + const c = s.root.firstChild!; + expect((c as any)._urlSegment.toString()).toEqual(url.root.toString()); + expect((c as any)._lastPathIndex).toBe(-1); - const c2 = s.firstChild(s.firstChild(s.root))!; - expect(c2._urlSegment).toBe(url.root); - expect(c2._lastPathIndex).toBe(-1); - }); + const c2 = s.root.firstChild!.firstChild!; + expect((c2 as any)._urlSegment.toString()).toEqual(url.root.toString()); + expect((c2 as any)._lastPathIndex).toBe(-1); }); it('should inherit params', () => { - checkRecognize( + const s = recognize( [{ path: 'a', component: ComponentA, children: [{path: '', component: ComponentB, children: [{path: '', component: ComponentC}]}] }], - '/a;p=1', (s: RouterStateSnapshot) => { - checkActivatedRoute((s as any).firstChild(s.root)!, 'a', {p: '1'}, ComponentA); - checkActivatedRoute( - (s as any).firstChild((s as any).firstChild(s.root)!)!, '', {p: '1'}, ComponentB); - checkActivatedRoute( - (s as any).firstChild((s as any).firstChild((s as any).firstChild(s.root)!)!)!, - '', {p: '1'}, ComponentC); - }); + '/a;p=1'); + checkActivatedRoute(s.root.firstChild!, 'a', {p: '1'}, ComponentA); + checkActivatedRoute(s.root.firstChild!.firstChild!, '', {p: '1'}, ComponentB); + checkActivatedRoute(s.root.firstChild!.firstChild!.firstChild!, '', {p: '1'}, ComponentC); }); }); describe('aux split is in the middle', () => { it('should match (non-terminal)', () => { - checkRecognize( + const s = recognize( [{ path: 'a', component: ComponentA, @@ -329,13 +287,12 @@ describe('recognize', () => { {path: 'b', component: ComponentB}, {path: '', component: ComponentC, outlet: 'aux'} ] }], - 'a/b', (s: RouterStateSnapshot) => { - checkActivatedRoute((s as any).firstChild(s.root)!, 'a', {}, ComponentA); + 'a/b'); + checkActivatedRoute(s.root.firstChild!, 'a', {}, ComponentA); - const c = (s as any).children((s as any).firstChild(s.root)!); - checkActivatedRoute(c[0], 'b', {}, ComponentB); - checkActivatedRoute(c[1], '', {}, ComponentC, 'aux'); - }); + const c = s.root.firstChild!.children; + checkActivatedRoute(c[0], 'b', {}, ComponentB); + checkActivatedRoute(c[1], '', {}, ComponentC, 'aux'); }); it('should match (non-terminal) when both primary and secondary and primary has a child', @@ -359,20 +316,19 @@ describe('recognize', () => { ] }]; - checkRecognize(config, 'parent/b', (s: RouterStateSnapshot) => { - checkActivatedRoute(s.root, '', {}, RootComponent); - checkActivatedRoute((s as any).firstChild(s.root)!, 'parent', {}, undefined!); + const s = recognize(config, 'parent/b'); + checkActivatedRoute(s.root, '', {}, RootComponent); + checkActivatedRoute(s.root.firstChild!, 'parent', {}, undefined!); - const cc = (s as any).children((s as any).firstChild(s.root)!); - checkActivatedRoute(cc[0], '', {}, ComponentA); - checkActivatedRoute(cc[1], '', {}, ComponentD, 'secondary'); + const cc = s.root.firstChild!.children; + checkActivatedRoute(cc[0], '', {}, ComponentA); + checkActivatedRoute(cc[1], '', {}, ComponentD, 'secondary'); - checkActivatedRoute((s as any).firstChild(cc[0])!, 'b', {}, ComponentB); - }); + checkActivatedRoute(cc[0].firstChild!, 'b', {}, ComponentB); }); it('should match (terminal)', () => { - checkRecognize( + const s = recognize( [{ path: 'a', component: ComponentA, @@ -381,76 +337,77 @@ describe('recognize', () => { {path: '', pathMatch: 'full', component: ComponentC, outlet: 'aux'} ] }], - 'a/b', (s: RouterStateSnapshot) => { - checkActivatedRoute((s as any).firstChild(s.root)!, 'a', {}, ComponentA); + 'a/b'); + checkActivatedRoute(s.root.firstChild!, 'a', {}, ComponentA); - const c = (s as any).children((s as any).firstChild(s.root)!); - expect(c.length).toEqual(1); - checkActivatedRoute(c[0], 'b', {}, ComponentB); - }); + const c = s.root.firstChild!.children; + expect(c.length).toEqual(1); + checkActivatedRoute(c[0], 'b', {}, ComponentB); }); it('should set url segment and index properly', () => { const url = tree('a/b') as any; - recognize( - RootComponent, [{ + const s = recognize( + [{ path: 'a', component: ComponentA, children: [ {path: 'b', component: ComponentB}, {path: '', component: ComponentC, outlet: 'aux'} ] }], - url, 'a/b') - .forEach((s: any) => { - expect(s.root._urlSegment).toBe(url.root); - expect(s.root._lastPathIndex).toBe(-1); + 'a/b'); + expect((s.root as any)._urlSegment.toString()).toEqual(url.root.toString()); + expect((s.root as any)._lastPathIndex).toBe(-1); - const a = s.firstChild(s.root)!; - expect(a._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]); - expect(a._lastPathIndex).toBe(0); + const a = s.root.firstChild!; + expect((a as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((a as any)._lastPathIndex).toBe(0); - const b = s.firstChild(a)!; - expect(b._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]); - expect(b._lastPathIndex).toBe(1); + const b = a.firstChild!; + expect((b as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((b as any)._lastPathIndex).toBe(1); - const c = s.children(a)[1]; - expect(c._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]); - expect(c._lastPathIndex).toBe(0); - }); + const c = a.children[1]; + expect((c as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((c as any)._lastPathIndex).toBe(0); }); it('should set url segment and index properly when nested empty-path segments', () => { const url = tree('a') as any; - recognize( - RootComponent, [{ + const s = recognize( + [{ path: 'a', children: [{path: '', component: ComponentB, children: [{path: '', component: ComponentC}]}] }], - url, 'a') - .forEach((s: any) => { - expect(s.root._urlSegment).toBe(url.root); - expect(s.root._lastPathIndex).toBe(-1); + 'a'); + expect((s.root as any)._urlSegment.toString()).toEqual(url.root.toString()); + expect((s.root as any)._lastPathIndex).toBe(-1); - const a = s.firstChild(s.root)!; - expect(a._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]); - expect(a._lastPathIndex).toBe(0); + const a = s.root.firstChild!; + expect((a as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((a as any)._lastPathIndex).toBe(0); - const b = s.firstChild(a)!; - expect(b._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]); - expect(b._lastPathIndex).toBe(0); + const b = a.firstChild!; + expect((b as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((b as any)._lastPathIndex).toBe(0); - const c = s.firstChild(b)!; - expect(c._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]); - expect(c._lastPathIndex).toBe(0); - }); + const c = b.firstChild!; + expect((c as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((c as any)._lastPathIndex).toBe(0); }); it('should set url segment and index properly with the "corrected" option for nested empty-path segments', () => { - const url = tree('a/b') as any; - recognize( - RootComponent, [{ + const url = tree('a/b'); + const s = recognize( + [{ path: 'a', children: [{ path: 'b', @@ -462,60 +419,60 @@ describe('recognize', () => { }] }] }], - url, 'a/b', 'emptyOnly', 'corrected') - .forEach((s: any) => { - expect(s.root._urlSegment).toBe(url.root); - expect(s.root._lastPathIndex).toBe(-1); + 'a/b', 'emptyOnly', 'corrected'); + expect((s.root as any)._urlSegment.toString()).toEqual(url.root.toString()); + expect((s.root as any)._lastPathIndex).toBe(-1); - const a = s.firstChild(s.root)!; - expect(a._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]); - expect(a._lastPathIndex).toBe(0); + const a = s.root.firstChild!; + expect((a as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((a as any)._lastPathIndex).toBe(0); - const b = s.firstChild(a)!; - expect(b._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]); - expect(b._lastPathIndex).toBe(1); + const b = a.firstChild!; + expect((b as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((b as any)._lastPathIndex).toBe(1); - const c = s.firstChild(b)!; - expect(c._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]); - expect(c._lastPathIndex).toBe(1); + const c = b.firstChild!; + expect((c as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((c as any)._lastPathIndex).toBe(1); - const d = s.firstChild(c)!; - expect(d._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]); - expect(d._lastPathIndex).toBe(1); - }); + const d = c.firstChild!; + expect((d as any)._urlSegment.toString()) + .toEqual(url.root.children[PRIMARY_OUTLET].toString()); + expect((d as any)._lastPathIndex).toBe(1); }); it('should set url segment and index properly when nested empty-path segments (2)', () => { const url = tree(''); - recognize( - RootComponent, [{ + const s = recognize( + [{ path: '', children: [{path: '', component: ComponentB, children: [{path: '', component: ComponentC}]}] }], - url, '') - .forEach((s: any) => { - expect(s.root._urlSegment).toBe(url.root); - expect(s.root._lastPathIndex).toBe(-1); + ''); + expect((s.root as any)._urlSegment.toString()).toEqual(url.root.toString()); + expect((s.root as any)._lastPathIndex).toBe(-1); - const a = (s as any).firstChild(s.root)!; - expect(a._urlSegment).toBe(url.root); - expect(a._lastPathIndex).toBe(-1); + const a = s.root.firstChild!; + expect((a as any)._urlSegment.toString()).toEqual(url.root.toString()); + expect((a as any)._lastPathIndex).toBe(-1); - const b = (s as any).firstChild(a)!; - expect(b._urlSegment).toBe(url.root); - expect(b._lastPathIndex).toBe(-1); + const b = a.firstChild!; + expect((b as any)._urlSegment.toString()).toEqual(url.root.toString()); + expect((b as any)._lastPathIndex).toBe(-1); - const c = (s as any).firstChild(b)!; - expect(c._urlSegment).toBe(url.root); - expect(c._lastPathIndex).toBe(-1); - }); + const c = b.firstChild!; + expect((c as any)._urlSegment.toString()).toEqual(url.root.toString()); + expect((c as any)._lastPathIndex).toBe(-1); }); }); describe('aux split at the end (no right child)', () => { it('should match (non-terminal)', () => { - checkRecognize( + const s = recognize( [{ path: 'a', component: ComponentA, @@ -524,17 +481,16 @@ describe('recognize', () => { {path: '', component: ComponentC, outlet: 'aux'}, ] }], - 'a', (s: RouterStateSnapshot) => { - checkActivatedRoute((s as any).firstChild(s.root)!, 'a', {}, ComponentA); + 'a'); + checkActivatedRoute(s.root.firstChild!, 'a', {}, ComponentA); - const c = (s as any).children((s as any).firstChild(s.root)!); - checkActivatedRoute(c[0], '', {}, ComponentB); - checkActivatedRoute(c[1], '', {}, ComponentC, 'aux'); - }); + const c = s.root.firstChild!.children; + checkActivatedRoute(c[0], '', {}, ComponentB); + checkActivatedRoute(c[1], '', {}, ComponentC, 'aux'); }); it('should match (terminal)', () => { - checkRecognize( + const s = recognize( [{ path: 'a', component: ComponentA, @@ -543,17 +499,16 @@ describe('recognize', () => { {path: '', pathMatch: 'full', component: ComponentC, outlet: 'aux'}, ] }], - 'a', (s: RouterStateSnapshot) => { - checkActivatedRoute((s as any).firstChild(s.root)!, 'a', {}, ComponentA); + 'a'); + checkActivatedRoute(s.root.firstChild!, 'a', {}, ComponentA); - const c = (s as any).children((s as any).firstChild(s.root)!); - checkActivatedRoute(c[0], '', {}, ComponentB); - checkActivatedRoute(c[1], '', {}, ComponentC, 'aux'); - }); + const c = s.root.firstChild!.children; + checkActivatedRoute(c[0], '', {}, ComponentB); + checkActivatedRoute(c[1], '', {}, ComponentC, 'aux'); }); it('should work only only primary outlet', () => { - checkRecognize( + const s = recognize( [{ path: 'a', component: ComponentA, @@ -562,35 +517,33 @@ describe('recognize', () => { {path: 'c', component: ComponentC, outlet: 'aux'}, ] }], - 'a/(aux:c)', (s: RouterStateSnapshot) => { - checkActivatedRoute((s as any).firstChild(s.root)!, 'a', {}, ComponentA); + 'a/(aux:c)'); + checkActivatedRoute(s.root.firstChild!, 'a', {}, ComponentA); - const c = (s as any).children((s as any).firstChild(s.root)!); - checkActivatedRoute(c[0], '', {}, ComponentB); - checkActivatedRoute(c[1], 'c', {}, ComponentC, 'aux'); - }); + const c = s.root.firstChild!.children; + checkActivatedRoute(c[0], '', {}, ComponentB); + checkActivatedRoute(c[1], 'c', {}, ComponentC, 'aux'); }); it('should work when split is at the root level', () => { - checkRecognize( + const s = recognize( [ {path: '', component: ComponentA}, {path: 'b', component: ComponentB}, {path: 'c', component: ComponentC, outlet: 'aux'} ], - '(aux:c)', (s: RouterStateSnapshot) => { - checkActivatedRoute(s.root, '', {}, RootComponent); + '(aux:c)'); + checkActivatedRoute(s.root, '', {}, RootComponent); - const children = (s as any).children(s.root); - expect(children.length).toEqual(2); - checkActivatedRoute(children[0], '', {}, ComponentA); - checkActivatedRoute(children[1], 'c', {}, ComponentC, 'aux'); - }); + const children = s.root.children; + expect(children.length).toEqual(2); + checkActivatedRoute(children[0], '', {}, ComponentA); + checkActivatedRoute(children[1], 'c', {}, ComponentC, 'aux'); }); }); describe('split at the end (right child)', () => { it('should match (non-terminal)', () => { - checkRecognize( + const s = recognize( [{ path: 'a', component: ComponentA, @@ -604,49 +557,45 @@ describe('recognize', () => { }, ] }], - 'a/(d//aux:e)', (s: RouterStateSnapshot) => { - checkActivatedRoute((s as any).firstChild(s.root)!, 'a', {}, ComponentA); + 'a/(d//aux:e)'); + checkActivatedRoute(s.root.firstChild!, 'a', {}, ComponentA); - const c = (s as any).children((s as any).firstChild(s.root)!); - checkActivatedRoute(c[0], '', {}, ComponentB); - checkActivatedRoute((s as any).firstChild(c[0])!, 'd', {}, ComponentD); - checkActivatedRoute(c[1], '', {}, ComponentC, 'aux'); - checkActivatedRoute((s as any).firstChild(c[1])!, 'e', {}, ComponentE); - }); + const c = s.root.firstChild!.children; + checkActivatedRoute(c[0], '', {}, ComponentB); + checkActivatedRoute(c[0].firstChild!, 'd', {}, ComponentD); + checkActivatedRoute(c[1], '', {}, ComponentC, 'aux'); + checkActivatedRoute(c[1].firstChild!, 'e', {}, ComponentE); }); }); }); describe('wildcards', () => { it('should support simple wildcards', () => { - checkRecognize( - [{path: '**', component: ComponentA}], 'a/b/c/d;a1=11', (s: RouterStateSnapshot) => { - checkActivatedRoute((s as any).firstChild(s.root)!, 'a/b/c/d', {a1: '11'}, ComponentA); - }); + const s = recognize([{path: '**', component: ComponentA}], 'a/b/c/d;a1=11'); + checkActivatedRoute(s.root.firstChild!, 'a/b/c/d', {a1: '11'}, ComponentA); }); }); describe('componentless routes', () => { it('should work', () => { - checkRecognize( + const s = recognize( [{ path: 'p/:id', children: [ {path: 'a', component: ComponentA}, {path: 'b', component: ComponentB, outlet: 'aux'} ] }], - 'p/11;pp=22/(a;pa=33//aux:b;pb=44)', (s: RouterStateSnapshot) => { - const p = (s as any).firstChild(s.root)!; - checkActivatedRoute(p, 'p/11', {id: '11', pp: '22'}, undefined!); + 'p/11;pp=22/(a;pa=33//aux:b;pb=44)'); + const p = s.root.firstChild!; + checkActivatedRoute(p, 'p/11', {id: '11', pp: '22'}, undefined!); - const c = (s as any).children(p); - checkActivatedRoute(c[0], 'a', {id: '11', pp: '22', pa: '33'}, ComponentA); - checkActivatedRoute(c[1], 'b', {id: '11', pp: '22', pb: '44'}, ComponentB, 'aux'); - }); + const c = p.children; + checkActivatedRoute(c[0], 'a', {id: '11', pp: '22', pa: '33'}, ComponentA); + checkActivatedRoute(c[1], 'b', {id: '11', pp: '22', pb: '44'}, ComponentB, 'aux'); }); it('should inherit params until encounters a normal route', () => { - checkRecognize( + const s = recognize( [{ path: 'p/:id', children: [{ @@ -656,23 +605,22 @@ describe('recognize', () => { ] }] }], - 'p/11/a/victor/b/c', (s: RouterStateSnapshot) => { - const p = (s as any).firstChild(s.root)!; - checkActivatedRoute(p, 'p/11', {id: '11'}, undefined!); + 'p/11/a/victor/b/c'); + const p = s.root.firstChild!; + checkActivatedRoute(p, 'p/11', {id: '11'}, undefined!); - const a = (s as any).firstChild(p)!; - checkActivatedRoute(a, 'a/victor', {id: '11', name: 'victor'}, undefined!); + const a = p.firstChild!; + checkActivatedRoute(a, 'a/victor', {id: '11', name: 'victor'}, undefined!); - const b = (s as any).firstChild(a)!; - checkActivatedRoute(b, 'b', {id: '11', name: 'victor'}, ComponentB); + const b = a.firstChild!; + checkActivatedRoute(b, 'b', {id: '11', name: 'victor'}, ComponentB); - const c = (s as any).firstChild(b)!; - checkActivatedRoute(c, 'c', {}, ComponentC); - }); + const c = b.firstChild!; + checkActivatedRoute(c, 'c', {}, ComponentC); }); it('should inherit all params if paramsInheritanceStrategy is \'always\'', () => { - checkRecognize( + const s = recognize( [{ path: 'p/:id', children: [{ @@ -682,25 +630,23 @@ describe('recognize', () => { ] }] }], - 'p/11/a/victor/b/c', (s: any /* RouterStateSnapshot */) => { - const c = s.firstChild(s.firstChild(s.firstChild(s.firstChild(s.root)!)!)!)!; - checkActivatedRoute(c, 'c', {id: '11', name: 'victor'}, ComponentC); - }, 'always'); + 'p/11/a/victor/b/c', 'always'); + const c = s.root.firstChild!.firstChild!.firstChild!.firstChild!; + checkActivatedRoute(c, 'c', {id: '11', name: 'victor'}, ComponentC); }); }); describe('empty URL leftovers', () => { it('should not throw when no children matching', () => { - checkRecognize( + const s = recognize( [{path: 'a', component: ComponentA, children: [{path: 'b', component: ComponentB}]}], - '/a', (s: RouterStateSnapshot) => { - const a = (s as any).firstChild(s.root); - checkActivatedRoute(a!, 'a', {}, ComponentA); - }); + '/a'); + const a = s.root.firstChild; + checkActivatedRoute(a!, 'a', {}, ComponentA); }); it('should not throw when no children matching (aux routes)', () => { - checkRecognize( + const s = recognize( [{ path: 'a', component: ComponentA, @@ -709,11 +655,10 @@ describe('recognize', () => { {path: '', component: ComponentC, outlet: 'aux'}, ] }], - '/a', (s: RouterStateSnapshot) => { - const a = (s as any).firstChild(s.root)!; - checkActivatedRoute(a, 'a', {}, ComponentA); - checkActivatedRoute((a as any).children[0], '', {}, ComponentC, 'aux'); - }); + '/a'); + const a = s.root.firstChild!; + checkActivatedRoute(a, 'a', {}, ComponentA); + checkActivatedRoute(a.children[0], '', {}, ComponentC, 'aux'); }); }); @@ -727,98 +672,75 @@ describe('recognize', () => { } }; - checkRecognize( + const s = recognize( [{ matcher: matcher, component: ComponentA, children: [{path: 'b', component: ComponentB}] }] as any, - '/a/1;p=99/b', (s: RouterStateSnapshot) => { - const a = (s as any).root.firstChild!; - checkActivatedRoute(a, 'a/1', {id: '1', p: '99'}, ComponentA); - checkActivatedRoute((a as any).firstChild!, 'b', {}, ComponentB); - }); + '/a/1;p=99/b'); + const a = s.root.firstChild!; + checkActivatedRoute(a, 'a/1', {id: '1', p: '99'}, ComponentA); + checkActivatedRoute(a.firstChild!, 'b', {}, ComponentB); }); it('should work with terminal route', () => { const matcher = (s: any, g: any, r: any) => s.length === 0 ? ({consumed: s}) : null; - checkRecognize([{matcher, component: ComponentA}] as any, '', (s: RouterStateSnapshot) => { - const a = (s as any).root.firstChild!; - checkActivatedRoute(a, '', {}, ComponentA); - }); + const s = recognize([{matcher, component: ComponentA}] as any, ''); + const a = s.root.firstChild!; + checkActivatedRoute(a, '', {}, ComponentA); }); it('should work with child terminal route', () => { const matcher = (s: any, g: any, r: any) => s.length === 0 ? ({consumed: s}) : null; - checkRecognize( + const s = recognize( [{path: 'a', component: ComponentA, children: [{matcher, component: ComponentB}]}] as any, - 'a', (s: RouterStateSnapshot) => { - const a = (s as any).root.firstChild!; - checkActivatedRoute(a, 'a', {}, ComponentA); - }); + 'a'); + const a = s.root.firstChild!; + checkActivatedRoute(a, 'a', {}, ComponentA); }); }); describe('query parameters', () => { it('should support query params', () => { const config = [{path: 'a', component: ComponentA}]; - checkRecognize(config, 'a?q=11', (s: RouterStateSnapshot) => { - expect(s.root.queryParams).toEqual({q: '11'}); - expect(s.root.queryParamMap.get('q')).toEqual('11'); - }); + const s = recognize(config, 'a?q=11'); + expect(s.root.queryParams).toEqual({q: '11'}); + expect(s.root.queryParamMap.get('q')).toEqual('11'); }); it('should freeze query params object', () => { - checkRecognize([{path: 'a', component: ComponentA}], 'a?q=11', (s: RouterStateSnapshot) => { - expect(Object.isFrozen(s.root.queryParams)).toBeTruthy(); - }); + const s = recognize([{path: 'a', component: ComponentA}], 'a?q=11'); + expect(Object.isFrozen(s.root.queryParams)).toBeTruthy(); }); it('should not freeze UrlTree query params', () => { const url = tree('a?q=11'); - recognize(RootComponent, [{path: 'a', component: ComponentA}], url, 'a?q=11') - .subscribe((s: RouterStateSnapshot) => { - expect(Object.isFrozen(url.queryParams)).toBe(false); - }); + const s = recognize([{path: 'a', component: ComponentA}], 'a?q=11'); + expect(Object.isFrozen(url.queryParams)).toBe(false); }); }); describe('fragment', () => { it('should support fragment', () => { const config = [{path: 'a', component: ComponentA}]; - checkRecognize(config, 'a#f1', (s: RouterStateSnapshot) => { - expect(s.root.fragment).toEqual('f1'); - }); - }); - }); - - describe('error handling', () => { - it('should error when two routes with the same outlet name got matched', () => { - recognize( - RootComponent, - [ - {path: 'a', component: ComponentA}, {path: 'b', component: ComponentB, outlet: 'aux'}, - {path: 'c', component: ComponentC, outlet: 'aux'} - ], - tree('a(aux:b//aux:c)'), 'a(aux:b//aux:c)') - .subscribe((_) => {}, (s: RouterStateSnapshot) => { - expect(s.toString()) - .toContain( - 'Two segments cannot have the same outlet name: \'aux:b\' and \'aux:c\'.'); - }); + const s = recognize(config, 'a#f1'); + expect(s.root.fragment).toEqual('f1'); }); }); }); -function checkRecognize( - config: Routes, url: string, callback: any, - paramsInheritanceStrategy?: 'emptyOnly'|'always'): void { - recognize(RootComponent, config, tree(url), url, paramsInheritanceStrategy) - .subscribe(callback, e => { - throw e; - }); +function recognize( + config: Routes, url: string, paramsInheritanceStrategy: 'emptyOnly'|'always' = 'emptyOnly', + relativeLinkResolution: 'legacy'|'corrected' = 'legacy'): RouterStateSnapshot { + const result = + new Recognizer( + RootComponent, config, tree(url), url, paramsInheritanceStrategy, relativeLinkResolution) + .recognize(); + expect(result).not.toBeNull(); + return result!; } function checkActivatedRoute(