1
|
/**
|
2
|
* @license Angular v4.4.6
|
3
|
* (c) 2010-2017 Google, Inc. https://angular.io/
|
4
|
* License: MIT
|
5
|
*/
|
6
|
import { EventEmitter, Injectable } from '@angular/core';
|
7
|
import { LocationStrategy } from '@angular/common';
|
8
|
|
9
|
/**
|
10
|
* @license
|
11
|
* Copyright Google Inc. All Rights Reserved.
|
12
|
*
|
13
|
* Use of this source code is governed by an MIT-style license that can be
|
14
|
* found in the LICENSE file at https://angular.io/license
|
15
|
*/
|
16
|
/**
|
17
|
* A spy for {@link Location} that allows tests to fire simulated location events.
|
18
|
*
|
19
|
* @experimental
|
20
|
*/
|
21
|
class SpyLocation {
|
22
|
constructor() {
|
23
|
this.urlChanges = [];
|
24
|
this._history = [new LocationState('', '')];
|
25
|
this._historyIndex = 0;
|
26
|
/** @internal */
|
27
|
this._subject = new EventEmitter();
|
28
|
/** @internal */
|
29
|
this._baseHref = '';
|
30
|
/** @internal */
|
31
|
this._platformStrategy = null;
|
32
|
}
|
33
|
setInitialPath(url) { this._history[this._historyIndex].path = url; }
|
34
|
setBaseHref(url) { this._baseHref = url; }
|
35
|
path() { return this._history[this._historyIndex].path; }
|
36
|
isCurrentPathEqualTo(path, query = '') {
|
37
|
const givenPath = path.endsWith('/') ? path.substring(0, path.length - 1) : path;
|
38
|
const currPath = this.path().endsWith('/') ? this.path().substring(0, this.path().length - 1) : this.path();
|
39
|
return currPath == givenPath + (query.length > 0 ? ('?' + query) : '');
|
40
|
}
|
41
|
simulateUrlPop(pathname) { this._subject.emit({ 'url': pathname, 'pop': true }); }
|
42
|
simulateHashChange(pathname) {
|
43
|
// Because we don't prevent the native event, the browser will independently update the path
|
44
|
this.setInitialPath(pathname);
|
45
|
this.urlChanges.push('hash: ' + pathname);
|
46
|
this._subject.emit({ 'url': pathname, 'pop': true, 'type': 'hashchange' });
|
47
|
}
|
48
|
prepareExternalUrl(url) {
|
49
|
if (url.length > 0 && !url.startsWith('/')) {
|
50
|
url = '/' + url;
|
51
|
}
|
52
|
return this._baseHref + url;
|
53
|
}
|
54
|
go(path, query = '') {
|
55
|
path = this.prepareExternalUrl(path);
|
56
|
if (this._historyIndex > 0) {
|
57
|
this._history.splice(this._historyIndex + 1);
|
58
|
}
|
59
|
this._history.push(new LocationState(path, query));
|
60
|
this._historyIndex = this._history.length - 1;
|
61
|
const locationState = this._history[this._historyIndex - 1];
|
62
|
if (locationState.path == path && locationState.query == query) {
|
63
|
return;
|
64
|
}
|
65
|
const url = path + (query.length > 0 ? ('?' + query) : '');
|
66
|
this.urlChanges.push(url);
|
67
|
this._subject.emit({ 'url': url, 'pop': false });
|
68
|
}
|
69
|
replaceState(path, query = '') {
|
70
|
path = this.prepareExternalUrl(path);
|
71
|
const history = this._history[this._historyIndex];
|
72
|
if (history.path == path && history.query == query) {
|
73
|
return;
|
74
|
}
|
75
|
history.path = path;
|
76
|
history.query = query;
|
77
|
const url = path + (query.length > 0 ? ('?' + query) : '');
|
78
|
this.urlChanges.push('replace: ' + url);
|
79
|
}
|
80
|
forward() {
|
81
|
if (this._historyIndex < (this._history.length - 1)) {
|
82
|
this._historyIndex++;
|
83
|
this._subject.emit({ 'url': this.path(), 'pop': true });
|
84
|
}
|
85
|
}
|
86
|
back() {
|
87
|
if (this._historyIndex > 0) {
|
88
|
this._historyIndex--;
|
89
|
this._subject.emit({ 'url': this.path(), 'pop': true });
|
90
|
}
|
91
|
}
|
92
|
subscribe(onNext, onThrow, onReturn) {
|
93
|
return this._subject.subscribe({ next: onNext, error: onThrow, complete: onReturn });
|
94
|
}
|
95
|
normalize(url) { return null; }
|
96
|
}
|
97
|
SpyLocation.decorators = [
|
98
|
{ type: Injectable },
|
99
|
];
|
100
|
/** @nocollapse */
|
101
|
SpyLocation.ctorParameters = () => [];
|
102
|
class LocationState {
|
103
|
constructor(path, query) {
|
104
|
this.path = path;
|
105
|
this.query = query;
|
106
|
}
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* @license
|
111
|
* Copyright Google Inc. All Rights Reserved.
|
112
|
*
|
113
|
* Use of this source code is governed by an MIT-style license that can be
|
114
|
* found in the LICENSE file at https://angular.io/license
|
115
|
*/
|
116
|
/**
|
117
|
* A mock implementation of {@link LocationStrategy} that allows tests to fire simulated
|
118
|
* location events.
|
119
|
*
|
120
|
* @stable
|
121
|
*/
|
122
|
class MockLocationStrategy extends LocationStrategy {
|
123
|
constructor() {
|
124
|
super();
|
125
|
this.internalBaseHref = '/';
|
126
|
this.internalPath = '/';
|
127
|
this.internalTitle = '';
|
128
|
this.urlChanges = [];
|
129
|
/** @internal */
|
130
|
this._subject = new EventEmitter();
|
131
|
}
|
132
|
simulatePopState(url) {
|
133
|
this.internalPath = url;
|
134
|
this._subject.emit(new _MockPopStateEvent(this.path()));
|
135
|
}
|
136
|
path(includeHash = false) { return this.internalPath; }
|
137
|
prepareExternalUrl(internal) {
|
138
|
if (internal.startsWith('/') && this.internalBaseHref.endsWith('/')) {
|
139
|
return this.internalBaseHref + internal.substring(1);
|
140
|
}
|
141
|
return this.internalBaseHref + internal;
|
142
|
}
|
143
|
pushState(ctx, title, path, query) {
|
144
|
this.internalTitle = title;
|
145
|
const url = path + (query.length > 0 ? ('?' + query) : '');
|
146
|
this.internalPath = url;
|
147
|
const externalUrl = this.prepareExternalUrl(url);
|
148
|
this.urlChanges.push(externalUrl);
|
149
|
}
|
150
|
replaceState(ctx, title, path, query) {
|
151
|
this.internalTitle = title;
|
152
|
const url = path + (query.length > 0 ? ('?' + query) : '');
|
153
|
this.internalPath = url;
|
154
|
const externalUrl = this.prepareExternalUrl(url);
|
155
|
this.urlChanges.push('replace: ' + externalUrl);
|
156
|
}
|
157
|
onPopState(fn) { this._subject.subscribe({ next: fn }); }
|
158
|
getBaseHref() { return this.internalBaseHref; }
|
159
|
back() {
|
160
|
if (this.urlChanges.length > 0) {
|
161
|
this.urlChanges.pop();
|
162
|
const nextUrl = this.urlChanges.length > 0 ? this.urlChanges[this.urlChanges.length - 1] : '';
|
163
|
this.simulatePopState(nextUrl);
|
164
|
}
|
165
|
}
|
166
|
forward() { throw 'not implemented'; }
|
167
|
}
|
168
|
MockLocationStrategy.decorators = [
|
169
|
{ type: Injectable },
|
170
|
];
|
171
|
/** @nocollapse */
|
172
|
MockLocationStrategy.ctorParameters = () => [];
|
173
|
class _MockPopStateEvent {
|
174
|
constructor(newUrl) {
|
175
|
this.newUrl = newUrl;
|
176
|
this.pop = true;
|
177
|
this.type = 'popstate';
|
178
|
}
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* @license
|
183
|
* Copyright Google Inc. All Rights Reserved.
|
184
|
*
|
185
|
* Use of this source code is governed by an MIT-style license that can be
|
186
|
* found in the LICENSE file at https://angular.io/license
|
187
|
*/
|
188
|
/**
|
189
|
* @module
|
190
|
* @description
|
191
|
* Entry point for all public APIs of the common/testing package.
|
192
|
*/
|
193
|
|
194
|
/**
|
195
|
* @license
|
196
|
* Copyright Google Inc. All Rights Reserved.
|
197
|
*
|
198
|
* Use of this source code is governed by an MIT-style license that can be
|
199
|
* found in the LICENSE file at https://angular.io/license
|
200
|
*/
|
201
|
/**
|
202
|
* @module
|
203
|
* @description
|
204
|
* Entry point for all public APIs of the core/testing package.
|
205
|
*/
|
206
|
|
207
|
export { SpyLocation, MockLocationStrategy };
|
208
|
//# sourceMappingURL=testing.js.map
|