angular2
Version:
Angular 2 - a web framework for modern web apps
79 lines (78 loc) • 3.2 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Injectable } from 'angular2/src/core/di';
import { EventEmitter, ObservableWrapper } from 'angular2/src/facade/async';
/**
* A spy for {@link Location} that allows tests to fire simulated location events.
*/
export let SpyLocation = class SpyLocation {
constructor() {
this.urlChanges = [];
/** @internal */
this._path = '';
/** @internal */
this._query = '';
/** @internal */
this._subject = new EventEmitter();
/** @internal */
this._baseHref = '';
// TODO: remove these once Location is an interface, and can be implemented cleanly
this.platformStrategy = null;
}
setInitialPath(url) { this._path = url; }
setBaseHref(url) { this._baseHref = url; }
path() { return this._path; }
simulateUrlPop(pathname) {
ObservableWrapper.callEmit(this._subject, { 'url': pathname, 'pop': true });
}
simulateHashChange(pathname) {
// Because we don't prevent the native event, the browser will independently update the path
this.setInitialPath(pathname);
this.urlChanges.push('hash: ' + pathname);
ObservableWrapper.callEmit(this._subject, { 'url': pathname, 'pop': true, 'type': 'hashchange' });
}
prepareExternalUrl(url) {
if (url.length > 0 && !url.startsWith('/')) {
url = '/' + url;
}
return this._baseHref + url;
}
go(path, query = '') {
path = this.prepareExternalUrl(path);
if (this._path == path && this._query == query) {
return;
}
this._path = path;
this._query = query;
var url = path + (query.length > 0 ? ('?' + query) : '');
this.urlChanges.push(url);
}
replaceState(path, query = '') {
path = this.prepareExternalUrl(path);
this._path = path;
this._query = query;
var url = path + (query.length > 0 ? ('?' + query) : '');
this.urlChanges.push('replace: ' + url);
}
forward() {
// TODO
}
back() {
// TODO
}
subscribe(onNext, onThrow = null, onReturn = null) {
return ObservableWrapper.subscribe(this._subject, onNext, onThrow, onReturn);
}
normalize(url) { return null; }
};
SpyLocation = __decorate([
Injectable(),
__metadata('design:paramtypes', [])
], SpyLocation);