1 | /**
|
---|
2 | * @license Angular v12.2.13
|
---|
3 | * (c) 2010-2021 Google LLC. https://angular.io/
|
---|
4 | * License: MIT
|
---|
5 | */
|
---|
6 |
|
---|
7 | import { EventEmitter, Injectable, InjectionToken, Inject, Optional } from '@angular/core';
|
---|
8 | import { LocationStrategy } from '@angular/common';
|
---|
9 | import { Subject } from 'rxjs';
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * @license
|
---|
13 | * Copyright Google LLC All Rights Reserved.
|
---|
14 | *
|
---|
15 | * Use of this source code is governed by an MIT-style license that can be
|
---|
16 | * found in the LICENSE file at https://angular.io/license
|
---|
17 | */
|
---|
18 | /**
|
---|
19 | * A spy for {@link Location} that allows tests to fire simulated location events.
|
---|
20 | *
|
---|
21 | * @publicApi
|
---|
22 | */
|
---|
23 | class SpyLocation {
|
---|
24 | constructor() {
|
---|
25 | this.urlChanges = [];
|
---|
26 | this._history = [new LocationState('', '', null)];
|
---|
27 | this._historyIndex = 0;
|
---|
28 | /** @internal */
|
---|
29 | this._subject = new EventEmitter();
|
---|
30 | /** @internal */
|
---|
31 | this._baseHref = '';
|
---|
32 | /** @internal */
|
---|
33 | this._platformStrategy = null;
|
---|
34 | /** @internal */
|
---|
35 | this._platformLocation = null;
|
---|
36 | /** @internal */
|
---|
37 | this._urlChangeListeners = [];
|
---|
38 | }
|
---|
39 | setInitialPath(url) {
|
---|
40 | this._history[this._historyIndex].path = url;
|
---|
41 | }
|
---|
42 | setBaseHref(url) {
|
---|
43 | this._baseHref = url;
|
---|
44 | }
|
---|
45 | path() {
|
---|
46 | return this._history[this._historyIndex].path;
|
---|
47 | }
|
---|
48 | getState() {
|
---|
49 | return this._history[this._historyIndex].state;
|
---|
50 | }
|
---|
51 | isCurrentPathEqualTo(path, query = '') {
|
---|
52 | const givenPath = path.endsWith('/') ? path.substring(0, path.length - 1) : path;
|
---|
53 | const currPath = this.path().endsWith('/') ? this.path().substring(0, this.path().length - 1) : this.path();
|
---|
54 | return currPath == givenPath + (query.length > 0 ? ('?' + query) : '');
|
---|
55 | }
|
---|
56 | simulateUrlPop(pathname) {
|
---|
57 | this._subject.emit({ 'url': pathname, 'pop': true, 'type': 'popstate' });
|
---|
58 | }
|
---|
59 | simulateHashChange(pathname) {
|
---|
60 | // Because we don't prevent the native event, the browser will independently update the path
|
---|
61 | this.setInitialPath(pathname);
|
---|
62 | this.urlChanges.push('hash: ' + pathname);
|
---|
63 | this._subject.emit({ 'url': pathname, 'pop': true, 'type': 'hashchange' });
|
---|
64 | }
|
---|
65 | prepareExternalUrl(url) {
|
---|
66 | if (url.length > 0 && !url.startsWith('/')) {
|
---|
67 | url = '/' + url;
|
---|
68 | }
|
---|
69 | return this._baseHref + url;
|
---|
70 | }
|
---|
71 | go(path, query = '', state = null) {
|
---|
72 | path = this.prepareExternalUrl(path);
|
---|
73 | if (this._historyIndex > 0) {
|
---|
74 | this._history.splice(this._historyIndex + 1);
|
---|
75 | }
|
---|
76 | this._history.push(new LocationState(path, query, state));
|
---|
77 | this._historyIndex = this._history.length - 1;
|
---|
78 | const locationState = this._history[this._historyIndex - 1];
|
---|
79 | if (locationState.path == path && locationState.query == query) {
|
---|
80 | return;
|
---|
81 | }
|
---|
82 | const url = path + (query.length > 0 ? ('?' + query) : '');
|
---|
83 | this.urlChanges.push(url);
|
---|
84 | this._subject.emit({ 'url': url, 'pop': false });
|
---|
85 | }
|
---|
86 | replaceState(path, query = '', state = null) {
|
---|
87 | path = this.prepareExternalUrl(path);
|
---|
88 | const history = this._history[this._historyIndex];
|
---|
89 | if (history.path == path && history.query == query) {
|
---|
90 | return;
|
---|
91 | }
|
---|
92 | history.path = path;
|
---|
93 | history.query = query;
|
---|
94 | history.state = state;
|
---|
95 | const url = path + (query.length > 0 ? ('?' + query) : '');
|
---|
96 | this.urlChanges.push('replace: ' + url);
|
---|
97 | }
|
---|
98 | forward() {
|
---|
99 | if (this._historyIndex < (this._history.length - 1)) {
|
---|
100 | this._historyIndex++;
|
---|
101 | this._subject.emit({ 'url': this.path(), 'state': this.getState(), 'pop': true });
|
---|
102 | }
|
---|
103 | }
|
---|
104 | back() {
|
---|
105 | if (this._historyIndex > 0) {
|
---|
106 | this._historyIndex--;
|
---|
107 | this._subject.emit({ 'url': this.path(), 'state': this.getState(), 'pop': true });
|
---|
108 | }
|
---|
109 | }
|
---|
110 | historyGo(relativePosition = 0) {
|
---|
111 | const nextPageIndex = this._historyIndex + relativePosition;
|
---|
112 | if (nextPageIndex >= 0 && nextPageIndex < this._history.length) {
|
---|
113 | this._historyIndex = nextPageIndex;
|
---|
114 | this._subject.emit({ 'url': this.path(), 'state': this.getState(), 'pop': true, 'type': 'popstate' });
|
---|
115 | }
|
---|
116 | }
|
---|
117 | onUrlChange(fn) {
|
---|
118 | this._urlChangeListeners.push(fn);
|
---|
119 | if (!this._urlChangeSubscription) {
|
---|
120 | this._urlChangeSubscription = this.subscribe(v => {
|
---|
121 | this._notifyUrlChangeListeners(v.url, v.state);
|
---|
122 | });
|
---|
123 | }
|
---|
124 | }
|
---|
125 | /** @internal */
|
---|
126 | _notifyUrlChangeListeners(url = '', state) {
|
---|
127 | this._urlChangeListeners.forEach(fn => fn(url, state));
|
---|
128 | }
|
---|
129 | subscribe(onNext, onThrow, onReturn) {
|
---|
130 | return this._subject.subscribe({ next: onNext, error: onThrow, complete: onReturn });
|
---|
131 | }
|
---|
132 | normalize(url) {
|
---|
133 | return null;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | SpyLocation.decorators = [
|
---|
137 | { type: Injectable }
|
---|
138 | ];
|
---|
139 | class LocationState {
|
---|
140 | constructor(path, query, state) {
|
---|
141 | this.path = path;
|
---|
142 | this.query = query;
|
---|
143 | this.state = state;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * @license
|
---|
149 | * Copyright Google LLC All Rights Reserved.
|
---|
150 | *
|
---|
151 | * Use of this source code is governed by an MIT-style license that can be
|
---|
152 | * found in the LICENSE file at https://angular.io/license
|
---|
153 | */
|
---|
154 | /**
|
---|
155 | * A mock implementation of {@link LocationStrategy} that allows tests to fire simulated
|
---|
156 | * location events.
|
---|
157 | *
|
---|
158 | * @publicApi
|
---|
159 | */
|
---|
160 | class MockLocationStrategy extends LocationStrategy {
|
---|
161 | constructor() {
|
---|
162 | super();
|
---|
163 | this.internalBaseHref = '/';
|
---|
164 | this.internalPath = '/';
|
---|
165 | this.internalTitle = '';
|
---|
166 | this.urlChanges = [];
|
---|
167 | /** @internal */
|
---|
168 | this._subject = new EventEmitter();
|
---|
169 | this.stateChanges = [];
|
---|
170 | }
|
---|
171 | simulatePopState(url) {
|
---|
172 | this.internalPath = url;
|
---|
173 | this._subject.emit(new _MockPopStateEvent(this.path()));
|
---|
174 | }
|
---|
175 | path(includeHash = false) {
|
---|
176 | return this.internalPath;
|
---|
177 | }
|
---|
178 | prepareExternalUrl(internal) {
|
---|
179 | if (internal.startsWith('/') && this.internalBaseHref.endsWith('/')) {
|
---|
180 | return this.internalBaseHref + internal.substring(1);
|
---|
181 | }
|
---|
182 | return this.internalBaseHref + internal;
|
---|
183 | }
|
---|
184 | pushState(ctx, title, path, query) {
|
---|
185 | // Add state change to changes array
|
---|
186 | this.stateChanges.push(ctx);
|
---|
187 | this.internalTitle = title;
|
---|
188 | const url = path + (query.length > 0 ? ('?' + query) : '');
|
---|
189 | this.internalPath = url;
|
---|
190 | const externalUrl = this.prepareExternalUrl(url);
|
---|
191 | this.urlChanges.push(externalUrl);
|
---|
192 | }
|
---|
193 | replaceState(ctx, title, path, query) {
|
---|
194 | // Reset the last index of stateChanges to the ctx (state) object
|
---|
195 | this.stateChanges[(this.stateChanges.length || 1) - 1] = ctx;
|
---|
196 | this.internalTitle = title;
|
---|
197 | const url = path + (query.length > 0 ? ('?' + query) : '');
|
---|
198 | this.internalPath = url;
|
---|
199 | const externalUrl = this.prepareExternalUrl(url);
|
---|
200 | this.urlChanges.push('replace: ' + externalUrl);
|
---|
201 | }
|
---|
202 | onPopState(fn) {
|
---|
203 | this._subject.subscribe({ next: fn });
|
---|
204 | }
|
---|
205 | getBaseHref() {
|
---|
206 | return this.internalBaseHref;
|
---|
207 | }
|
---|
208 | back() {
|
---|
209 | if (this.urlChanges.length > 0) {
|
---|
210 | this.urlChanges.pop();
|
---|
211 | this.stateChanges.pop();
|
---|
212 | const nextUrl = this.urlChanges.length > 0 ? this.urlChanges[this.urlChanges.length - 1] : '';
|
---|
213 | this.simulatePopState(nextUrl);
|
---|
214 | }
|
---|
215 | }
|
---|
216 | forward() {
|
---|
217 | throw 'not implemented';
|
---|
218 | }
|
---|
219 | getState() {
|
---|
220 | return this.stateChanges[(this.stateChanges.length || 1) - 1];
|
---|
221 | }
|
---|
222 | }
|
---|
223 | MockLocationStrategy.decorators = [
|
---|
224 | { type: Injectable }
|
---|
225 | ];
|
---|
226 | MockLocationStrategy.ctorParameters = () => [];
|
---|
227 | class _MockPopStateEvent {
|
---|
228 | constructor(newUrl) {
|
---|
229 | this.newUrl = newUrl;
|
---|
230 | this.pop = true;
|
---|
231 | this.type = 'popstate';
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * @license
|
---|
237 | * Copyright Google LLC All Rights Reserved.
|
---|
238 | *
|
---|
239 | * Use of this source code is governed by an MIT-style license that can be
|
---|
240 | * found in the LICENSE file at https://angular.io/license
|
---|
241 | */
|
---|
242 | /**
|
---|
243 | * Parser from https://tools.ietf.org/html/rfc3986#appendix-B
|
---|
244 | * ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
|
---|
245 | * 12 3 4 5 6 7 8 9
|
---|
246 | *
|
---|
247 | * Example: http://www.ics.uci.edu/pub/ietf/uri/#Related
|
---|
248 | *
|
---|
249 | * Results in:
|
---|
250 | *
|
---|
251 | * $1 = http:
|
---|
252 | * $2 = http
|
---|
253 | * $3 = //www.ics.uci.edu
|
---|
254 | * $4 = www.ics.uci.edu
|
---|
255 | * $5 = /pub/ietf/uri/
|
---|
256 | * $6 = <undefined>
|
---|
257 | * $7 = <undefined>
|
---|
258 | * $8 = #Related
|
---|
259 | * $9 = Related
|
---|
260 | */
|
---|
261 | const urlParse = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;
|
---|
262 | function parseUrl(urlStr, baseHref) {
|
---|
263 | const verifyProtocol = /^((http[s]?|ftp):\/\/)/;
|
---|
264 | let serverBase;
|
---|
265 | // URL class requires full URL. If the URL string doesn't start with protocol, we need to add
|
---|
266 | // an arbitrary base URL which can be removed afterward.
|
---|
267 | if (!verifyProtocol.test(urlStr)) {
|
---|
268 | serverBase = 'http://empty.com/';
|
---|
269 | }
|
---|
270 | let parsedUrl;
|
---|
271 | try {
|
---|
272 | parsedUrl = new URL(urlStr, serverBase);
|
---|
273 | }
|
---|
274 | catch (e) {
|
---|
275 | const result = urlParse.exec(serverBase || '' + urlStr);
|
---|
276 | if (!result) {
|
---|
277 | throw new Error(`Invalid URL: ${urlStr} with base: ${baseHref}`);
|
---|
278 | }
|
---|
279 | const hostSplit = result[4].split(':');
|
---|
280 | parsedUrl = {
|
---|
281 | protocol: result[1],
|
---|
282 | hostname: hostSplit[0],
|
---|
283 | port: hostSplit[1] || '',
|
---|
284 | pathname: result[5],
|
---|
285 | search: result[6],
|
---|
286 | hash: result[8],
|
---|
287 | };
|
---|
288 | }
|
---|
289 | if (parsedUrl.pathname && parsedUrl.pathname.indexOf(baseHref) === 0) {
|
---|
290 | parsedUrl.pathname = parsedUrl.pathname.substring(baseHref.length);
|
---|
291 | }
|
---|
292 | return {
|
---|
293 | hostname: !serverBase && parsedUrl.hostname || '',
|
---|
294 | protocol: !serverBase && parsedUrl.protocol || '',
|
---|
295 | port: !serverBase && parsedUrl.port || '',
|
---|
296 | pathname: parsedUrl.pathname || '/',
|
---|
297 | search: parsedUrl.search || '',
|
---|
298 | hash: parsedUrl.hash || '',
|
---|
299 | };
|
---|
300 | }
|
---|
301 | /**
|
---|
302 | * Provider for mock platform location config
|
---|
303 | *
|
---|
304 | * @publicApi
|
---|
305 | */
|
---|
306 | const MOCK_PLATFORM_LOCATION_CONFIG = new InjectionToken('MOCK_PLATFORM_LOCATION_CONFIG');
|
---|
307 | /**
|
---|
308 | * Mock implementation of URL state.
|
---|
309 | *
|
---|
310 | * @publicApi
|
---|
311 | */
|
---|
312 | class MockPlatformLocation {
|
---|
313 | constructor(config) {
|
---|
314 | this.baseHref = '';
|
---|
315 | this.hashUpdate = new Subject();
|
---|
316 | this.urlChangeIndex = 0;
|
---|
317 | this.urlChanges = [{ hostname: '', protocol: '', port: '', pathname: '/', search: '', hash: '', state: null }];
|
---|
318 | if (config) {
|
---|
319 | this.baseHref = config.appBaseHref || '';
|
---|
320 | const parsedChanges = this.parseChanges(null, config.startUrl || 'http://<empty>/', this.baseHref);
|
---|
321 | this.urlChanges[0] = Object.assign({}, parsedChanges);
|
---|
322 | }
|
---|
323 | }
|
---|
324 | get hostname() {
|
---|
325 | return this.urlChanges[this.urlChangeIndex].hostname;
|
---|
326 | }
|
---|
327 | get protocol() {
|
---|
328 | return this.urlChanges[this.urlChangeIndex].protocol;
|
---|
329 | }
|
---|
330 | get port() {
|
---|
331 | return this.urlChanges[this.urlChangeIndex].port;
|
---|
332 | }
|
---|
333 | get pathname() {
|
---|
334 | return this.urlChanges[this.urlChangeIndex].pathname;
|
---|
335 | }
|
---|
336 | get search() {
|
---|
337 | return this.urlChanges[this.urlChangeIndex].search;
|
---|
338 | }
|
---|
339 | get hash() {
|
---|
340 | return this.urlChanges[this.urlChangeIndex].hash;
|
---|
341 | }
|
---|
342 | get state() {
|
---|
343 | return this.urlChanges[this.urlChangeIndex].state;
|
---|
344 | }
|
---|
345 | getBaseHrefFromDOM() {
|
---|
346 | return this.baseHref;
|
---|
347 | }
|
---|
348 | onPopState(fn) {
|
---|
349 | // No-op: a state stack is not implemented, so
|
---|
350 | // no events will ever come.
|
---|
351 | return () => { };
|
---|
352 | }
|
---|
353 | onHashChange(fn) {
|
---|
354 | const subscription = this.hashUpdate.subscribe(fn);
|
---|
355 | return () => subscription.unsubscribe();
|
---|
356 | }
|
---|
357 | get href() {
|
---|
358 | let url = `${this.protocol}//${this.hostname}${this.port ? ':' + this.port : ''}`;
|
---|
359 | url += `${this.pathname === '/' ? '' : this.pathname}${this.search}${this.hash}`;
|
---|
360 | return url;
|
---|
361 | }
|
---|
362 | get url() {
|
---|
363 | return `${this.pathname}${this.search}${this.hash}`;
|
---|
364 | }
|
---|
365 | parseChanges(state, url, baseHref = '') {
|
---|
366 | // When the `history.state` value is stored, it is always copied.
|
---|
367 | state = JSON.parse(JSON.stringify(state));
|
---|
368 | return Object.assign(Object.assign({}, parseUrl(url, baseHref)), { state });
|
---|
369 | }
|
---|
370 | replaceState(state, title, newUrl) {
|
---|
371 | const { pathname, search, state: parsedState, hash } = this.parseChanges(state, newUrl);
|
---|
372 | this.urlChanges[this.urlChangeIndex] = Object.assign(Object.assign({}, this.urlChanges[this.urlChangeIndex]), { pathname, search, hash, state: parsedState });
|
---|
373 | }
|
---|
374 | pushState(state, title, newUrl) {
|
---|
375 | const { pathname, search, state: parsedState, hash } = this.parseChanges(state, newUrl);
|
---|
376 | if (this.urlChangeIndex > 0) {
|
---|
377 | this.urlChanges.splice(this.urlChangeIndex + 1);
|
---|
378 | }
|
---|
379 | this.urlChanges.push(Object.assign(Object.assign({}, this.urlChanges[this.urlChangeIndex]), { pathname, search, hash, state: parsedState }));
|
---|
380 | this.urlChangeIndex = this.urlChanges.length - 1;
|
---|
381 | }
|
---|
382 | forward() {
|
---|
383 | const oldUrl = this.url;
|
---|
384 | const oldHash = this.hash;
|
---|
385 | if (this.urlChangeIndex < this.urlChanges.length) {
|
---|
386 | this.urlChangeIndex++;
|
---|
387 | }
|
---|
388 | this.scheduleHashUpdate(oldHash, oldUrl);
|
---|
389 | }
|
---|
390 | back() {
|
---|
391 | const oldUrl = this.url;
|
---|
392 | const oldHash = this.hash;
|
---|
393 | if (this.urlChangeIndex > 0) {
|
---|
394 | this.urlChangeIndex--;
|
---|
395 | }
|
---|
396 | this.scheduleHashUpdate(oldHash, oldUrl);
|
---|
397 | }
|
---|
398 | historyGo(relativePosition = 0) {
|
---|
399 | const oldUrl = this.url;
|
---|
400 | const oldHash = this.hash;
|
---|
401 | const nextPageIndex = this.urlChangeIndex + relativePosition;
|
---|
402 | if (nextPageIndex >= 0 && nextPageIndex < this.urlChanges.length) {
|
---|
403 | this.urlChangeIndex = nextPageIndex;
|
---|
404 | }
|
---|
405 | this.scheduleHashUpdate(oldHash, oldUrl);
|
---|
406 | }
|
---|
407 | getState() {
|
---|
408 | return this.state;
|
---|
409 | }
|
---|
410 | scheduleHashUpdate(oldHash, oldUrl) {
|
---|
411 | if (oldHash !== this.hash) {
|
---|
412 | scheduleMicroTask(() => this.hashUpdate.next({ type: 'hashchange', state: null, oldUrl, newUrl: this.url }));
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|
416 | MockPlatformLocation.decorators = [
|
---|
417 | { type: Injectable }
|
---|
418 | ];
|
---|
419 | MockPlatformLocation.ctorParameters = () => [
|
---|
420 | { type: undefined, decorators: [{ type: Inject, args: [MOCK_PLATFORM_LOCATION_CONFIG,] }, { type: Optional }] }
|
---|
421 | ];
|
---|
422 | function scheduleMicroTask(cb) {
|
---|
423 | Promise.resolve(null).then(cb);
|
---|
424 | }
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * @license
|
---|
428 | * Copyright Google LLC All Rights Reserved.
|
---|
429 | *
|
---|
430 | * Use of this source code is governed by an MIT-style license that can be
|
---|
431 | * found in the LICENSE file at https://angular.io/license
|
---|
432 | */
|
---|
433 |
|
---|
434 | /**
|
---|
435 | * @license
|
---|
436 | * Copyright Google LLC All Rights Reserved.
|
---|
437 | *
|
---|
438 | * Use of this source code is governed by an MIT-style license that can be
|
---|
439 | * found in the LICENSE file at https://angular.io/license
|
---|
440 | */
|
---|
441 | // This file only reexports content of the `src` folder. Keep it that way.
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * @license
|
---|
445 | * Copyright Google LLC All Rights Reserved.
|
---|
446 | *
|
---|
447 | * Use of this source code is governed by an MIT-style license that can be
|
---|
448 | * found in the LICENSE file at https://angular.io/license
|
---|
449 | */
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Generated bundle index. Do not edit.
|
---|
453 | */
|
---|
454 |
|
---|
455 | export { MOCK_PLATFORM_LOCATION_CONFIG, MockLocationStrategy, MockPlatformLocation, SpyLocation };
|
---|
456 | //# sourceMappingURL=testing.js.map
|
---|