1 | /**
|
---|
2 | * @license
|
---|
3 | * Copyright Google LLC All Rights Reserved.
|
---|
4 | *
|
---|
5 | * Use of this source code is governed by an MIT-style license that can be
|
---|
6 | * found in the LICENSE file at https://angular.io/license
|
---|
7 | */
|
---|
8 | import { LocationStrategy } from '@angular/common';
|
---|
9 | import { EventEmitter, Injectable } from '@angular/core';
|
---|
10 | /**
|
---|
11 | * A mock implementation of {@link LocationStrategy} that allows tests to fire simulated
|
---|
12 | * location events.
|
---|
13 | *
|
---|
14 | * @publicApi
|
---|
15 | */
|
---|
16 | export class MockLocationStrategy extends LocationStrategy {
|
---|
17 | constructor() {
|
---|
18 | super();
|
---|
19 | this.internalBaseHref = '/';
|
---|
20 | this.internalPath = '/';
|
---|
21 | this.internalTitle = '';
|
---|
22 | this.urlChanges = [];
|
---|
23 | /** @internal */
|
---|
24 | this._subject = new EventEmitter();
|
---|
25 | this.stateChanges = [];
|
---|
26 | }
|
---|
27 | simulatePopState(url) {
|
---|
28 | this.internalPath = url;
|
---|
29 | this._subject.emit(new _MockPopStateEvent(this.path()));
|
---|
30 | }
|
---|
31 | path(includeHash = false) {
|
---|
32 | return this.internalPath;
|
---|
33 | }
|
---|
34 | prepareExternalUrl(internal) {
|
---|
35 | if (internal.startsWith('/') && this.internalBaseHref.endsWith('/')) {
|
---|
36 | return this.internalBaseHref + internal.substring(1);
|
---|
37 | }
|
---|
38 | return this.internalBaseHref + internal;
|
---|
39 | }
|
---|
40 | pushState(ctx, title, path, query) {
|
---|
41 | // Add state change to changes array
|
---|
42 | this.stateChanges.push(ctx);
|
---|
43 | this.internalTitle = title;
|
---|
44 | const url = path + (query.length > 0 ? ('?' + query) : '');
|
---|
45 | this.internalPath = url;
|
---|
46 | const externalUrl = this.prepareExternalUrl(url);
|
---|
47 | this.urlChanges.push(externalUrl);
|
---|
48 | }
|
---|
49 | replaceState(ctx, title, path, query) {
|
---|
50 | // Reset the last index of stateChanges to the ctx (state) object
|
---|
51 | this.stateChanges[(this.stateChanges.length || 1) - 1] = ctx;
|
---|
52 | this.internalTitle = title;
|
---|
53 | const url = path + (query.length > 0 ? ('?' + query) : '');
|
---|
54 | this.internalPath = url;
|
---|
55 | const externalUrl = this.prepareExternalUrl(url);
|
---|
56 | this.urlChanges.push('replace: ' + externalUrl);
|
---|
57 | }
|
---|
58 | onPopState(fn) {
|
---|
59 | this._subject.subscribe({ next: fn });
|
---|
60 | }
|
---|
61 | getBaseHref() {
|
---|
62 | return this.internalBaseHref;
|
---|
63 | }
|
---|
64 | back() {
|
---|
65 | if (this.urlChanges.length > 0) {
|
---|
66 | this.urlChanges.pop();
|
---|
67 | this.stateChanges.pop();
|
---|
68 | const nextUrl = this.urlChanges.length > 0 ? this.urlChanges[this.urlChanges.length - 1] : '';
|
---|
69 | this.simulatePopState(nextUrl);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | forward() {
|
---|
73 | throw 'not implemented';
|
---|
74 | }
|
---|
75 | getState() {
|
---|
76 | return this.stateChanges[(this.stateChanges.length || 1) - 1];
|
---|
77 | }
|
---|
78 | }
|
---|
79 | MockLocationStrategy.decorators = [
|
---|
80 | { type: Injectable }
|
---|
81 | ];
|
---|
82 | MockLocationStrategy.ctorParameters = () => [];
|
---|
83 | class _MockPopStateEvent {
|
---|
84 | constructor(newUrl) {
|
---|
85 | this.newUrl = newUrl;
|
---|
86 | this.pop = true;
|
---|
87 | this.type = 'popstate';
|
---|
88 | }
|
---|
89 | }
|
---|
90 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibW9ja19sb2NhdGlvbl9zdHJhdGVneS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uL3BhY2thZ2VzL2NvbW1vbi90ZXN0aW5nL3NyYy9tb2NrX2xvY2F0aW9uX3N0cmF0ZWd5LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRztBQU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