Skip to content

Commit

Permalink
Fix undefined property bug in createCoordinates()
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Jun 21, 2024
1 parent 4926bf4 commit e9e9c6e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 16 additions & 11 deletions src/geolocation-coordinates.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
let canConstruct = false;

export function createCoordinates(
coords: Partial<globalThis.GeolocationCoordinates> = {},
): globalThis.GeolocationCoordinates {
export function createCoordinates({
latitude = 0,
longitude = 0,
altitude = null,
accuracy = 0,
altitudeAccuracy = null,
heading = null,
speed = null,
}: Partial<globalThis.GeolocationCoordinates> = {}): 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,
});
}

Expand Down
28 changes: 28 additions & 0 deletions test/vitest/create-coordinates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
});
});
});
34 changes: 34 additions & 0 deletions test/vitest/create-position.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit e9e9c6e

Please sign in to comment.