| 
									
										
										
										
											2019-02-11 14:18:29 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | import {CommonModule, PathLocationStrategy} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | import {inject, TestBed} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2019-02-11 14:18:29 -08:00
										 |  |  | import {UpgradeModule} from '@angular/upgrade/static'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-25 09:36:49 -05:00
										 |  |  | import {$locationShim} from '../src/location_shim'; | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import {LocationUpgradeTestModule} from './upgrade_location_test_module'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class MockUpgradeModule { | 
					
						
							|  |  |  |   $injector = { | 
					
						
							|  |  |  |     get(key: string) { | 
					
						
							|  |  |  |       if (key === '$rootScope') { | 
					
						
							|  |  |  |         return new $rootScopeMock(); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         throw new Error(`Unsupported mock service requested: ${key}`); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function injectorFactory() { | 
					
						
							|  |  |  |   const rootScopeMock = new $rootScopeMock(); | 
					
						
							|  |  |  |   const rootElementMock = {on: () => undefined}; | 
					
						
							|  |  |  |   return function $injectorGet(provider: string) { | 
					
						
							|  |  |  |     if (provider === '$rootScope') { | 
					
						
							|  |  |  |       return rootScopeMock; | 
					
						
							|  |  |  |     } else if (provider === '$rootElement') { | 
					
						
							|  |  |  |       return rootElementMock; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       throw new Error(`Unsupported injectable mock: ${provider}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class $rootScopeMock { | 
					
						
							|  |  |  |   private watchers: any[] = []; | 
					
						
							|  |  |  |   private events: {[k: string]: any[]} = {}; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   runWatchers() { | 
					
						
							|  |  |  |     this.watchers.forEach(fn => fn()); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   $watch(fn: any) { | 
					
						
							|  |  |  |     this.watchers.push(fn); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   $broadcast(evt: string, ...args: any[]) { | 
					
						
							|  |  |  |     if (this.events[evt]) { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |       this.events[evt].forEach(fn => { | 
					
						
							|  |  |  |         fn.apply(fn, args); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     return { | 
					
						
							|  |  |  |       defaultPrevented: false, | 
					
						
							|  |  |  |       preventDefault() { | 
					
						
							|  |  |  |         this.defaultPrevented = true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $on(evt: string, fn: any) { | 
					
						
							|  |  |  |     if (!this.events[evt]) { | 
					
						
							|  |  |  |       this.events[evt] = []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     this.events[evt].push(fn); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   $evalAsync(fn: any) { | 
					
						
							|  |  |  |     fn(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('LocationProvider', () => { | 
					
						
							| 
									
										
										
										
											2019-02-11 14:18:29 -08:00
										 |  |  |   let upgradeModule: UpgradeModule; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |       imports: [ | 
					
						
							|  |  |  |         LocationUpgradeTestModule.config(), | 
					
						
							|  |  |  |       ], | 
					
						
							| 
									
										
										
										
											2019-02-11 14:18:29 -08:00
										 |  |  |       providers: [UpgradeModule], | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-28 16:22:36 -07:00
										 |  |  |     upgradeModule = TestBed.inject(UpgradeModule); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |     upgradeModule.$injector = {get: injectorFactory()}; | 
					
						
							| 
									
										
										
										
											2019-02-11 14:18:29 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 10:21:19 -07:00
										 |  |  |   it('should instantiate LocationProvider', inject([$locationShim], ($location: $locationShim) => { | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |        expect($location).toBeDefined(); | 
					
						
							| 
									
										
										
										
											2019-04-24 10:21:19 -07:00
										 |  |  |        expect($location instanceof $locationShim).toBe(true); | 
					
						
							| 
									
										
										
										
											2019-02-11 14:18:29 -08:00
										 |  |  |      })); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('LocationHtml5Url', function() { | 
					
						
							| 
									
										
										
										
											2019-04-24 10:21:19 -07:00
										 |  |  |   let $location: $locationShim; | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |   let upgradeModule: UpgradeModule; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       imports: [ | 
					
						
							|  |  |  |         CommonModule, | 
					
						
							|  |  |  |         LocationUpgradeTestModule.config( | 
					
						
							|  |  |  |             {useHash: false, appBaseHref: '/pre', startUrl: 'http://server'}), | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       providers: [UpgradeModule], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-08-28 16:22:36 -07:00
										 |  |  |     upgradeModule = TestBed.inject(UpgradeModule); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |     upgradeModule.$injector = {get: injectorFactory()}; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   beforeEach(inject([$locationShim], (loc: $locationShim) => { | 
					
						
							|  |  |  |     $location = loc; | 
					
						
							|  |  |  |   })); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should set the URL', () => { | 
					
						
							|  |  |  |     $location.url(''); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://server/pre/'); | 
					
						
							|  |  |  |     $location.url('/test'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://server/pre/test'); | 
					
						
							|  |  |  |     $location.url('test'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://server/pre/test'); | 
					
						
							|  |  |  |     $location.url('/somewhere?something=1#hash_here'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://server/pre/somewhere?something=1#hash_here'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should rewrite regular URL', () => { | 
					
						
							|  |  |  |     expect(parseLinkAndReturn($location, 'http://other')).toEqual(undefined); | 
					
						
							|  |  |  |     expect(parseLinkAndReturn($location, 'http://server/pre')).toEqual('http://server/pre/'); | 
					
						
							|  |  |  |     expect(parseLinkAndReturn($location, 'http://server/pre/')).toEqual('http://server/pre/'); | 
					
						
							|  |  |  |     expect(parseLinkAndReturn($location, 'http://server/pre/otherPath')) | 
					
						
							|  |  |  |         .toEqual('http://server/pre/otherPath'); | 
					
						
							|  |  |  |     // Note: relies on the previous state!
 | 
					
						
							|  |  |  |     expect(parseLinkAndReturn($location, 'someIgnoredAbsoluteHref', '#test')) | 
					
						
							|  |  |  |         .toEqual('http://server/pre/otherPath#test'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should rewrite index URL', () => { | 
					
						
							|  |  |  |     // Reset hostname url and hostname
 | 
					
						
							|  |  |  |     $location.$$parseLinkUrl('http://server/pre/index.html'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toEqual('http://server/pre/'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(parseLinkAndReturn($location, 'http://server/pre')).toEqual('http://server/pre/'); | 
					
						
							|  |  |  |     expect(parseLinkAndReturn($location, 'http://server/pre/')).toEqual('http://server/pre/'); | 
					
						
							|  |  |  |     expect(parseLinkAndReturn($location, 'http://server/pre/otherPath')) | 
					
						
							|  |  |  |         .toEqual('http://server/pre/otherPath'); | 
					
						
							|  |  |  |     // Note: relies on the previous state!
 | 
					
						
							|  |  |  |     expect(parseLinkAndReturn($location, 'someIgnoredAbsoluteHref', '#test')) | 
					
						
							|  |  |  |         .toEqual('http://server/pre/otherPath#test'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should complain if the path starts with double slashes', function() { | 
					
						
							|  |  |  |     expect(function() { | 
					
						
							|  |  |  |       parseLinkAndReturn($location, 'http://server/pre///other/path'); | 
					
						
							|  |  |  |     }).toThrow(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(function() { | 
					
						
							|  |  |  |       parseLinkAndReturn($location, 'http://server/pre/\\\\other/path'); | 
					
						
							|  |  |  |     }).toThrow(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(function() { | 
					
						
							|  |  |  |       parseLinkAndReturn($location, 'http://server/pre//\\//other/path'); | 
					
						
							|  |  |  |     }).toThrow(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   it('should support state', function() { | 
					
						
							|  |  |  |     expect($location.state({a: 2}).state()).toEqual({a: 2}); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('NewUrl', function() { | 
					
						
							| 
									
										
										
										
											2019-04-24 10:21:19 -07:00
										 |  |  |   let $location: $locationShim; | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |   let upgradeModule: UpgradeModule; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       imports: [ | 
					
						
							|  |  |  |         CommonModule, | 
					
						
							|  |  |  |         LocationUpgradeTestModule.config({useHash: false, startUrl: 'http://www.domain.com:9877'}), | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       providers: [UpgradeModule], | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-28 16:22:36 -07:00
										 |  |  |     upgradeModule = TestBed.inject(UpgradeModule); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |     upgradeModule.$injector = {get: injectorFactory()}; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   beforeEach(inject([$locationShim], (loc: $locationShim) => { | 
					
						
							|  |  |  |     $location = loc; | 
					
						
							|  |  |  |   })); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Sets the default most of these tests rely on
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   function setupUrl(url = '/path/b?search=a&b=c&d#hash') { | 
					
						
							|  |  |  |     $location.url(url); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('should provide common getters', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d#hash'); | 
					
						
							|  |  |  |     expect($location.protocol()).toBe('http'); | 
					
						
							|  |  |  |     expect($location.host()).toBe('www.domain.com'); | 
					
						
							|  |  |  |     expect($location.port()).toBe(9877); | 
					
						
							|  |  |  |     expect($location.path()).toBe('/path/b'); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({search: 'a', b: 'c', d: true}); | 
					
						
							|  |  |  |     expect($location.hash()).toBe('hash'); | 
					
						
							|  |  |  |     expect($location.url()).toBe('/path/b?search=a&b=c&d#hash'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('path() should change path', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.path('/new/path'); | 
					
						
							|  |  |  |     expect($location.path()).toBe('/new/path'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/new/path?search=a&b=c&d#hash'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('path() should not break on numeric values', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.path(1); | 
					
						
							|  |  |  |     expect($location.path()).toBe('/1'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/1?search=a&b=c&d#hash'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('path() should allow using 0 as path', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.path(0); | 
					
						
							|  |  |  |     expect($location.path()).toBe('/0'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/0?search=a&b=c&d#hash'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('path() should set to empty path on null value', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.path('/foo'); | 
					
						
							|  |  |  |     expect($location.path()).toBe('/foo'); | 
					
						
							|  |  |  |     $location.path(null); | 
					
						
							|  |  |  |     expect($location.path()).toBe('/'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('search() should accept string', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.search('x=y&c'); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({x: 'y', c: true}); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/path/b?x=y&c#hash'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('search() should accept object', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.search({one: 1, two: true}); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({one: 1, two: true}); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/path/b?one=1&two#hash'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('search() should copy object', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     let obj = {one: 1, two: true, three: null}; | 
					
						
							|  |  |  |     $location.search(obj); | 
					
						
							|  |  |  |     expect(obj).toEqual({one: 1, two: true, three: null}); | 
					
						
							|  |  |  |     obj.one = 100;  // changed value
 | 
					
						
							|  |  |  |     expect($location.search()).toEqual({one: 1, two: true}); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/path/b?one=1&two#hash'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('search() should change single parameter', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.search({id: 'old', preserved: true}); | 
					
						
							|  |  |  |     $location.search('id', 'new'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect($location.search()).toEqual({id: 'new', preserved: true}); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('search() should remove single parameter', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.search({id: 'old', preserved: true}); | 
					
						
							|  |  |  |     $location.search('id', null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect($location.search()).toEqual({preserved: true}); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('search() should remove multiple parameters', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.search({one: 1, two: true}); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({one: 1, two: true}); | 
					
						
							|  |  |  |     $location.search({one: null, two: null}); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({}); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/path/b#hash'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('search() should accept numeric keys', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.search({1: 'one', 2: 'two'}); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({'1': 'one', '2': 'two'}); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/path/b?1=one&2=two#hash'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('search() should handle multiple value', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.search('a&b'); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({a: true, b: true}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $location.search('a', null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect($location.search()).toEqual({b: true}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $location.search('b', undefined); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({}); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('search() should handle single value', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.search('ignore'); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({ignore: true}); | 
					
						
							|  |  |  |     $location.search(1); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({1: true}); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('search() should throw error an incorrect argument', function() { | 
					
						
							|  |  |  |     expect(() => { | 
					
						
							|  |  |  |       $location.search((null as any)); | 
					
						
							|  |  |  |     }).toThrowError('LocationProvider.search(): First argument must be a string or an object.'); | 
					
						
							|  |  |  |     expect(function() { | 
					
						
							|  |  |  |       $location.search((undefined as any)); | 
					
						
							|  |  |  |     }).toThrowError('LocationProvider.search(): First argument must be a string or an object.'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('hash() should change hash fragment', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.hash('new-hash'); | 
					
						
							|  |  |  |     expect($location.hash()).toBe('new-hash'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d#new-hash'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('hash() should accept numeric parameter', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.hash(5); | 
					
						
							|  |  |  |     expect($location.hash()).toBe('5'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d#5'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('hash() should allow using 0', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.hash(0); | 
					
						
							|  |  |  |     expect($location.hash()).toBe('0'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d#0'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('hash() should accept null parameter', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.hash(null); | 
					
						
							|  |  |  |     expect($location.hash()).toBe(''); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('url() should change the path, search and hash', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.url('/some/path?a=b&c=d#hhh'); | 
					
						
							|  |  |  |     expect($location.url()).toBe('/some/path?a=b&c=d#hhh'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/some/path?a=b&c=d#hhh'); | 
					
						
							|  |  |  |     expect($location.path()).toBe('/some/path'); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({a: 'b', c: 'd'}); | 
					
						
							|  |  |  |     expect($location.hash()).toBe('hhh'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('url() should change only hash when no search and path specified', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.url('#some-hash'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect($location.hash()).toBe('some-hash'); | 
					
						
							|  |  |  |     expect($location.url()).toBe('/path/b?search=a&b=c&d#some-hash'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d#some-hash'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('url() should change only search and hash when no path specified', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.url('?a=b'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect($location.search()).toEqual({a: 'b'}); | 
					
						
							|  |  |  |     expect($location.hash()).toBe(''); | 
					
						
							|  |  |  |     expect($location.path()).toBe('/path/b'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('url() should reset search and hash when only path specified', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.url('/new/path'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect($location.path()).toBe('/new/path'); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({}); | 
					
						
							|  |  |  |     expect($location.hash()).toBe(''); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('url() should change path when empty string specified', function() { | 
					
						
							|  |  |  |     setupUrl(); | 
					
						
							|  |  |  |     $location.url(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect($location.path()).toBe('/'); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({}); | 
					
						
							|  |  |  |     expect($location.hash()).toBe(''); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('replace should set $$replace flag and return itself', function() { | 
					
						
							|  |  |  |     expect(($location as any).$$replace).toBe(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $location.replace(); | 
					
						
							|  |  |  |     expect(($location as any).$$replace).toBe(true); | 
					
						
							|  |  |  |     expect($location.replace()).toBe($location); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('encoding', function() { | 
					
						
							|  |  |  |     it('should encode special characters', function() { | 
					
						
							|  |  |  |       $location.path('/a <>#'); | 
					
						
							|  |  |  |       $location.search({'i j': '<>#'}); | 
					
						
							|  |  |  |       $location.hash('<>#'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect($location.path()).toBe('/a <>#'); | 
					
						
							|  |  |  |       expect($location.search()).toEqual({'i j': '<>#'}); | 
					
						
							|  |  |  |       expect($location.hash()).toBe('<>#'); | 
					
						
							|  |  |  |       expect($location.absUrl()) | 
					
						
							|  |  |  |           .toBe('http://www.domain.com:9877/a%20%3C%3E%23?i%20j=%3C%3E%23#%3C%3E%23'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should not encode !$:@', function() { | 
					
						
							|  |  |  |       $location.path('/!$:@'); | 
					
						
							|  |  |  |       $location.search(''); | 
					
						
							|  |  |  |       $location.hash('!$:@'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect($location.absUrl()).toBe('http://www.domain.com:9877/!$:@#!$:@'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should decode special characters', function() { | 
					
						
							|  |  |  |       $location.$$parse('http://www.domain.com:9877/a%20%3C%3E%23?i%20j=%3C%3E%23#x%20%3C%3E%23'); | 
					
						
							|  |  |  |       expect($location.path()).toBe('/a <>#'); | 
					
						
							|  |  |  |       expect($location.search()).toEqual({'i j': '<>#'}); | 
					
						
							|  |  |  |       expect($location.hash()).toBe('x <>#'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should not decode encoded forward slashes in the path', function() { | 
					
						
							|  |  |  |       $location.$$parse('http://www.domain.com:9877/a/ng2;path=%2Fsome%2Fpath'); | 
					
						
							|  |  |  |       expect($location.path()).toBe('/a/ng2;path=%2Fsome%2Fpath'); | 
					
						
							|  |  |  |       expect($location.search()).toEqual({}); | 
					
						
							|  |  |  |       expect($location.hash()).toBe(''); | 
					
						
							|  |  |  |       expect($location.url()).toBe('/a/ng2;path=%2Fsome%2Fpath'); | 
					
						
							|  |  |  |       expect($location.absUrl()).toBe('http://www.domain.com:9877/a/ng2;path=%2Fsome%2Fpath'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should decode pluses as spaces in urls', function() { | 
					
						
							|  |  |  |       $location.$$parse('http://www.domain.com:9877/?a+b=c+d'); | 
					
						
							|  |  |  |       expect($location.search()).toEqual({'a b': 'c d'}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should retain pluses when setting search queries', function() { | 
					
						
							|  |  |  |       $location.search({'a+b': 'c+d'}); | 
					
						
							|  |  |  |       expect($location.search()).toEqual({'a+b': 'c+d'}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should not preserve old properties when parsing new url', function() { | 
					
						
							|  |  |  |     $location.$$parse('http://www.domain.com:9877/a'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect($location.path()).toBe('/a'); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({}); | 
					
						
							|  |  |  |     expect($location.hash()).toBe(''); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://www.domain.com:9877/a'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('New URL Parsing', () => { | 
					
						
							| 
									
										
										
										
											2019-04-24 10:21:19 -07:00
										 |  |  |   let $location: $locationShim; | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |   let upgradeModule: UpgradeModule; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       imports: [ | 
					
						
							|  |  |  |         CommonModule, | 
					
						
							|  |  |  |         LocationUpgradeTestModule.config( | 
					
						
							|  |  |  |             {useHash: false, appBaseHref: '/base', startUrl: 'http://server'}), | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       providers: [UpgradeModule], | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-28 16:22:36 -07:00
										 |  |  |     upgradeModule = TestBed.inject(UpgradeModule); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |     upgradeModule.$injector = {get: injectorFactory()}; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   beforeEach(inject([$locationShim], (loc: $locationShim) => { | 
					
						
							|  |  |  |     $location = loc; | 
					
						
							|  |  |  |   })); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('should prepend path with basePath', function() { | 
					
						
							|  |  |  |     $location.$$parse('http://server/base/abc?a'); | 
					
						
							|  |  |  |     expect($location.path()).toBe('/abc'); | 
					
						
							|  |  |  |     expect($location.search()).toEqual({a: true}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $location.path('/new/path'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://server/base/new/path?a'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('New URL Parsing', () => { | 
					
						
							| 
									
										
										
										
											2019-04-24 10:21:19 -07:00
										 |  |  |   let $location: $locationShim; | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |   let upgradeModule: UpgradeModule; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       imports: [ | 
					
						
							|  |  |  |         CommonModule, | 
					
						
							|  |  |  |         LocationUpgradeTestModule.config({useHash: false, startUrl: 'http://host.com/'}), | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       providers: [UpgradeModule], | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-28 16:22:36 -07:00
										 |  |  |     upgradeModule = TestBed.inject(UpgradeModule); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |     upgradeModule.$injector = {get: injectorFactory()}; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   beforeEach(inject([$locationShim], (loc: $locationShim) => { | 
					
						
							|  |  |  |     $location = loc; | 
					
						
							|  |  |  |   })); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('should parse new url', function() { | 
					
						
							|  |  |  |     $location.$$parse('http://host.com/base'); | 
					
						
							|  |  |  |     expect($location.path()).toBe('/base'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should parse new url with #', function() { | 
					
						
							|  |  |  |     $location.$$parse('http://host.com/base#'); | 
					
						
							|  |  |  |     expect($location.path()).toBe('/base'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should prefix path with forward-slash', function() { | 
					
						
							|  |  |  |     $location.path('b'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect($location.path()).toBe('/b'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://host.com/b'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should set path to forward-slash when empty', function() { | 
					
						
							|  |  |  |     $location.$$parse('http://host.com/'); | 
					
						
							|  |  |  |     expect($location.path()).toBe('/'); | 
					
						
							|  |  |  |     expect($location.absUrl()).toBe('http://host.com/'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('setters should return Url object to allow chaining', function() { | 
					
						
							|  |  |  |     expect($location.path('/any')).toBe($location); | 
					
						
							|  |  |  |     expect($location.search('')).toBe($location); | 
					
						
							|  |  |  |     expect($location.hash('aaa')).toBe($location); | 
					
						
							|  |  |  |     expect($location.url('/some')).toBe($location); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should throw error when invalid server url given', function() { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     expect(function() { | 
					
						
							|  |  |  |       $location.$$parse('http://other.server.org/path#/path'); | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |         .toThrowError( | 
					
						
							|  |  |  |             'Invalid url "http://other.server.org/path#/path", missing path prefix "http://host.com/".'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('state', function() { | 
					
						
							|  |  |  |     let mock$rootScope: $rootScopeMock; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(inject([UpgradeModule], (ngUpgrade: UpgradeModule) => { | 
					
						
							|  |  |  |       mock$rootScope = ngUpgrade.$injector.get('$rootScope'); | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should set $$state and return itself', function() { | 
					
						
							|  |  |  |       expect(($location as any).$$state).toEqual(null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       let returned = $location.state({a: 2}); | 
					
						
							|  |  |  |       expect(($location as any).$$state).toEqual({a: 2}); | 
					
						
							|  |  |  |       expect(returned).toBe($location); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should set state', function() { | 
					
						
							|  |  |  |       $location.state({a: 2}); | 
					
						
							|  |  |  |       expect($location.state()).toEqual({a: 2}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should allow to set both URL and state', function() { | 
					
						
							|  |  |  |       $location.url('/foo').state({a: 2}); | 
					
						
							|  |  |  |       expect($location.url()).toEqual('/foo'); | 
					
						
							|  |  |  |       expect($location.state()).toEqual({a: 2}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should allow to mix state and various URL functions', function() { | 
					
						
							|  |  |  |       $location.path('/foo').hash('abcd').state({a: 2}).search('bar', 'baz'); | 
					
						
							|  |  |  |       expect($location.path()).toEqual('/foo'); | 
					
						
							|  |  |  |       expect($location.state()).toEqual({a: 2}); | 
					
						
							|  |  |  |       expect($location.search() && $location.search().bar).toBe('baz'); | 
					
						
							|  |  |  |       expect($location.hash()).toEqual('abcd'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should always have the same value by reference until the value is changed', function() { | 
					
						
							|  |  |  |       expect(($location as any).$$state).toEqual(null); | 
					
						
							|  |  |  |       expect($location.state()).toEqual(null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const stateValue = {foo: 'bar'}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $location.state(stateValue); | 
					
						
							|  |  |  |       expect($location.state()).toBe(stateValue); | 
					
						
							|  |  |  |       mock$rootScope.runWatchers(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const testState = $location.state(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // $location.state() should equal by reference
 | 
					
						
							|  |  |  |       expect($location.state()).toEqual(stateValue); | 
					
						
							|  |  |  |       expect($location.state()).toBe(testState); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       mock$rootScope.runWatchers(); | 
					
						
							|  |  |  |       expect($location.state()).toBe(testState); | 
					
						
							|  |  |  |       mock$rootScope.runWatchers(); | 
					
						
							|  |  |  |       expect($location.state()).toBe(testState); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Confirm updating other values doesn't change the value of `state`
 | 
					
						
							|  |  |  |       $location.path('/new'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect($location.state()).toBe(testState); | 
					
						
							|  |  |  |       mock$rootScope.runWatchers(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // After watchers have been run, location should be updated and `state` should change
 | 
					
						
							|  |  |  |       expect($location.state()).toBe(null); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  | describe('$location.onChange()', () => { | 
					
						
							|  |  |  |   let $location: $locationShim; | 
					
						
							|  |  |  |   let upgradeModule: UpgradeModule; | 
					
						
							| 
									
										
										
										
											2019-08-07 06:52:04 -07:00
										 |  |  |   let mock$rootScope: $rootScopeMock; | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       imports: [ | 
					
						
							|  |  |  |         CommonModule, | 
					
						
							|  |  |  |         LocationUpgradeTestModule.config({useHash: false, startUrl: 'http://host.com/'}), | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       providers: [UpgradeModule], | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-28 16:22:36 -07:00
										 |  |  |     upgradeModule = TestBed.inject(UpgradeModule); | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  |     upgradeModule.$injector = {get: injectorFactory()}; | 
					
						
							| 
									
										
										
										
											2019-08-07 06:52:04 -07:00
										 |  |  |     mock$rootScope = upgradeModule.$injector.get('$rootScope'); | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   beforeEach(inject([$locationShim], (loc: $locationShim) => { | 
					
						
							|  |  |  |     $location = loc; | 
					
						
							|  |  |  |   })); | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   it('should have onChange method', () => { | 
					
						
							|  |  |  |     expect(typeof $location.onChange).toBe('function'); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('should add registered functions to changeListeners', () => { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     function changeListener(url: string, state: unknown) { | 
					
						
							|  |  |  |       return undefined; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  |     function errorHandler(e: Error) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(($location as any).$$changeListeners.length).toBe(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $location.onChange(changeListener, errorHandler); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(($location as any).$$changeListeners.length).toBe(1); | 
					
						
							|  |  |  |     expect(($location as any).$$changeListeners[0][0]).toEqual(changeListener); | 
					
						
							|  |  |  |     expect(($location as any).$$changeListeners[0][1]).toEqual(errorHandler); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should call changeListeners when URL is updated', () => { | 
					
						
							|  |  |  |     const onChangeVals = | 
					
						
							|  |  |  |         {url: 'url', state: 'state' as unknown, oldUrl: 'oldUrl', oldState: 'oldState' as unknown}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function changeListener(url: string, state: unknown, oldUrl: string, oldState: unknown) { | 
					
						
							|  |  |  |       onChangeVals.url = url; | 
					
						
							|  |  |  |       onChangeVals.state = state; | 
					
						
							|  |  |  |       onChangeVals.oldUrl = oldUrl; | 
					
						
							|  |  |  |       onChangeVals.oldState = oldState; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $location.onChange(changeListener); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const newState = {foo: 'bar'}; | 
					
						
							| 
									
										
										
										
											2019-08-07 06:52:04 -07:00
										 |  |  |     $location.state(newState); | 
					
						
							|  |  |  |     $location.path('/newUrl'); | 
					
						
							|  |  |  |     mock$rootScope.runWatchers(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  |     expect(onChangeVals.url).toBe('/newUrl'); | 
					
						
							| 
									
										
										
										
											2019-08-07 06:52:04 -07:00
										 |  |  |     expect(onChangeVals.state).toEqual(newState); | 
					
						
							|  |  |  |     expect(onChangeVals.oldUrl).toBe('http://host.com'); | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  |     expect(onChangeVals.oldState).toBe(null); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-07 06:52:04 -07:00
										 |  |  |   it('should call changeListeners after $locationChangeSuccess', () => { | 
					
						
							|  |  |  |     let changeListenerCalled = false; | 
					
						
							|  |  |  |     let locationChangeSuccessEmitted = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function changeListener(url: string, state: unknown, oldUrl: string, oldState: unknown) { | 
					
						
							|  |  |  |       changeListenerCalled = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $location.onChange(changeListener); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mock$rootScope.$on('$locationChangeSuccess', () => { | 
					
						
							|  |  |  |       // Ensure that the changeListener hasn't been called yet
 | 
					
						
							|  |  |  |       expect(changeListenerCalled).toBe(false); | 
					
						
							|  |  |  |       locationChangeSuccessEmitted = true; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Update state and run watchers
 | 
					
						
							|  |  |  |     const stateValue = {foo: 'bar'}; | 
					
						
							|  |  |  |     $location.state(stateValue); | 
					
						
							|  |  |  |     mock$rootScope.runWatchers(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Ensure that change listeners are called and location events are emitted
 | 
					
						
							|  |  |  |     expect(changeListenerCalled).toBe(true); | 
					
						
							|  |  |  |     expect(locationChangeSuccessEmitted).toBe(true); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  |   it('should call forward errors to error handler', () => { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     let error!: Error; | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function changeListener(url: string, state: unknown, oldUrl: string, oldState: unknown) { | 
					
						
							|  |  |  |       throw new Error('Handle error'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     function errorHandler(e: Error) { | 
					
						
							|  |  |  |       error = e; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $location.onChange(changeListener, errorHandler); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-07 06:52:04 -07:00
										 |  |  |     $location.url('/newUrl'); | 
					
						
							|  |  |  |     mock$rootScope.runWatchers(); | 
					
						
							| 
									
										
										
										
											2019-05-14 15:00:21 -07:00
										 |  |  |     expect(error.message).toBe('Handle error'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 10:21:19 -07:00
										 |  |  | function parseLinkAndReturn(location: $locationShim, toUrl: string, relHref?: string) { | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  |   const resetUrl = location.$$parseLinkUrl(toUrl, relHref); | 
					
						
							|  |  |  |   return resetUrl && location.absUrl() || undefined; | 
					
						
							|  |  |  | } |