2017-05-26 14:02:31 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2017-05-26 14:02:31 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2019-04-23 10:16:08 -04:00
|
|
|
import {CommonModule, Location, LocationStrategy, PathLocationStrategy, PlatformLocation} from '@angular/common';
|
2019-02-06 17:42:57 -05:00
|
|
|
import {MockPlatformLocation} from '@angular/common/testing';
|
2020-04-13 19:40:21 -04:00
|
|
|
import {inject, TestBed} from '@angular/core/testing';
|
2017-05-26 14:02:31 -04:00
|
|
|
|
2018-11-13 05:51:02 -05:00
|
|
|
const baseUrl = '/base';
|
2017-05-26 14:02:31 -04:00
|
|
|
|
2018-11-13 05:51:02 -05:00
|
|
|
describe('Location Class', () => {
|
|
|
|
describe('stripTrailingSlash', () => {
|
|
|
|
it('should strip single character slash', () => {
|
|
|
|
const input = '/';
|
|
|
|
expect(Location.stripTrailingSlash(input)).toBe('');
|
|
|
|
});
|
2017-05-26 14:02:31 -04:00
|
|
|
|
2018-11-13 05:51:02 -05:00
|
|
|
it('should normalize strip a trailing slash', () => {
|
|
|
|
const input = baseUrl + '/';
|
|
|
|
expect(Location.stripTrailingSlash(input)).toBe(baseUrl);
|
|
|
|
});
|
2017-05-26 14:02:31 -04:00
|
|
|
|
2018-11-13 05:51:02 -05:00
|
|
|
it('should ignore query params when stripping a slash', () => {
|
|
|
|
const input = baseUrl + '/?param=1';
|
|
|
|
expect(Location.stripTrailingSlash(input)).toBe(baseUrl + '?param=1');
|
|
|
|
});
|
2017-05-26 14:02:31 -04:00
|
|
|
|
2018-11-13 05:51:02 -05:00
|
|
|
it('should not remove slashes inside query params', () => {
|
|
|
|
const input = baseUrl + '?test/?=3';
|
|
|
|
expect(Location.stripTrailingSlash(input)).toBe(input);
|
|
|
|
});
|
2017-05-26 14:02:31 -04:00
|
|
|
|
2018-11-13 05:51:02 -05:00
|
|
|
it('should not remove slashes after a pound sign', () => {
|
|
|
|
const input = baseUrl + '#test/?=3';
|
|
|
|
expect(Location.stripTrailingSlash(input)).toBe(input);
|
2017-05-26 14:02:31 -04:00
|
|
|
});
|
|
|
|
});
|
2019-02-06 17:42:57 -05:00
|
|
|
|
|
|
|
describe('location.getState()', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
imports: [CommonModule],
|
|
|
|
providers: [
|
|
|
|
{provide: LocationStrategy, useClass: PathLocationStrategy},
|
2020-04-13 19:40:21 -04:00
|
|
|
{
|
|
|
|
provide: PlatformLocation,
|
|
|
|
useFactory: () => {
|
|
|
|
return new MockPlatformLocation();
|
|
|
|
}
|
|
|
|
},
|
2019-02-06 17:42:57 -05:00
|
|
|
{provide: Location, useClass: Location, deps: [LocationStrategy, PlatformLocation]},
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get the state object', inject([Location], (location: Location) => {
|
|
|
|
expect(location.getState()).toBe(null);
|
|
|
|
|
|
|
|
location.go('/test', '', {foo: 'bar'});
|
|
|
|
|
|
|
|
expect(location.getState()).toEqual({foo: 'bar'});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should work after using back button', inject([Location], (location: Location) => {
|
|
|
|
expect(location.getState()).toBe(null);
|
|
|
|
|
|
|
|
location.go('/test1', '', {url: 'test1'});
|
|
|
|
location.go('/test2', '', {url: 'test2'});
|
|
|
|
|
|
|
|
expect(location.getState()).toEqual({url: 'test2'});
|
|
|
|
|
|
|
|
location.back();
|
|
|
|
|
|
|
|
expect(location.getState()).toEqual({url: 'test1'});
|
|
|
|
}));
|
|
|
|
});
|
2019-04-22 18:48:27 -04:00
|
|
|
|
|
|
|
describe('location.onUrlChange()', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
imports: [CommonModule],
|
|
|
|
providers: [
|
|
|
|
{provide: LocationStrategy, useClass: PathLocationStrategy},
|
2020-04-13 19:40:21 -04:00
|
|
|
{
|
|
|
|
provide: PlatformLocation,
|
|
|
|
useFactory: () => {
|
|
|
|
return new MockPlatformLocation();
|
|
|
|
}
|
|
|
|
},
|
2019-04-22 18:48:27 -04:00
|
|
|
{provide: Location, useClass: Location, deps: [LocationStrategy, PlatformLocation]},
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have onUrlChange method', inject([Location], (location: Location) => {
|
2019-04-23 10:16:08 -04:00
|
|
|
expect(typeof location.onUrlChange).toBe('function');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should add registered functions to urlChangeListeners',
|
|
|
|
inject([Location], (location: Location) => {
|
2020-04-13 19:40:21 -04:00
|
|
|
function changeListener(url: string, state: unknown) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-04-22 18:48:27 -04:00
|
|
|
|
2019-04-23 10:16:08 -04:00
|
|
|
expect((location as any)._urlChangeListeners.length).toBe(0);
|
2019-04-22 18:48:27 -04:00
|
|
|
|
2019-04-23 10:16:08 -04:00
|
|
|
location.onUrlChange(changeListener);
|
2019-04-22 18:48:27 -04:00
|
|
|
|
2019-04-23 10:16:08 -04:00
|
|
|
expect((location as any)._urlChangeListeners.length).toBe(1);
|
|
|
|
expect((location as any)._urlChangeListeners[0]).toEqual(changeListener);
|
|
|
|
}));
|
2019-04-22 18:48:27 -04:00
|
|
|
});
|
2020-05-19 15:08:49 -04:00
|
|
|
});
|