|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Location} from '@angular/common'; |
| 10 | +import {TestBed} from '@angular/core/testing'; |
| 11 | +import {Router} from '@angular/router'; |
| 12 | +import {setUpLocationSync} from '@angular/router/upgrade'; |
| 13 | +import {UpgradeModule} from '@angular/upgrade/static'; |
| 14 | + |
| 15 | +describe('setUpLocationSync', () => { |
| 16 | + let upgradeModule: UpgradeModule; |
| 17 | + let RouterMock: any; |
| 18 | + let LocationMock: any; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + RouterMock = jasmine.createSpyObj('Router', ['navigateByUrl']); |
| 22 | + LocationMock = jasmine.createSpyObj('Location', ['normalize']); |
| 23 | + |
| 24 | + TestBed.configureTestingModule({ |
| 25 | + providers: [ |
| 26 | + UpgradeModule, {provide: Router, useValue: RouterMock}, |
| 27 | + {provide: Location, useValue: LocationMock} |
| 28 | + ], |
| 29 | + }); |
| 30 | + |
| 31 | + upgradeModule = TestBed.get(UpgradeModule); |
| 32 | + upgradeModule.$injector = { |
| 33 | + get: jasmine.createSpy('$injector.get').and.returnValue({'$on': () => undefined}) |
| 34 | + }; |
| 35 | + }); |
| 36 | + |
| 37 | + it('should throw an error if the UpgradeModule.bootstrap has not been called', () => { |
| 38 | + upgradeModule.$injector = null; |
| 39 | + |
| 40 | + expect(() => setUpLocationSync(upgradeModule)).toThrowError(` |
| 41 | + RouterUpgradeInitializer can be used only after UpgradeModule.bootstrap has been called. |
| 42 | + Remove RouterUpgradeInitializer and call setUpLocationSync after UpgradeModule.bootstrap. |
| 43 | + `); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should get the $rootScope from AngularJS and set an $on watch on $locationChangeStart', |
| 47 | + () => { |
| 48 | + const $rootScope = jasmine.createSpyObj('$rootScope', ['$on']); |
| 49 | + |
| 50 | + upgradeModule.$injector.get.and.callFake( |
| 51 | + (name: string) => (name === '$rootScope') && $rootScope); |
| 52 | + |
| 53 | + setUpLocationSync(upgradeModule); |
| 54 | + |
| 55 | + expect($rootScope.$on).toHaveBeenCalledTimes(1); |
| 56 | + expect($rootScope.$on).toHaveBeenCalledWith('$locationChangeStart', jasmine.any(Function)); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should navigate by url every time $locationChangeStart is broadcasted', () => { |
| 60 | + const url = 'https://google.com'; |
| 61 | + const pathname = '/custom/route'; |
| 62 | + const normalizedPathname = 'foo'; |
| 63 | + const query = '?query=1&query2=3'; |
| 64 | + const hash = '#new/hash'; |
| 65 | + const $rootScope = jasmine.createSpyObj('$rootScope', ['$on']); |
| 66 | + |
| 67 | + upgradeModule.$injector.get.and.returnValue($rootScope); |
| 68 | + LocationMock.normalize.and.returnValue(normalizedPathname); |
| 69 | + |
| 70 | + setUpLocationSync(upgradeModule); |
| 71 | + |
| 72 | + const callback = $rootScope.$on.calls.argsFor(0)[1]; |
| 73 | + callback({}, url + pathname + query + hash, ''); |
| 74 | + |
| 75 | + expect(LocationMock.normalize).toHaveBeenCalledTimes(1); |
| 76 | + expect(LocationMock.normalize).toHaveBeenCalledWith(pathname); |
| 77 | + |
| 78 | + expect(RouterMock.navigateByUrl).toHaveBeenCalledTimes(1); |
| 79 | + expect(RouterMock.navigateByUrl).toHaveBeenCalledWith(normalizedPathname + query + hash); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should work correctly on browsers that do not start pathname with `/`', () => { |
| 83 | + const anchorProto = HTMLAnchorElement.prototype; |
| 84 | + const originalDescriptor = Object.getOwnPropertyDescriptor(anchorProto, 'pathname'); |
| 85 | + Object.defineProperty(anchorProto, 'pathname', {get: () => 'foo/bar'}); |
| 86 | + |
| 87 | + try { |
| 88 | + const $rootScope = jasmine.createSpyObj('$rootScope', ['$on']); |
| 89 | + upgradeModule.$injector.get.and.returnValue($rootScope); |
| 90 | + |
| 91 | + setUpLocationSync(upgradeModule); |
| 92 | + |
| 93 | + const callback = $rootScope.$on.calls.argsFor(0)[1]; |
| 94 | + callback({}, '', ''); |
| 95 | + |
| 96 | + expect(LocationMock.normalize).toHaveBeenCalledWith('/foo/bar'); |
| 97 | + } finally { |
| 98 | + Object.defineProperty(anchorProto, 'pathname', originalDescriptor !); |
| 99 | + } |
| 100 | + }); |
| 101 | +}); |
0 commit comments