Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): Fix MockPlatformLocation events and missing onPopState i… #48113

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 14 additions & 10 deletions packages/common/testing/src/mock_platform_location.ts
Expand Up @@ -105,6 +105,7 @@ export const MOCK_PLATFORM_LOCATION_CONFIG =
export class MockPlatformLocation implements PlatformLocation {
private baseHref: string = '';
private hashUpdate = new Subject<LocationChangeEvent>();
private popStateSubject = new Subject<LocationChangeEvent>();
private urlChangeIndex: number = 0;
private urlChanges: {
hostname: string,
Expand Down Expand Up @@ -155,9 +156,8 @@ export class MockPlatformLocation implements PlatformLocation {
}

onPopState(fn: LocationChangeListener): VoidFunction {
// No-op: a state stack is not implemented, so
// no events will ever come.
return () => {};
const subscription = this.popStateSubject.subscribe(fn);
return () => subscription.unsubscribe();
}

onHashChange(fn: LocationChangeListener): VoidFunction {
Expand Down Expand Up @@ -231,14 +231,18 @@ export class MockPlatformLocation implements PlatformLocation {
}

private scheduleHashUpdate(oldHash: string, oldUrl: string) {
atscott marked this conversation as resolved.
Show resolved Hide resolved
// Browsers are inconsistent in when they fire events and perform the state updates
// The most easiest thing to do in our mock is synchronous and that happens to match
// Firefox and Chrome, at least somewhat closely
//
// https://github.com/WICG/navigation-api#watching-for-navigations
// https://docs.google.com/document/d/1Pdve-DJ1JCGilj9Yqf5HxRJyBKSel5owgOvUJqTauwU/edit#heading=h.3ye4v71wsz94
this.popStateSubject.next(
{type: 'popstate', state: this.getState(), oldUrl, newUrl: this.url} as
LocationChangeEvent);
if (oldHash !== this.hash) {
scheduleMicroTask(
() => this.hashUpdate.next(
{type: 'hashchange', state: null, oldUrl, newUrl: this.url} as LocationChangeEvent));
this.hashUpdate.next(
{type: 'hashchange', state: null, oldUrl, newUrl: this.url} as LocationChangeEvent);
}
}
}

export function scheduleMicroTask(cb: () => any) {
Promise.resolve().then(cb);
}