Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/common/src/location/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export class Location {
_platformLocation: PlatformLocation;
/** @internal */
_urlChangeListeners: ((url: string, state: unknown) => void)[] = [];
private _urlChangeSubscription?: SubscriptionLike;
/** @internal */
_urlChangeSubscription?: SubscriptionLike;

constructor(platformStrategy: LocationStrategy, platformLocation: PlatformLocation) {
this._platformStrategy = platformStrategy;
Expand Down
65 changes: 36 additions & 29 deletions packages/common/test/location/location_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {CommonModule, Location, LocationStrategy, PathLocationStrategy, PlatformLocation} from '@angular/common';
import {MockLocationStrategy, MockPlatformLocation} from '@angular/common/testing';
import {inject, TestBed} from '@angular/core/testing';
import {TestBed} from '@angular/core/testing';

const baseUrl = '/base';

Expand Down Expand Up @@ -41,6 +41,8 @@ describe('Location Class', () => {
});

describe('location.getState()', () => {
let location: Location;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [CommonModule],
Expand All @@ -55,31 +57,36 @@ describe('Location Class', () => {
{provide: Location, useClass: Location, deps: [LocationStrategy, PlatformLocation]},
]
});

location = TestBed.inject(Location);
});

it('should get the state object', inject([Location], (location: Location) => {
expect(location.getState()).toBe(null);
it('should get the state object', () => {
expect(location.getState()).toBe(null);

location.go('/test', '', {foo: 'bar'});
location.go('/test', '', {foo: 'bar'});

expect(location.getState()).toEqual({foo: 'bar'});
}));
expect(location.getState()).toEqual({foo: 'bar'});
});

it('should work after using back button', inject([Location], (location: Location) => {
expect(location.getState()).toBe(null);
it('should work after using back button', () => {
expect(location.getState()).toBe(null);

location.go('/test1', '', {url: 'test1'});
location.go('/test2', '', {url: 'test2'});
location.go('/test1', '', {url: 'test1'});
location.go('/test2', '', {url: 'test2'});

expect(location.getState()).toEqual({url: 'test2'});
expect(location.getState()).toEqual({url: 'test2'});

location.back();
location.back();

expect(location.getState()).toEqual({url: 'test1'});
}));
expect(location.getState()).toEqual({url: 'test1'});
});
});

describe('location.onUrlChange()', () => {
let location: Location;
let locationStrategy: MockLocationStrategy;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [CommonModule],
Expand All @@ -94,29 +101,29 @@ describe('Location Class', () => {
{provide: Location, useClass: Location, deps: [LocationStrategy, PlatformLocation]},
]
});

location = TestBed.inject(Location);
locationStrategy = TestBed.inject(LocationStrategy) as MockLocationStrategy;
});

it('should have onUrlChange method', inject([Location], (location: Location) => {
expect(typeof location.onUrlChange).toBe('function');
}));
it('should have onUrlChange method', () => {
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;
}
it('should add registered functions to urlChangeListeners', () => {
function changeListener(url: string, state: unknown) {
return undefined;
}

expect((location as any)._urlChangeListeners.length).toBe(0);
expect((location as any)._urlChangeListeners.length).toBe(0);

location.onUrlChange(changeListener);
location.onUrlChange(changeListener);

expect((location as any)._urlChangeListeners.length).toBe(1);
expect((location as any)._urlChangeListeners[0]).toEqual(changeListener);
}));
expect((location as any)._urlChangeListeners.length).toBe(1);
expect((location as any)._urlChangeListeners[0]).toEqual(changeListener);
});

it('should only notify listeners once when multiple listeners are registered', () => {
const location = TestBed.inject(Location);
const locationStrategy = TestBed.inject(LocationStrategy) as MockLocationStrategy;
let notificationCount = 0;

function incrementChangeListener(url: string, state: unknown) {
Expand Down
11 changes: 8 additions & 3 deletions packages/common/testing/src/location_mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export class SpyLocation implements Location {
_platformLocation: PlatformLocation = null!;
/** @internal */
_urlChangeListeners: ((url: string, state: unknown) => void)[] = [];
/** @internal */
_urlChangeSubscription?: SubscriptionLike;

setInitialPath(url: string) {
this._history[this._historyIndex].path = url;
Expand Down Expand Up @@ -123,9 +125,12 @@ export class SpyLocation implements Location {
}
onUrlChange(fn: (url: string, state: unknown) => void) {
this._urlChangeListeners.push(fn);
this.subscribe(v => {
this._notifyUrlChangeListeners(v.url, v.state);
});

if (!this._urlChangeSubscription) {
this._urlChangeSubscription = this.subscribe(v => {
this._notifyUrlChangeListeners(v.url, v.state);
});
}
}

/** @internal */
Expand Down