angular-docs-cn/modules/@angular/common/testing/location_mock.ts

125 lines
3.6 KiB
TypeScript
Raw Normal View History

/**
* @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
*/
import {Location, LocationStrategy} from '@angular/common';
import {EventEmitter, Injectable} from '@angular/core';
2015-04-21 14:23:23 -04:00
/**
* A spy for {@link Location} that allows tests to fire simulated location events.
*
* @experimental
*/
@Injectable()
2015-08-26 14:41:41 -04:00
export class SpyLocation implements Location {
urlChanges: string[] = [];
private _history: LocationState[] = [new LocationState('', '')];
private _historyIndex: number = 0;
/** @internal */
_subject: EventEmitter<any> = new EventEmitter();
/** @internal */
2015-08-26 14:41:41 -04:00
_baseHref: string = '';
/** @internal */
_platformStrategy: LocationStrategy = null;
2015-04-21 14:23:23 -04:00
setInitialPath(url: string) { this._history[this._historyIndex].path = url; }
2015-04-21 14:23:23 -04:00
setBaseHref(url: string) { this._baseHref = url; }
path(): string { return this._history[this._historyIndex].path; }
2015-04-21 14:23:23 -04:00
isCurrentPathEqualTo(path: string, query: string = ''): boolean {
const givenPath = path.endsWith('/') ? path.substring(0, path.length - 1) : path;
const currPath =
this.path().endsWith('/') ? this.path().substring(0, this.path().length - 1) : this.path();
return currPath == givenPath + (query.length > 0 ? ('?' + query) : '');
}
simulateUrlPop(pathname: string) { this._subject.emit({'url': pathname, 'pop': true}); }
simulateHashChange(pathname: string) {
// Because we don't prevent the native event, the browser will independently update the path
this.setInitialPath(pathname);
this.urlChanges.push('hash: ' + pathname);
this._subject.emit({'url': pathname, 'pop': true, 'type': 'hashchange'});
}
2015-04-21 14:23:23 -04:00
prepareExternalUrl(url: string): string {
if (url.length > 0 && !url.startsWith('/')) {
url = '/' + url;
}
return this._baseHref + url;
}
go(path: string, query: string = '') {
path = this.prepareExternalUrl(path);
if (this._historyIndex > 0) {
this._history.splice(this._historyIndex + 1);
}
this._history.push(new LocationState(path, query));
this._historyIndex = this._history.length - 1;
const locationState = this._history[this._historyIndex - 1];
if (locationState.path == path && locationState.query == query) {
2015-04-21 14:23:23 -04:00
return;
}
const url = path + (query.length > 0 ? ('?' + query) : '');
this.urlChanges.push(url);
this._subject.emit({'url': url, 'pop': false});
2015-04-21 14:23:23 -04:00
}
replaceState(path: string, query: string = '') {
path = this.prepareExternalUrl(path);
const history = this._history[this._historyIndex];
if (history.path == path && history.query == query) {
return;
}
history.path = path;
history.query = query;
const url = path + (query.length > 0 ? ('?' + query) : '');
this.urlChanges.push('replace: ' + url);
}
2015-04-21 14:23:23 -04:00
forward() {
if (this._historyIndex < (this._history.length - 1)) {
this._historyIndex++;
this._subject.emit({'url': this.path(), 'pop': true});
}
2015-04-21 14:23:23 -04:00
}
back() {
if (this._historyIndex > 0) {
this._historyIndex--;
this._subject.emit({'url': this.path(), 'pop': true});
}
2015-04-21 14:23:23 -04:00
}
subscribe(
onNext: (value: any) => void, onThrow: (error: any) => void = null,
onReturn: () => void = null): Object {
return this._subject.subscribe({next: onNext, error: onThrow, complete: onReturn});
2015-04-21 14:23:23 -04:00
}
2015-08-26 14:41:41 -04:00
normalize(url: string): string { return null; }
2015-04-21 14:23:23 -04:00
}
class LocationState {
path: string;
query: string;
constructor(path: string, query: string) {
this.path = path;
this.query = query;
}
}