/
location_spec.ts
269 lines (200 loc) Β· 8.49 KB
/
location_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* 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
*/
import {APP_BASE_HREF, CommonModule, Location, LocationStrategy, PathLocationStrategy, PlatformLocation} from '@angular/common';
import {MockLocationStrategy, MockPlatformLocation} from '@angular/common/testing';
import {TestBed} from '@angular/core/testing';
const baseUrl = '/base';
describe('Location Class', () => {
describe('stripTrailingSlash', () => {
it('should strip single character slash', () => {
const input = '/';
expect(Location.stripTrailingSlash(input)).toBe('');
});
it('should normalize strip a trailing slash', () => {
const input = baseUrl + '/';
expect(Location.stripTrailingSlash(input)).toBe(baseUrl);
});
it('should ignore query params when stripping a slash', () => {
const input = baseUrl + '/?param=1';
expect(Location.stripTrailingSlash(input)).toBe(baseUrl + '?param=1');
});
it('should not remove slashes inside query params', () => {
const input = baseUrl + '?test/?=3';
expect(Location.stripTrailingSlash(input)).toBe(input);
});
it('should not remove slashes after a pound sign', () => {
const input = baseUrl + '#test/?=3';
expect(Location.stripTrailingSlash(input)).toBe(input);
});
});
describe('location.getState()', () => {
let location: Location;
beforeEach(() => {
TestBed.configureTestingModule({
teardown: {destroyAfterEach: true},
imports: [CommonModule],
providers: [
{provide: LocationStrategy, useClass: PathLocationStrategy},
{
provide: PlatformLocation,
useFactory: () => {
return new MockPlatformLocation();
}
},
{provide: Location, useClass: Location, deps: [LocationStrategy]},
]
});
location = TestBed.inject(Location);
});
it('should get the state object', () => {
expect(location.getState()).toBe(null);
location.go('/test', '', {foo: 'bar'});
expect(location.getState()).toEqual({foo: 'bar'});
});
it('should work after using back button', () => {
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'});
});
it('should work after using forward button', () => {
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'});
location.forward();
expect(location.getState()).toEqual({url: 'test2'});
});
it('should work after using location.historyGo()', () => {
expect(location.getState()).toBe(null);
location.go('/test1', '', {url: 'test1'});
location.go('/test2', '', {url: 'test2'});
location.go('/test3', '', {url: 'test3'});
expect(location.getState()).toEqual({url: 'test3'});
location.historyGo(-2);
expect(location.getState()).toEqual({url: 'test1'});
location.historyGo(2);
expect(location.getState()).toEqual({url: 'test3'});
location.go('/test3', '', {url: 'test4'});
location.historyGo(0);
expect(location.getState()).toEqual({url: 'test4'});
location.historyGo();
expect(location.getState()).toEqual({url: 'test4'});
// we are testing the behaviour of the `historyGo` method at the moment when the value of
// the relativePosition goes out of bounds.
// The result should be that the locationState does not change.
location.historyGo(100);
expect(location.getState()).toEqual({url: 'test4'});
location.historyGo(-100);
expect(location.getState()).toEqual({url: 'test4'});
location.back();
expect(location.getState()).toEqual({url: 'test3'});
});
});
describe('location.onUrlChange()', () => {
let location: Location;
let locationStrategy: MockLocationStrategy;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [CommonModule],
providers: [
{provide: LocationStrategy, useClass: MockLocationStrategy},
{
provide: PlatformLocation,
useFactory: () => {
return new MockPlatformLocation();
}
},
{provide: Location, useClass: Location, deps: [LocationStrategy]},
]
});
location = TestBed.inject(Location);
locationStrategy = TestBed.inject(LocationStrategy) as MockLocationStrategy;
});
it('should have onUrlChange method', () => {
expect(typeof location.onUrlChange).toBe('function');
});
it('should add registered functions to urlChangeListeners', () => {
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);
});
it('should unregister a URL change listener and unsubscribe from URL changes when the root view is removed',
() => {
const changeListener = jasmine.createSpy('changeListener');
const removeUrlChangeFn = location.onUrlChange(changeListener);
location.go('x');
expect(changeListener).toHaveBeenCalledTimes(1);
removeUrlChangeFn();
expect(changeListener).toHaveBeenCalledTimes(1);
location.onUrlChange((url: string, state: unknown) => {});
TestBed.resetTestingModule();
// Let's ensure that URL change listeners are unregistered when the root view is removed,
// tho the last returned `onUrlChange` function hasn't been invoked.
expect((location as any)._urlChangeListeners.length).toEqual(0);
expect((location as any)._urlChangeSubscription.closed).toEqual(true);
});
it('should only notify listeners once when multiple listeners are registered', () => {
let notificationCount = 0;
function incrementChangeListener(url: string, state: unknown) {
notificationCount += 1;
return undefined;
}
function noopChangeListener(url: string, state: unknown) {
return undefined;
}
location.onUrlChange(incrementChangeListener);
location.onUrlChange(noopChangeListener);
expect(notificationCount).toBe(0);
locationStrategy.simulatePopState('/test');
expect(notificationCount).toBe(1);
});
});
describe('location.normalize(url) should return only route', () => {
const basePath = '/en';
const route = '/go/to/there';
const url = basePath + route;
const getBaseHref = (origin: string) => origin + basePath + '/';
it('in case APP_BASE_HREF starts with http:', () => {
const origin = 'http://example.com';
const baseHref = getBaseHref(origin);
TestBed.configureTestingModule({providers: [{provide: APP_BASE_HREF, useValue: baseHref}]});
const location = TestBed.inject(Location);
expect(location.normalize(url)).toBe(route);
});
it('in case APP_BASE_HREF starts with https:', () => {
const origin = 'https://example.com';
const baseHref = getBaseHref(origin);
TestBed.configureTestingModule({providers: [{provide: APP_BASE_HREF, useValue: baseHref}]});
const location = TestBed.inject(Location);
expect(location.normalize(url)).toBe(route);
});
it('in case APP_BASE_HREF starts with no protocol', () => {
const origin = '//example.com';
const baseHref = getBaseHref(origin);
TestBed.configureTestingModule({providers: [{provide: APP_BASE_HREF, useValue: baseHref}]});
const location = TestBed.inject(Location);
expect(location.normalize(url)).toBe(route);
});
it('in case APP_BASE_HREF starts with no origin', () => {
const origin = '';
const baseHref = getBaseHref(origin);
TestBed.configureTestingModule({providers: [{provide: APP_BASE_HREF, useValue: baseHref}]});
const location = TestBed.inject(Location);
expect(location.normalize(url)).toBe(route);
});
});
});