Skip to content

Commit 3860535

Browse files
authored
fix: Parse.GeoPoint.current returns undefined (#2127)
1 parent d366dff commit 3860535

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

src/ParseGeoPoint.js renamed to src/ParseGeoPoint.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* @flow
3-
*/
4-
51
/**
62
* Creates a new GeoPoint with any of the following forms:<br>
73
* <pre>
@@ -102,7 +98,7 @@ class ParseGeoPoint {
10298
};
10399
}
104100

105-
equals(other: mixed): boolean {
101+
equals(other: any): boolean {
106102
return (
107103
other instanceof ParseGeoPoint &&
108104
this.latitude === other.latitude &&
@@ -183,12 +179,18 @@ class ParseGeoPoint {
183179
/**
184180
* Creates a GeoPoint with the user's current location, if available.
185181
*
182+
* @param {object} options The options.
183+
* @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.
184+
* @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.
185+
* @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.
186186
* @static
187-
* @returns {Parse.GeoPoint} User's current location
187+
* @returns {Promise<Parse.GeoPoint>} User's current location
188188
*/
189-
static current() {
190-
return navigator.geolocation.getCurrentPosition(location => {
191-
return new ParseGeoPoint(location.coords.latitude, location.coords.longitude);
189+
static current(options): Promise<ParseGeoPoint> {
190+
return new Promise((resolve, reject) => {
191+
navigator.geolocation.getCurrentPosition(location => {
192+
resolve(new ParseGeoPoint(location.coords.latitude, location.coords.longitude));
193+
}, reject, options);
192194
});
193195
}
194196
}

src/__tests__/ParseGeoPoint-test.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
jest.autoMockOff();
22

33
const ParseGeoPoint = require('../ParseGeoPoint').default;
4-
global.navigator.geolocation = {
5-
getCurrentPosition: cb => {
6-
return cb({
7-
coords: {
8-
latitude: 10,
9-
longitude: 20,
10-
},
11-
});
12-
},
13-
};
144

155
describe('GeoPoint', () => {
166
it('can be constructed from various inputs', () => {
@@ -217,8 +207,31 @@ describe('GeoPoint', () => {
217207
});
218208

219209
it('can get current location', async () => {
220-
const geoPoint = ParseGeoPoint.current();
210+
global.navigator.geolocation = {
211+
getCurrentPosition: (success, _, options) => {
212+
success({
213+
coords: {
214+
latitude: 10,
215+
longitude: 20,
216+
},
217+
});
218+
expect(options).toEqual({ timeout: 5000 });
219+
},
220+
};
221+
const geoPoint = await ParseGeoPoint.current({ timeout: 5000 });
221222
expect(geoPoint.latitude).toBe(10);
222223
expect(geoPoint.longitude).toBe(20);
223224
});
225+
226+
it('can get current location error', async () => {
227+
global.navigator.geolocation = {
228+
getCurrentPosition: (_, error, options) => {
229+
error({
230+
message: 'PERMISSION_DENIED',
231+
});
232+
expect(options).toEqual({ timeout: 5000 });
233+
},
234+
};
235+
await expect(ParseGeoPoint.current({ timeout: 5000 })).rejects.toEqual({ message: 'PERMISSION_DENIED' });
236+
});
224237
});

types/ParseGeoPoint.d.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// @ts-nocheck
2-
export default ParseGeoPoint;
3-
/**
4-
* @flow
5-
*/
61
/**
72
* Creates a new GeoPoint with any of the following forms:<br>
83
* <pre>
@@ -27,14 +22,8 @@ export default ParseGeoPoint;
2722
* @alias Parse.GeoPoint
2823
*/
2924
declare class ParseGeoPoint {
30-
static _validate(latitude: number, longitude: number): void;
31-
/**
32-
* Creates a GeoPoint with the user's current location, if available.
33-
*
34-
* @static
35-
* @returns {Parse.GeoPoint} User's current location
36-
*/
37-
static current(): Parse.GeoPoint;
25+
_latitude: number;
26+
_longitude: number;
3827
/**
3928
* @param {(number[] | object | number)} arg1 Either a list of coordinate pairs, an object with `latitude`, `longitude`, or the latitude or the point.
4029
* @param {number} arg2 The longitude of the GeoPoint
@@ -43,9 +32,6 @@ declare class ParseGeoPoint {
4332
latitude: number;
4433
longitude: number;
4534
} | number, arg2?: number);
46-
_latitude: number;
47-
_longitude: number;
48-
set latitude(arg: number);
4935
/**
5036
* North-south portion of the coordinate, in range [-90, 90].
5137
* Throws an exception if set out of range in a modern browser.
@@ -54,7 +40,7 @@ declare class ParseGeoPoint {
5440
* @returns {number}
5541
*/
5642
get latitude(): number;
57-
set longitude(arg: number);
43+
set latitude(val: number);
5844
/**
5945
* East-west portion of the coordinate, in range [-180, 180].
6046
* Throws if set out of range in a modern browser.
@@ -63,6 +49,7 @@ declare class ParseGeoPoint {
6349
* @returns {number}
6450
*/
6551
get longitude(): number;
52+
set longitude(val: number);
6653
/**
6754
* Returns a JSON representation of the GeoPoint, suitable for Parse.
6855
*
@@ -73,7 +60,7 @@ declare class ParseGeoPoint {
7360
latitude: number;
7461
longitude: number;
7562
};
76-
equals(other: mixed): boolean;
63+
equals(other: any): boolean;
7764
/**
7865
* Returns the distance from this GeoPoint to another in radians.
7966
*
@@ -95,4 +82,17 @@ declare class ParseGeoPoint {
9582
* @returns {number}
9683
*/
9784
milesTo(point: ParseGeoPoint): number;
85+
static _validate(latitude: number, longitude: number): void;
86+
/**
87+
* Creates a GeoPoint with the user's current location, if available.
88+
*
89+
* @param {object} options The options.
90+
* @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.
91+
* @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.
92+
* @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.
93+
* @static
94+
* @returns {Promise<Parse.GeoPoint>} User's current location
95+
*/
96+
static current(options: any): Promise<ParseGeoPoint>;
9897
}
98+
export default ParseGeoPoint;

0 commit comments

Comments
 (0)