From e9e9c6ecaa800d8f13de44a8bdc63817559a7ffa Mon Sep 17 00:00:00 2001 From: Erin Millard Date: Fri, 21 Jun 2024 11:56:42 +1000 Subject: [PATCH] Fix undefined property bug in createCoordinates() --- CHANGELOG.md | 6 +++++ src/geolocation-coordinates.ts | 27 +++++++++++--------- test/vitest/create-coordinates.spec.ts | 28 +++++++++++++++++++++ test/vitest/create-position.spec.ts | 34 ++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9284470..33b4b6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ Versioning]. ## Unreleased +### Fixed + +- Fixed an issue where passing explicit `undefined` values to + `createCoordinates()` or `createPosition()` could produce a + `GeolocationCoordinates` object with `undefined` properties. + ## [v0.8.0] - 2024-06-21 [v0.8.0]: https://github.com/ezzatron/fake-geolocation/releases/tag/v0.8.0 diff --git a/src/geolocation-coordinates.ts b/src/geolocation-coordinates.ts index b940e32..58b7c1a 100644 --- a/src/geolocation-coordinates.ts +++ b/src/geolocation-coordinates.ts @@ -1,19 +1,24 @@ let canConstruct = false; -export function createCoordinates( - coords: Partial = {}, -): globalThis.GeolocationCoordinates { +export function createCoordinates({ + latitude = 0, + longitude = 0, + altitude = null, + accuracy = 0, + altitudeAccuracy = null, + heading = null, + speed = null, +}: Partial = {}): globalThis.GeolocationCoordinates { canConstruct = true; return new GeolocationCoordinates({ - latitude: 0, - longitude: 0, - altitude: null, - accuracy: 0, - altitudeAccuracy: null, - heading: null, - speed: null, - ...coords, + latitude, + longitude, + altitude, + accuracy, + altitudeAccuracy, + heading, + speed, }); } diff --git a/test/vitest/create-coordinates.spec.ts b/test/vitest/create-coordinates.spec.ts index a91a7b2..93b796e 100644 --- a/test/vitest/create-coordinates.spec.ts +++ b/test/vitest/create-coordinates.spec.ts @@ -71,4 +71,32 @@ describe("createCoordinates()", () => { }); }); }); + + describe("when explicit undefined properties are provided", () => { + const properties = { + latitude: undefined, + longitude: undefined, + accuracy: undefined, + altitude: undefined, + altitudeAccuracy: undefined, + heading: undefined, + speed: undefined, + } as const; + + it("creates coordinates", () => { + const coords = createCoordinates(properties); + + expect(coords).toBeInstanceOf(GeolocationCoordinates); + expect(coords).toEqual({ + latitude: 0, + longitude: 0, + altitude: null, + accuracy: 0, + altitudeAccuracy: null, + heading: null, + speed: null, + [Symbol.toStringTag]: "GeolocationCoordinates", + }); + }); + }); }); diff --git a/test/vitest/create-position.spec.ts b/test/vitest/create-position.spec.ts index 6038412..db13335 100644 --- a/test/vitest/create-position.spec.ts +++ b/test/vitest/create-position.spec.ts @@ -94,4 +94,38 @@ describe("createPosition()", () => { expect(isHighAccuracy(position)).toBe(true); }); }); + + describe("when explicit undefined arguments and properties are provided", () => { + const coordsProperties = { + latitude: undefined, + longitude: undefined, + accuracy: undefined, + altitude: undefined, + altitudeAccuracy: undefined, + heading: undefined, + speed: undefined, + } as const; + + it("creates a position", () => { + const position = createPosition(coordsProperties, undefined, undefined); + + expect(position).toBeInstanceOf(GeolocationPosition); + expect(position.coords).toBeInstanceOf(GeolocationCoordinates); + expect(position).toEqual({ + coords: { + latitude: 0, + longitude: 0, + altitude: null, + accuracy: 0, + altitudeAccuracy: null, + heading: null, + speed: null, + [Symbol.toStringTag]: "GeolocationCoordinates", + }, + timestamp: 0, + [Symbol.toStringTag]: "GeolocationPosition", + }); + expect(isHighAccuracy(position)).toBe(true); + }); + }); });