2015-12-03 12:06:42 -05:00
|
|
|
import {Injectable} from 'angular2/src/core/di';
|
2015-11-06 20:34:07 -05:00
|
|
|
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
|
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
2016-02-09 14:12:41 -05:00
|
|
|
import {Location} from 'angular2/src/router/location/location';
|
2015-04-21 14:23:23 -04:00
|
|
|
|
2015-12-03 18:49:09 -05:00
|
|
|
/**
|
|
|
|
* A spy for {@link Location} that allows tests to fire simulated location events.
|
|
|
|
*/
|
2015-12-03 12:06:42 -05:00
|
|
|
@Injectable()
|
2015-08-26 14:41:41 -04:00
|
|
|
export class SpyLocation implements Location {
|
2015-08-28 14:29:19 -04:00
|
|
|
urlChanges: string[] = [];
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-08-26 14:41:41 -04:00
|
|
|
_path: string = '';
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-09-23 03:10:26 -04:00
|
|
|
_query: string = '';
|
|
|
|
/** @internal */
|
2015-10-24 21:48:43 -04:00
|
|
|
_subject: EventEmitter<any> = new EventEmitter();
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-08-26 14:41:41 -04:00
|
|
|
_baseHref: string = '';
|
2015-04-21 14:23:23 -04:00
|
|
|
|
2015-05-29 17:58:41 -04:00
|
|
|
setInitialPath(url: string) { this._path = url; }
|
2015-04-21 14:23:23 -04:00
|
|
|
|
2015-05-29 17:58:41 -04:00
|
|
|
setBaseHref(url: string) { this._baseHref = url; }
|
2015-05-12 19:18:58 -04:00
|
|
|
|
2015-05-29 17:58:41 -04:00
|
|
|
path(): string { return this._path; }
|
2015-04-21 14:23:23 -04:00
|
|
|
|
2015-12-07 19:05:57 -05:00
|
|
|
simulateUrlPop(pathname: string) {
|
|
|
|
ObservableWrapper.callEmit(this._subject, {'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);
|
|
|
|
ObservableWrapper.callEmit(this._subject, {'url': pathname, 'pop': true, 'type': 'hashchange'});
|
|
|
|
}
|
2015-04-21 14:23:23 -04:00
|
|
|
|
2015-10-26 09:57:41 -04:00
|
|
|
prepareExternalUrl(url: string): string {
|
|
|
|
if (url.length > 0 && !url.startsWith('/')) {
|
|
|
|
url = '/' + url;
|
|
|
|
}
|
|
|
|
return this._baseHref + url;
|
|
|
|
}
|
2015-05-12 19:18:58 -04:00
|
|
|
|
2015-09-23 03:10:26 -04:00
|
|
|
go(path: string, query: string = '') {
|
2015-10-26 09:57:41 -04:00
|
|
|
path = this.prepareExternalUrl(path);
|
2015-09-23 03:10:26 -04:00
|
|
|
if (this._path == path && this._query == query) {
|
2015-04-21 14:23:23 -04:00
|
|
|
return;
|
|
|
|
}
|
2015-09-23 03:10:26 -04:00
|
|
|
this._path = path;
|
|
|
|
this._query = query;
|
|
|
|
|
|
|
|
var url = path + (query.length > 0 ? ('?' + query) : '');
|
2015-06-17 14:17:21 -04:00
|
|
|
this.urlChanges.push(url);
|
2015-04-21 14:23:23 -04:00
|
|
|
}
|
|
|
|
|
2015-12-07 19:05:57 -05:00
|
|
|
replaceState(path: string, query: string = '') {
|
|
|
|
path = this.prepareExternalUrl(path);
|
|
|
|
this._path = path;
|
|
|
|
this._query = query;
|
|
|
|
|
|
|
|
var url = path + (query.length > 0 ? ('?' + query) : '');
|
|
|
|
this.urlChanges.push('replace: ' + url);
|
|
|
|
}
|
|
|
|
|
2015-04-21 14:23:23 -04:00
|
|
|
forward() {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
back() {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
subscribe(
|
|
|
|
onNext: (value: any) => void, onThrow: (error: any) => void = null,
|
|
|
|
onReturn: () => void = null): Object {
|
2015-10-26 14:00:24 -04:00
|
|
|
return ObservableWrapper.subscribe(this._subject, onNext, onThrow, onReturn);
|
2015-04-21 14:23:23 -04:00
|
|
|
}
|
|
|
|
|
2015-08-26 14:41:41 -04:00
|
|
|
// TODO: remove these once Location is an interface, and can be implemented cleanly
|
|
|
|
platformStrategy: any = null;
|
|
|
|
normalize(url: string): string { return null; }
|
2015-04-21 14:23:23 -04:00
|
|
|
}
|