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