MCPcopy
hub / github.com/angular/angular / SpyLocation

Class SpyLocation

packages/common/testing/src/location_mock.ts:24–209  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22 */
23@Injectable()
24export class SpyLocation implements Location {
25 urlChanges: string[] = [];
26 private _history: LocationState[] = [new LocationState('', '', null)];
27 private _historyIndex: number = 0;
28 /** @internal */
29 _subject = new Subject<PopStateEvent>();
30 /** @internal */
31 _basePath: string = '';
32 /** @internal */
33 _locationStrategy: LocationStrategy = null!;
34 /** @internal */
35 _urlChangeListeners: ((url: string, state: unknown) => void)[] = [];
36 /** @internal */
37 _urlChangeSubscription: SubscriptionLike | null = null;
38
39 /** @docs-private */
40 ngOnDestroy(): void {
41 this._urlChangeSubscription?.unsubscribe();
42 this._urlChangeListeners = [];
43 }
44
45 setInitialPath(url: string) {
46 this._history[this._historyIndex].path = url;
47 }
48
49 setBaseHref(url: string) {
50 this._basePath = url;
51 }
52
53 path(): string {
54 return this._history[this._historyIndex].path;
55 }
56
57 getState(): unknown {
58 return this._history[this._historyIndex].state;
59 }
60
61 isCurrentPathEqualTo(path: string, query: string = ''): boolean {
62 const givenPath = path.endsWith('/') ? path.substring(0, path.length - 1) : path;
63 const currPath = this.path().endsWith('/')
64 ? this.path().substring(0, this.path().length - 1)
65 : this.path();
66
67 return currPath == givenPath + (query.length > 0 ? '?' + query : '');
68 }
69
70 simulateUrlPop(pathname: string) {
71 this._subject.next({'url': pathname, 'pop': true, 'type': 'popstate'});
72 }
73
74 simulateHashChange(pathname: string) {
75 const path = this.prepareExternalUrl(pathname);
76 this.pushHistory(path, '', null);
77
78 this.urlChanges.push('hash: ' + pathname);
79 // the browser will automatically fire popstate event before each `hashchange` event, so we need
80 // to simulate it.
81 this._subject.next({'url': pathname, 'pop': true, 'type': 'popstate'});

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…