Skip to content

Commit 4929577

Browse files
atscottthePunderWoman
authored andcommitted
refactor(core): Use the provided Document value rather than global in FakeNavigation
This commit ensures the Document used by `FakeNavigation` is the one passed in the constructor rather than the global `document`, which may be different.
1 parent 473dd3e commit 4929577

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

packages/common/testing/src/mock_platform_location.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,15 @@ export class FakeNavigationPlatformLocation implements PlatformLocation {
302302
return this.config?.appBaseHref ?? '';
303303
}
304304

305+
// window, addEventListener, removeEventListener might be undefined due to test mocks
305306
onPopState(fn: LocationChangeListener): VoidFunction {
306-
this._platformNavigation.window.addEventListener('popstate', fn);
307-
return () => this._platformNavigation.window.removeEventListener('popstate', fn);
307+
this._platformNavigation.window?.addEventListener?.('popstate', fn);
308+
return () => this._platformNavigation.window?.removeEventListener?.('popstate', fn);
308309
}
309310

310311
onHashChange(fn: LocationChangeListener): VoidFunction {
311-
this._platformNavigation.window.addEventListener('hashchange', fn as any);
312-
return () => this._platformNavigation.window.removeEventListener('hashchange', fn as any);
312+
this._platformNavigation.window?.addEventListener?.('hashchange', fn as any);
313+
return () => this._platformNavigation.window?.removeEventListener?.('hashchange', fn as any);
313314
}
314315

315316
get href(): string {

packages/core/primitives/dom-navigation/testing/fake_navigation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class FakeNavigation implements Navigation {
125125
return new EventTarget();
126126
}
127127
};
128-
this._window = document.defaultView ?? this.createEventTarget();
128+
this._window = doc.defaultView ?? this.createEventTarget();
129129
this.eventTarget = this.createEventTarget();
130130
// First entry.
131131
this.setInitialEntryForTesting(startURL);

0 commit comments

Comments
 (0)