| 
									
										
										
										
											2017-05-26 11:02:31 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2017-05-26 11:02:31 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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, Location, LocationStrategy, PathLocationStrategy, PlatformLocation} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2020-06-02 23:05:39 +02:00
										 |  |  | import {MockLocationStrategy, MockPlatformLocation} from '@angular/common/testing'; | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  | import {TestBed} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2017-05-26 11:02:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 11:51:02 +01:00
										 |  |  | const baseUrl = '/base'; | 
					
						
							| 
									
										
										
										
											2017-05-26 11:02:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 11:51:02 +01:00
										 |  |  | describe('Location Class', () => { | 
					
						
							|  |  |  |   describe('stripTrailingSlash', () => { | 
					
						
							|  |  |  |     it('should strip single character slash', () => { | 
					
						
							|  |  |  |       const input = '/'; | 
					
						
							|  |  |  |       expect(Location.stripTrailingSlash(input)).toBe(''); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-05-26 11:02:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 11:51:02 +01:00
										 |  |  |     it('should normalize strip a trailing slash', () => { | 
					
						
							|  |  |  |       const input = baseUrl + '/'; | 
					
						
							|  |  |  |       expect(Location.stripTrailingSlash(input)).toBe(baseUrl); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-05-26 11:02:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 11:51:02 +01:00
										 |  |  |     it('should ignore query params when stripping a slash', () => { | 
					
						
							|  |  |  |       const input = baseUrl + '/?param=1'; | 
					
						
							|  |  |  |       expect(Location.stripTrailingSlash(input)).toBe(baseUrl + '?param=1'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-05-26 11:02:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 11:51:02 +01:00
										 |  |  |     it('should not remove slashes inside query params', () => { | 
					
						
							|  |  |  |       const input = baseUrl + '?test/?=3'; | 
					
						
							|  |  |  |       expect(Location.stripTrailingSlash(input)).toBe(input); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-05-26 11:02:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 11:51:02 +01:00
										 |  |  |     it('should not remove slashes after a pound sign', () => { | 
					
						
							|  |  |  |       const input = baseUrl + '#test/?=3'; | 
					
						
							|  |  |  |       expect(Location.stripTrailingSlash(input)).toBe(input); | 
					
						
							| 
									
										
										
										
											2017-05-26 11:02:31 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-02-06 14:42:57 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('location.getState()', () => { | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |     let location: Location; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-06 14:42:57 -08:00
										 |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       TestBed.configureTestingModule({ | 
					
						
							|  |  |  |         imports: [CommonModule], | 
					
						
							|  |  |  |         providers: [ | 
					
						
							|  |  |  |           {provide: LocationStrategy, useClass: PathLocationStrategy}, | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |           { | 
					
						
							|  |  |  |             provide: PlatformLocation, | 
					
						
							|  |  |  |             useFactory: () => { | 
					
						
							|  |  |  |               return new MockPlatformLocation(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           }, | 
					
						
							| 
									
										
										
										
											2019-02-06 14:42:57 -08:00
										 |  |  |           {provide: Location, useClass: Location, deps: [LocationStrategy, PlatformLocation]}, | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       location = TestBed.inject(Location); | 
					
						
							| 
									
										
										
										
											2019-02-06 14:42:57 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |     it('should get the state object', () => { | 
					
						
							|  |  |  |       expect(location.getState()).toBe(null); | 
					
						
							| 
									
										
										
										
											2019-02-06 14:42:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |       location.go('/test', '', {foo: 'bar'}); | 
					
						
							| 
									
										
										
										
											2019-02-06 14:42:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |       expect(location.getState()).toEqual({foo: 'bar'}); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-02-06 14:42:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |     it('should work after using back button', () => { | 
					
						
							|  |  |  |       expect(location.getState()).toBe(null); | 
					
						
							| 
									
										
										
										
											2019-02-06 14:42:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |       location.go('/test1', '', {url: 'test1'}); | 
					
						
							|  |  |  |       location.go('/test2', '', {url: 'test2'}); | 
					
						
							| 
									
										
										
										
											2019-02-06 14:42:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |       expect(location.getState()).toEqual({url: 'test2'}); | 
					
						
							| 
									
										
										
										
											2019-02-06 14:42:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |       location.back(); | 
					
						
							| 
									
										
										
										
											2019-02-06 14:42:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |       expect(location.getState()).toEqual({url: 'test1'}); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-02-06 14:42:57 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-04-22 15:48:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('location.onUrlChange()', () => { | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |     let location: Location; | 
					
						
							|  |  |  |     let locationStrategy: MockLocationStrategy; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 15:48:27 -07:00
										 |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       TestBed.configureTestingModule({ | 
					
						
							|  |  |  |         imports: [CommonModule], | 
					
						
							|  |  |  |         providers: [ | 
					
						
							| 
									
										
										
										
											2020-06-02 23:05:39 +02:00
										 |  |  |           {provide: LocationStrategy, useClass: MockLocationStrategy}, | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |           { | 
					
						
							|  |  |  |             provide: PlatformLocation, | 
					
						
							|  |  |  |             useFactory: () => { | 
					
						
							|  |  |  |               return new MockPlatformLocation(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           }, | 
					
						
							| 
									
										
										
										
											2019-04-22 15:48:27 -07:00
										 |  |  |           {provide: Location, useClass: Location, deps: [LocationStrategy, PlatformLocation]}, | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       location = TestBed.inject(Location); | 
					
						
							|  |  |  |       locationStrategy = TestBed.inject(LocationStrategy) as MockLocationStrategy; | 
					
						
							| 
									
										
										
										
											2019-04-22 15:48:27 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |     it('should have onUrlChange method', () => { | 
					
						
							|  |  |  |       expect(typeof location.onUrlChange).toBe('function'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-04-23 07:16:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |     it('should add registered functions to urlChangeListeners', () => { | 
					
						
							|  |  |  |       function changeListener(url: string, state: unknown) { | 
					
						
							|  |  |  |         return undefined; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-04-22 15:48:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |       expect((location as any)._urlChangeListeners.length).toBe(0); | 
					
						
							| 
									
										
										
										
											2019-04-22 15:48:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |       location.onUrlChange(changeListener); | 
					
						
							| 
									
										
										
										
											2019-04-22 15:48:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:24:16 +02:00
										 |  |  |       expect((location as any)._urlChangeListeners.length).toBe(1); | 
					
						
							|  |  |  |       expect((location as any)._urlChangeListeners[0]).toEqual(changeListener); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-06-02 23:05:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should only notify listeners once when multiple listeners are registered', () => { | 
					
						
							|  |  |  |       let notificationCount = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       function incrementChangeListener(url: string, state: unknown) { | 
					
						
							|  |  |  |         notificationCount += 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return undefined; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       function noopChangeListener(url: string, state: unknown) { | 
					
						
							|  |  |  |         return undefined; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       location.onUrlChange(incrementChangeListener); | 
					
						
							|  |  |  |       location.onUrlChange(noopChangeListener); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(notificationCount).toBe(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       locationStrategy.simulatePopState('/test'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(notificationCount).toBe(1); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-04-22 15:48:27 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  | }); |