mock_location_strategy.js
3.37 KB
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
/**
* @license
* Copyright Google Inc. 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
*/
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
import { LocationStrategy } from '@angular/common';
import { EventEmitter, Injectable } from '@angular/core';
/**
* A mock implementation of {@link LocationStrategy} that allows tests to fire simulated
* location events.
*
* @stable
*/
export var MockLocationStrategy = (function (_super) {
__extends(MockLocationStrategy, _super);
function MockLocationStrategy() {
_super.call(this);
this.internalBaseHref = '/';
this.internalPath = '/';
this.internalTitle = '';
this.urlChanges = [];
/** @internal */
this._subject = new EventEmitter();
}
MockLocationStrategy.prototype.simulatePopState = function (url) {
this.internalPath = url;
this._subject.emit(new _MockPopStateEvent(this.path()));
};
MockLocationStrategy.prototype.path = function (includeHash) {
if (includeHash === void 0) { includeHash = false; }
return this.internalPath;
};
MockLocationStrategy.prototype.prepareExternalUrl = function (internal) {
if (internal.startsWith('/') && this.internalBaseHref.endsWith('/')) {
return this.internalBaseHref + internal.substring(1);
}
return this.internalBaseHref + internal;
};
MockLocationStrategy.prototype.pushState = function (ctx, title, path, query) {
this.internalTitle = title;
var url = path + (query.length > 0 ? ('?' + query) : '');
this.internalPath = url;
var externalUrl = this.prepareExternalUrl(url);
this.urlChanges.push(externalUrl);
};
MockLocationStrategy.prototype.replaceState = function (ctx, title, path, query) {
this.internalTitle = title;
var url = path + (query.length > 0 ? ('?' + query) : '');
this.internalPath = url;
var externalUrl = this.prepareExternalUrl(url);
this.urlChanges.push('replace: ' + externalUrl);
};
MockLocationStrategy.prototype.onPopState = function (fn) { this._subject.subscribe({ next: fn }); };
MockLocationStrategy.prototype.getBaseHref = function () { return this.internalBaseHref; };
MockLocationStrategy.prototype.back = function () {
if (this.urlChanges.length > 0) {
this.urlChanges.pop();
var nextUrl = this.urlChanges.length > 0 ? this.urlChanges[this.urlChanges.length - 1] : '';
this.simulatePopState(nextUrl);
}
};
MockLocationStrategy.prototype.forward = function () { throw 'not implemented'; };
MockLocationStrategy.decorators = [
{ type: Injectable },
];
/** @nocollapse */
MockLocationStrategy.ctorParameters = function () { return []; };
return MockLocationStrategy;
}(LocationStrategy));
var _MockPopStateEvent = (function () {
function _MockPopStateEvent(newUrl) {
this.newUrl = newUrl;
this.pop = true;
this.type = 'popstate';
}
return _MockPopStateEvent;
}());
//# sourceMappingURL=mock_location_strategy.js.map