From 2d0effbfecb3a270af375449e5f33ce3e0f5ff5b Mon Sep 17 00:00:00 2001 From: Martin Sikora Date: Sun, 10 Jan 2021 19:55:39 +0100 Subject: [PATCH] test(router): add failing Router regression test for hash location strategy This situation can probably happen only when using HashLocationStrategy and by manually changing hash that triggers a route guard that returns a new UrlTree. Then hash in the browser might not match the current route because navigation was canceled, while hash in the URL remained set by the user. Related to #37048 --- .../test/regression_integration.spec.ts | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/router/test/regression_integration.spec.ts b/packages/router/test/regression_integration.spec.ts index 9dd5b8ba323b65..9f8d9218bde714 100644 --- a/packages/router/test/regression_integration.spec.ts +++ b/packages/router/test/regression_integration.spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {CommonModule} from '@angular/common'; +import {CommonModule, Location} from '@angular/common'; import {ChangeDetectionStrategy, Component, ContentChild, NgModule, TemplateRef, Type, ViewChild, ViewContainerRef} from '@angular/core'; import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; import {Router} from '@angular/router'; @@ -187,6 +187,48 @@ describe('Integration', () => { expect(fixture.nativeElement.innerHTML).toContain('isActive: true'); })); }); + + describe('useHash', () => { + xit('should restore hash to match current route - #28561', fakeAsync(() => { + @Component({selector: 'root-cmp', template: ``}) + class RootCmp { + } + + @Component({template: 'simple'}) + class SimpleCmp { + } + + TestBed.configureTestingModule({ + imports: [RouterTestingModule.withRoutes([ + {path: '', component: SimpleCmp}, + {path: 'one', component: SimpleCmp, canActivate: ['returnRootUrlTree']} + ])], + declarations: [SimpleCmp, RootCmp], + providers: [ + { + provide: 'returnRootUrlTree', + useFactory: (router: Router) => () => { + return router.parseUrl('/'); + }, + deps: [Router] + }, + ], + }); + + const router = TestBed.inject(Router); + const location = TestBed.inject(Location); + + router.navigateByUrl('/'); + // Will setup location change listeners + const fixture = createRoot(router, RootCmp); + + (location).simulateHashChange('/one'); + advance(fixture); + + expect(location.path()).toEqual('/'); + expect((location).urlChanges).toEqual(['replace: /', 'hash: /one', 'replace: /']); + })); + }); }); function advance(fixture: ComponentFixture): void {