feat(common): add ability to track all location changes (#30055)

This feature adds an `onUrlChange` to Angular's `Location` class. This is useful to track all updates coming from anywhere in the framework. Without this method, it's difficult (or impossible) to track updates run through `location.go()` or `location.replaceState()` as the browser doesn't publish events when `history.pushState()` or `.replaceState()` are run.

PR Close #30055
This commit is contained in:
Jason Aden
2019-04-22 15:48:27 -07:00
committed by Ben Lesh
parent 152d99eef0
commit 3a9cf3f2ba
3 changed files with 65 additions and 2 deletions

View File

@ -77,4 +77,37 @@ describe('Location Class', () => {
}));
});
describe('location.onUrlChange()', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [CommonModule],
providers: [
{provide: LocationStrategy, useClass: PathLocationStrategy},
{provide: PlatformLocation, useFactory: () => { return new MockPlatformLocation(); }},
{provide: Location, useClass: Location, deps: [LocationStrategy, PlatformLocation]},
]
});
});
it('should have onUrlChange method', inject([Location], (location: Location) => {
expect(typeof location.onUrlChange).toBe('function');
}));
it('should add registered functions to urlChangeListeners', inject([Location], (location: Location) => {
function changeListener(url: string, state: unknown) {
return undefined;
}
expect((location as any).urlChangeListeners.length).toBe(0);
location.onUrlChange(changeListener);
expect((location as any).urlChangeListeners.length).toBe(1);
expect((location as any).urlChangeListeners[0]).toEqual(changeListener);
}));
});
});