Skip to content

Commit

Permalink
Fix an issue with observing NaN heading coords
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Aug 8, 2024
1 parent ba057a7 commit e53d336
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/geolocation-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ export function createGeolocationObserver(

nextMatcher: for (const matcher of matchers) {
for (const property in matcher) {
const a = matcher[property as keyof typeof matcher];
const b = coords[property as keyof typeof coords];

if (
matcher[property as keyof typeof matcher] !==
coords[property as keyof typeof coords]
a !== b &&
/* v8 ignore start: can't test without another execution context */
!(typeof a === "undefined" && typeof b === "undefined") &&
/* v8 ignore stop */
!(Number.isNaN(a) && Number.isNaN(b))
) {
continue nextMatcher;
}
Expand Down
17 changes: 17 additions & 0 deletions test/vitest/geolocation-observer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ describe("GeolocationObserver", () => {
});
});

describe("when called with a matcher with a NaN heading", () => {
it("resolves when the coords match", async () => {
locationServices.setLowAccuracyCoordinates({
...coordsA,
heading: NaN,
});
permissionStore.setStatus({ name: "geolocation" }, "GRANTED");
await runTaskQueue();
const observer = createGeolocationObserver(geolocation, permissions);
await runTaskQueue();

await expect(
observer.waitForCoordinates({ heading: NaN }),
).resolves.toBeUndefined();
});
});

describe("when called with an async task function", () => {
it("runs the task while waiting", async () => {
locationServices.setLowAccuracyCoordinates(coordsA);
Expand Down

0 comments on commit e53d336

Please sign in to comment.