|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC 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.dev/license |
| 7 | + */ |
| 8 | +import {LocationStrategy, Location, HashLocationStrategy} from '@angular/common'; |
| 9 | +import {fakeAsync, TestBed, inject} from '@angular/core/testing'; |
| 10 | +import {Router, NavigationStart, RoutesRecognized} from '@angular/router/src'; |
| 11 | +import {createRoot, RootCmp, BlankCmp, TeamCmp, advance} from './integration_helpers'; |
| 12 | + |
| 13 | +export function redirectsIntegrationSuite() { |
| 14 | + describe('redirects', () => { |
| 15 | + it('should work', fakeAsync( |
| 16 | + inject([Router, Location], (router: Router, location: Location) => { |
| 17 | + const fixture = createRoot(router, RootCmp); |
| 18 | + |
| 19 | + router.resetConfig([ |
| 20 | + {path: 'old/team/:id', redirectTo: 'team/:id'}, |
| 21 | + {path: 'team/:id', component: TeamCmp}, |
| 22 | + ]); |
| 23 | + |
| 24 | + router.navigateByUrl('old/team/22'); |
| 25 | + advance(fixture); |
| 26 | + |
| 27 | + expect(location.path()).toEqual('/team/22'); |
| 28 | + }), |
| 29 | + )); |
| 30 | + |
| 31 | + it('can redirect from componentless named outlets', fakeAsync(() => { |
| 32 | + const router = TestBed.inject(Router); |
| 33 | + const fixture = createRoot(router, RootCmp); |
| 34 | + |
| 35 | + router.resetConfig([ |
| 36 | + {path: 'main', outlet: 'aux', component: BlankCmp}, |
| 37 | + {path: '', pathMatch: 'full', outlet: 'aux', redirectTo: 'main'}, |
| 38 | + ]); |
| 39 | + |
| 40 | + router.navigateByUrl(''); |
| 41 | + advance(fixture); |
| 42 | + |
| 43 | + expect(TestBed.inject(Location).path()).toEqual('/(aux:main)'); |
| 44 | + })); |
| 45 | + |
| 46 | + it('should update Navigation object after redirects are applied', fakeAsync( |
| 47 | + inject([Router, Location], (router: Router, location: Location) => { |
| 48 | + const fixture = createRoot(router, RootCmp); |
| 49 | + let initialUrl, afterRedirectUrl; |
| 50 | + |
| 51 | + router.resetConfig([ |
| 52 | + {path: 'old/team/:id', redirectTo: 'team/:id'}, |
| 53 | + {path: 'team/:id', component: TeamCmp}, |
| 54 | + ]); |
| 55 | + |
| 56 | + router.events.subscribe((e) => { |
| 57 | + if (e instanceof NavigationStart) { |
| 58 | + const navigation = router.getCurrentNavigation(); |
| 59 | + initialUrl = navigation && navigation.finalUrl; |
| 60 | + } |
| 61 | + if (e instanceof RoutesRecognized) { |
| 62 | + const navigation = router.getCurrentNavigation(); |
| 63 | + afterRedirectUrl = navigation && navigation.finalUrl; |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + router.navigateByUrl('old/team/22'); |
| 68 | + advance(fixture); |
| 69 | + |
| 70 | + expect(initialUrl).toBeUndefined(); |
| 71 | + expect(router.serializeUrl(afterRedirectUrl as any)).toBe('/team/22'); |
| 72 | + }), |
| 73 | + )); |
| 74 | + |
| 75 | + it('should not break the back button when trigger by location change', fakeAsync(() => { |
| 76 | + TestBed.configureTestingModule({ |
| 77 | + providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}], |
| 78 | + }); |
| 79 | + const router = TestBed.inject(Router); |
| 80 | + const location = TestBed.inject(Location); |
| 81 | + const fixture = TestBed.createComponent(RootCmp); |
| 82 | + advance(fixture); |
| 83 | + router.resetConfig([ |
| 84 | + {path: 'initial', component: BlankCmp}, |
| 85 | + {path: 'old/team/:id', redirectTo: 'team/:id'}, |
| 86 | + {path: 'team/:id', component: TeamCmp}, |
| 87 | + ]); |
| 88 | + |
| 89 | + location.go('initial'); |
| 90 | + location.historyGo(0); |
| 91 | + location.go('old/team/22'); |
| 92 | + location.historyGo(0); |
| 93 | + |
| 94 | + // initial navigation |
| 95 | + router.initialNavigation(); |
| 96 | + advance(fixture); |
| 97 | + expect(location.path()).toEqual('/team/22'); |
| 98 | + |
| 99 | + location.back(); |
| 100 | + advance(fixture); |
| 101 | + expect(location.path()).toEqual('/initial'); |
| 102 | + |
| 103 | + // location change |
| 104 | + location.go('/old/team/33'); |
| 105 | + location.historyGo(0); |
| 106 | + |
| 107 | + advance(fixture); |
| 108 | + expect(location.path()).toEqual('/team/33'); |
| 109 | + |
| 110 | + location.back(); |
| 111 | + advance(fixture); |
| 112 | + expect(location.path()).toEqual('/initial'); |
| 113 | + })); |
| 114 | + }); |
| 115 | +} |
0 commit comments