Skip to content

Commit

Permalink
fix: Parse.GeoPoint.current returns undefined (#2127)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis committed May 16, 2024
1 parent d366dff commit 3860535
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
20 changes: 11 additions & 9 deletions src/ParseGeoPoint.js → src/ParseGeoPoint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @flow
*/

/**
* Creates a new GeoPoint with any of the following forms:<br>
* <pre>
Expand Down Expand Up @@ -102,7 +98,7 @@ class ParseGeoPoint {
};
}

equals(other: mixed): boolean {
equals(other: any): boolean {
return (
other instanceof ParseGeoPoint &&
this.latitude === other.latitude &&
Expand Down Expand Up @@ -183,12 +179,18 @@ class ParseGeoPoint {
/**
* Creates a GeoPoint with the user's current location, if available.
*
* @param {object} options The options.
* @param {boolean} [options.enableHighAccuracy=false] A boolean value that indicates the application would like to receive the best possible results. If true and if the device is able to provide a more accurate position, it will do so. Note that this can result in slower response times or increased power consumption (with a GPS chip on a mobile device for example). On the other hand, if false, the device can take the liberty to save resources by responding more quickly and/or using less power. Default: false.
* @param {number} [options.timeout=Infinity] A positive long value representing the maximum length of time (in milliseconds) the device is allowed to take in order to return a position. The default value is Infinity, meaning that getCurrentPosition() won't return until the position is available.
* @param {number} [options.maximumAge=0] A positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return. If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position. If set to Infinity the device must return a cached position regardless of its age. Default: 0.
* @static
* @returns {Parse.GeoPoint} User's current location
* @returns {Promise<Parse.GeoPoint>} User's current location
*/
static current() {
return navigator.geolocation.getCurrentPosition(location => {
return new ParseGeoPoint(location.coords.latitude, location.coords.longitude);
static current(options): Promise<ParseGeoPoint> {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(location => {
resolve(new ParseGeoPoint(location.coords.latitude, location.coords.longitude));
}, reject, options);
});
}
}
Expand Down
35 changes: 24 additions & 11 deletions src/__tests__/ParseGeoPoint-test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
jest.autoMockOff();

const ParseGeoPoint = require('../ParseGeoPoint').default;
global.navigator.geolocation = {
getCurrentPosition: cb => {
return cb({
coords: {
latitude: 10,
longitude: 20,
},
});
},
};

describe('GeoPoint', () => {
it('can be constructed from various inputs', () => {
Expand Down Expand Up @@ -217,8 +207,31 @@ describe('GeoPoint', () => {
});

it('can get current location', async () => {
const geoPoint = ParseGeoPoint.current();
global.navigator.geolocation = {
getCurrentPosition: (success, _, options) => {
success({
coords: {
latitude: 10,
longitude: 20,
},
});
expect(options).toEqual({ timeout: 5000 });
},
};
const geoPoint = await ParseGeoPoint.current({ timeout: 5000 });
expect(geoPoint.latitude).toBe(10);
expect(geoPoint.longitude).toBe(20);
});

it('can get current location error', async () => {
global.navigator.geolocation = {
getCurrentPosition: (_, error, options) => {
error({
message: 'PERMISSION_DENIED',
});
expect(options).toEqual({ timeout: 5000 });
},
};
await expect(ParseGeoPoint.current({ timeout: 5000 })).rejects.toEqual({ message: 'PERMISSION_DENIED' });
});
});
36 changes: 18 additions & 18 deletions types/ParseGeoPoint.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
// @ts-nocheck
export default ParseGeoPoint;
/**
* @flow
*/
/**
* Creates a new GeoPoint with any of the following forms:<br>
* <pre>
Expand All @@ -27,14 +22,8 @@ export default ParseGeoPoint;
* @alias Parse.GeoPoint
*/
declare class ParseGeoPoint {
static _validate(latitude: number, longitude: number): void;
/**
* Creates a GeoPoint with the user's current location, if available.
*
* @static
* @returns {Parse.GeoPoint} User's current location
*/
static current(): Parse.GeoPoint;
_latitude: number;
_longitude: number;
/**
* @param {(number[] | object | number)} arg1 Either a list of coordinate pairs, an object with `latitude`, `longitude`, or the latitude or the point.
* @param {number} arg2 The longitude of the GeoPoint
Expand All @@ -43,9 +32,6 @@ declare class ParseGeoPoint {
latitude: number;
longitude: number;
} | number, arg2?: number);
_latitude: number;
_longitude: number;
set latitude(arg: number);
/**
* North-south portion of the coordinate, in range [-90, 90].
* Throws an exception if set out of range in a modern browser.
Expand All @@ -54,7 +40,7 @@ declare class ParseGeoPoint {
* @returns {number}
*/
get latitude(): number;
set longitude(arg: number);
set latitude(val: number);
/**
* East-west portion of the coordinate, in range [-180, 180].
* Throws if set out of range in a modern browser.
Expand All @@ -63,6 +49,7 @@ declare class ParseGeoPoint {
* @returns {number}
*/
get longitude(): number;
set longitude(val: number);
/**
* Returns a JSON representation of the GeoPoint, suitable for Parse.
*
Expand All @@ -73,7 +60,7 @@ declare class ParseGeoPoint {
latitude: number;
longitude: number;
};
equals(other: mixed): boolean;
equals(other: any): boolean;
/**
* Returns the distance from this GeoPoint to another in radians.
*
Expand All @@ -95,4 +82,17 @@ declare class ParseGeoPoint {
* @returns {number}
*/
milesTo(point: ParseGeoPoint): number;
static _validate(latitude: number, longitude: number): void;
/**
* Creates a GeoPoint with the user's current location, if available.
*
* @param {object} options The options.
* @param {boolean} [options.enableHighAccuracy=false] A boolean value that indicates the application would like to receive the best possible results. If true and if the device is able to provide a more accurate position, it will do so. Note that this can result in slower response times or increased power consumption (with a GPS chip on a mobile device for example). On the other hand, if false, the device can take the liberty to save resources by responding more quickly and/or using less power. Default: false.
* @param {number} [options.timeout=Infinity] A positive long value representing the maximum length of time (in milliseconds) the device is allowed to take in order to return a position. The default value is Infinity, meaning that getCurrentPosition() won't return until the position is available.
* @param {number} [options.maximumAge=0] A positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return. If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position. If set to Infinity the device must return a cached position regardless of its age. Default: 0.
* @static
* @returns {Promise<Parse.GeoPoint>} User's current location
*/
static current(options: any): Promise<ParseGeoPoint>;
}
export default ParseGeoPoint;

0 comments on commit 3860535

Please sign in to comment.