angular-docs-cn/modules/angular2/src/mock/location_mock.ts

59 lines
1.4 KiB
TypeScript
Raw Normal View History

2015-04-21 14:23:23 -04:00
import {SpyObject, proxy} from 'angular2/test_lib';
2015-08-20 17:28:25 -04:00
import {IMPLEMENTS} from 'angular2/src/core/facade/lang';
import {EventEmitter, ObservableWrapper} from 'angular2/src/core/facade/async';
import {List, ListWrapper} from 'angular2/src/core/facade/collection';
2015-04-21 14:23:23 -04:00
import {Location} from 'angular2/src/router/location';
@proxy
2015-04-21 14:23:23 -04:00
@IMPLEMENTS(Location)
export class SpyLocation extends SpyObject {
urlChanges: List<string>;
_path: string;
_subject: EventEmitter;
_baseHref: string;
2015-04-21 14:23:23 -04:00
constructor() {
super();
this._path = '';
this.urlChanges = [];
2015-04-21 14:23:23 -04:00
this._subject = new EventEmitter();
this._baseHref = '';
2015-04-21 14:23:23 -04:00
}
setInitialPath(url: string) { this._path = url; }
2015-04-21 14:23:23 -04:00
setBaseHref(url: string) { this._baseHref = url; }
path(): string { return this._path; }
2015-04-21 14:23:23 -04:00
simulateUrlPop(pathname: string) { ObservableWrapper.callNext(this._subject, {'url': pathname}); }
2015-04-21 14:23:23 -04:00
normalizeAbsolutely(url: string): string { return this._baseHref + url; }
go(url: string) {
url = this.normalizeAbsolutely(url);
if (this._path == url) {
2015-04-21 14:23:23 -04:00
return;
}
this._path = url;
this.urlChanges.push(url);
2015-04-21 14:23:23 -04:00
}
forward() {
// TODO
}
back() {
// TODO
}
subscribe(onNext: (value: any) => void, onThrow: (error: any) => void = null,
onReturn: () => void = null) {
2015-04-21 14:23:23 -04:00
ObservableWrapper.subscribe(this._subject, onNext, onThrow, onReturn);
}
noSuchMethod(m: any) { super.noSuchMethod(m); }
2015-04-21 14:23:23 -04:00
}