forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseGeoPoint-test.js
239 lines (194 loc) · 6.96 KB
/
ParseGeoPoint-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
jest.autoMockOff();
const ParseGeoPoint = require('../ParseGeoPoint').default;
describe('GeoPoint', () => {
it('can be constructed from various inputs', () => {
let point = new ParseGeoPoint();
expect(point.latitude).toBe(0);
expect(point.longitude).toBe(0);
point = new ParseGeoPoint(42, 36);
expect(point.latitude).toBe(42);
expect(point.longitude).toBe(36);
point = new ParseGeoPoint([12, 24]);
expect(point.latitude).toBe(12);
expect(point.longitude).toBe(24);
point = new ParseGeoPoint({ latitude: 8, longitude: 88 });
expect(point.latitude).toBe(8);
expect(point.longitude).toBe(88);
});
it('throws when created with non numbers values', () => {
[
[NaN, NaN],
[false, true],
['29', '10'],
[29, '10'],
['29', 10],
[['29', '10']],
[{ latitude: '29', longitude: '10' }],
].forEach(case_test => {
expect(function () {
new ParseGeoPoint(...case_test);
}).toThrow('GeoPoint latitude and longitude must be valid numbers');
});
});
it('can set latitude and longitude', () => {
const point = new ParseGeoPoint();
expect(point.latitude).toBe(0);
expect(point.longitude).toBe(0);
point.latitude = 5.5;
expect(point.latitude).toBe(5.5);
point.latitude = 10;
expect(point.latitude).toBe(10);
point.longitude = 12.1;
expect(point.longitude).toBe(12.1);
point.longitude = 14.9;
expect(point.longitude).toBe(14.9);
});
it('throws for points out of bounds', () => {
expect(() => {
new ParseGeoPoint(90.01, 0.0);
}).toThrow();
expect(() => {
new ParseGeoPoint(-90.01, 0.0);
}).toThrow();
expect(() => {
new ParseGeoPoint(0.0, 180.01);
}).toThrow();
expect(() => {
new ParseGeoPoint(0.0, -180.01);
}).toThrow();
});
it('can calculate distance in radians', () => {
const d2r = Math.PI / 180.0;
const pointA = new ParseGeoPoint();
const pointB = new ParseGeoPoint();
// Zero
expect(pointA.radiansTo(pointB)).toBe(0);
expect(pointB.radiansTo(pointA)).toBe(0);
// Wrap Long
pointA.longitude = 179.0;
pointB.longitude = -179.0;
expect(pointA.radiansTo(pointB)).toBeCloseTo(2 * d2r, 5);
expect(pointB.radiansTo(pointA)).toBeCloseTo(2 * d2r, 5);
// North South Lat
pointA.latitude = 89.0;
pointA.longitude = 0.0;
pointB.latitude = -89.0;
pointB.longitude = 0.0;
expect(pointA.radiansTo(pointB)).toBeCloseTo(178 * d2r, 5);
expect(pointB.radiansTo(pointA)).toBeCloseTo(178 * d2r, 5);
// Long wrap Lat
pointA.latitude = 89.0;
pointA.longitude = 0.0;
pointB.latitude = -89.0;
pointB.longitude = 179.9999;
expect(pointA.radiansTo(pointB)).toBeCloseTo(180 * d2r, 5);
expect(pointB.radiansTo(pointA)).toBeCloseTo(180 * d2r, 5);
pointA.latitude = 79.0;
pointA.longitude = 90.0;
pointB.latitude = -79.0;
pointB.longitude = -90;
expect(pointA.radiansTo(pointB)).toBeCloseTo(180 * d2r, 5);
expect(pointB.radiansTo(pointA)).toBeCloseTo(180 * d2r, 5);
// Wrap near pole - somewhat ill conditioned case due to pole proximity
pointA.latitude = 85.0;
pointA.longitude = 90.0;
pointB.latitude = 85.0;
pointB.longitude = -90;
expect(pointA.radiansTo(pointB)).toBeCloseTo(10 * d2r, 5);
expect(pointB.radiansTo(pointA)).toBeCloseTo(10 * d2r, 5);
// Reference cities
// Sydney Australia
pointA.latitude = -34.0;
pointA.longitude = 151.0;
// Buenos Aires
pointB.latitude = -34.5;
pointB.longitude = -58.35;
expect(pointA.radiansTo(pointB)).toBeCloseTo(1.85, 2);
expect(pointB.radiansTo(pointA)).toBeCloseTo(1.85, 2);
});
it('can calculate distances in mi and km', () => {
// [SAC] 38.52 -121.50 Sacramento,CA
const sacramento = new ParseGeoPoint(38.52, -121.5);
// [HNL] 21.35 -157.93 Honolulu Int,HI
const honolulu = new ParseGeoPoint(21.35, -157.93);
// [51Q] 37.75 -122.68 San Francisco,CA
const sanfran = new ParseGeoPoint(37.75, -122.68);
// Vorkuta 67.509619,64.085999
const vorkuta = new ParseGeoPoint(67.509619, 64.085999);
// London
const london = new ParseGeoPoint(51.501904, -0.115356);
// Northampton
const northampton = new ParseGeoPoint(52.241256, -0.895386);
// Powell St BART station
const powell = new ParseGeoPoint(37.785071, -122.407007);
// Apple store
const astore = new ParseGeoPoint(37.785809, -122.406363);
// Self
expect(honolulu.kilometersTo(honolulu)).toBeCloseTo(0.0, 3);
expect(honolulu.milesTo(honolulu)).toBeCloseTo(0.0, 3);
// Symmetric
expect(sacramento.kilometersTo(honolulu)).toBeCloseTo(3964.8, -2);
expect(honolulu.kilometersTo(sacramento)).toBeCloseTo(3964.8, -2);
expect(sacramento.milesTo(honolulu)).toBeCloseTo(2463.6, -1);
expect(honolulu.milesTo(sacramento)).toBeCloseTo(2463.6, -1);
// Semi-local
expect(london.kilometersTo(northampton)).toBeCloseTo(98.4, 0);
expect(london.milesTo(northampton)).toBeCloseTo(61.2, 0);
expect(sacramento.kilometersTo(sanfran)).toBeCloseTo(134.5, 0);
expect(sacramento.milesTo(sanfran)).toBeCloseTo(84.8, -1);
// Very local
expect(powell.kilometersTo(astore)).toBeCloseTo(0.1, 2);
// Far (for error tolerance's sake)
expect(sacramento.kilometersTo(vorkuta)).toBeCloseTo(8303.8, -3);
expect(sacramento.milesTo(vorkuta)).toBeCloseTo(5159.7, -3);
});
it('can test equality against another GeoPoint', () => {
let a = new ParseGeoPoint(40, 40);
expect(a.equals(a)).toBe(true);
let b = new ParseGeoPoint(40, 40);
expect(a.equals(b)).toBe(true);
expect(b.equals(a)).toBe(true);
b = new ParseGeoPoint(50, 40);
expect(a.equals(b)).toBe(false);
expect(b.equals(a)).toBe(false);
a = new ParseGeoPoint(40.001, 40.001);
b = new ParseGeoPoint(40.001, 40.001);
expect(a.equals(b)).toBe(true);
expect(b.equals(a)).toBe(true);
b = new ParseGeoPoint(40, 40);
expect(a.equals(b)).toBe(false);
expect(b.equals(a)).toBe(false);
a = new ParseGeoPoint(40, 50);
expect(a.equals(b)).toBe(false);
expect(b.equals(a)).toBe(false);
});
it('can get current location', async () => {
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',
});
});
});