angular2
Version:
Angular 2 - a web framework for modern web apps
74 lines (73 loc) • 3.17 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';
import { LocationStrategy } from 'angular2/platform/common';
/**
* A mock implementation of {@link LocationStrategy} that allows tests to fire simulated
* location events.
*/
export let MockLocationStrategy = class MockLocationStrategy extends LocationStrategy {
constructor() {
super();
this.internalBaseHref = '/';
this.internalPath = '/';
this.internalTitle = '';
this.urlChanges = [];
/** @internal */
this._subject = new EventEmitter();
}
simulatePopState(url) {
this.internalPath = url;
ObservableWrapper.callEmit(this._subject, new _MockPopStateEvent(this.path()));
}
path() { return this.internalPath; }
prepareExternalUrl(internal) {
if (internal.startsWith('/') && this.internalBaseHref.endsWith('/')) {
return this.internalBaseHref + internal.substring(1);
}
return this.internalBaseHref + internal;
}
pushState(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);
}
replaceState(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);
}
onPopState(fn) { ObservableWrapper.subscribe(this._subject, fn); }
getBaseHref() { return this.internalBaseHref; }
back() {
if (this.urlChanges.length > 0) {
this.urlChanges.pop();
var nextUrl = this.urlChanges.length > 0 ? this.urlChanges[this.urlChanges.length - 1] : '';
this.simulatePopState(nextUrl);
}
}
forward() { throw 'not implemented'; }
};
MockLocationStrategy = __decorate([
Injectable(),
__metadata('design:paramtypes', [])
], MockLocationStrategy);
class _MockPopStateEvent {
constructor(newUrl) {
this.newUrl = newUrl;
this.pop = true;
this.type = 'popstate';
}
}