Skip to content

Commit

Permalink
test(router): add failing Router regression test for hash location st…
Browse files Browse the repository at this point in the history
…rategy

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 angular#37048
  • Loading branch information
martinsik committed Jan 27, 2021
1 parent b48eabd commit 2d0effb
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion packages/router/test/regression_integration.spec.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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: `<router-outlet></router-outlet>`})
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);

(<any>location).simulateHashChange('/one');
advance(fixture);

expect(location.path()).toEqual('/');
expect((<any>location).urlChanges).toEqual(['replace: /', 'hash: /one', 'replace: /']);
}));
});
});

function advance<T>(fixture: ComponentFixture<T>): void {
Expand Down

0 comments on commit 2d0effb

Please sign in to comment.