77 */
88
99import { Location } from '@angular/common' ;
10- import { EnvironmentInjector , inject } from '@angular/core' ;
10+ import { EnvironmentInjector , inject , ɵConsole as Console } from '@angular/core' ;
1111import { TestBed } from '@angular/core/testing' ;
1212import { RouterModule } from '../index' ;
1313import { of } from 'rxjs' ;
@@ -20,7 +20,7 @@ import {resolveData as resolveDataOperator} from '../src/operators/resolve_data'
2020import { Router } from '../src/router' ;
2121import { ChildrenOutletContexts } from '../src/router_outlet_context' ;
2222import { createEmptyStateSnapshot , RouterStateSnapshot } from '../src/router_state' ;
23- import { DefaultUrlSerializer , UrlTree } from '../src/url_tree' ;
23+ import { DefaultUrlSerializer , UrlSerializer , UrlTree } from '../src/url_tree' ;
2424import { getAllRouteGuards } from '../src/utils/preactivation' ;
2525import { TreeNode } from '../src/utils/tree' ;
2626
@@ -108,6 +108,30 @@ describe('Router', () => {
108108 } ) ;
109109 } ) ;
110110
111+ describe ( 'parseUrl' , ( ) => {
112+ beforeEach ( ( ) => {
113+ TestBed . configureTestingModule ( { imports : [ RouterModule . forRoot ( [ ] ) ] } ) ;
114+ } ) ;
115+
116+ it ( 'should log a warning and fall back to "/" when parsing fails' , ( ) => {
117+ const router : Router = TestBed . inject ( Router ) ;
118+ const urlSerializer : UrlSerializer = TestBed . inject ( UrlSerializer ) ;
119+ const console : Console = TestBed . inject ( Console ) ;
120+ spyOn ( urlSerializer , 'parse' ) . and . callFake ( ( url : string ) => {
121+ if ( url === 'invalid-url' ) {
122+ throw new Error ( 'test error' ) ;
123+ }
124+ // The fallback call should not be mocked
125+ return new DefaultUrlSerializer ( ) . parse ( url ) ;
126+ } ) ;
127+ const spy = spyOn ( console , 'warn' ) ;
128+
129+ const result = router . parseUrl ( 'invalid-url' ) ;
130+ expect ( spy . calls . argsFor ( 0 ) ) . toMatch ( / E r r o r p a r s i n g U R L / ) ;
131+ expect ( result ) . toEqual ( new DefaultUrlSerializer ( ) . parse ( '/' ) ) ;
132+ } ) ;
133+ } ) ;
134+
111135 describe ( 'PreActivation' , ( ) => {
112136 const serializer = new DefaultUrlSerializer ( ) ;
113137 let empty : RouterStateSnapshot ;
0 commit comments