forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParsePolygon-test.js
97 lines (81 loc) · 2.31 KB
/
ParsePolygon-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
jest.autoMockOff();
const ParseGeoPoint = require('../ParseGeoPoint').default;
const ParsePolygon = require('../ParsePolygon').default;
const points = [
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
];
describe('Polygon', () => {
it('can initialize with points', () => {
const polygon = new ParsePolygon(points);
expect(polygon.coordinates).toEqual(points);
});
it('can initialize with geopoints', () => {
const geopoints = [
new ParseGeoPoint(0, 0),
new ParseGeoPoint(0, 1),
new ParseGeoPoint(1, 1),
new ParseGeoPoint(1, 0),
new ParseGeoPoint(0, 0),
];
const polygon = new ParsePolygon(geopoints);
expect(polygon.coordinates).toEqual(points);
});
it('can set points', () => {
const newPoints = [
[0, 0],
[0, 10],
[10, 10],
[10, 0],
[0, 0],
];
const polygon = new ParsePolygon(points);
expect(polygon.coordinates).toEqual(points);
polygon.coordinates = newPoints;
expect(polygon.coordinates).toEqual(newPoints);
});
it('toJSON', () => {
const polygon = new ParsePolygon(points);
expect(polygon.toJSON()).toEqual({
__type: 'Polygon',
coordinates: points,
});
});
it('equals', () => {
const polygon1 = new ParsePolygon(points);
const polygon2 = new ParsePolygon(points);
const geopoint = new ParseGeoPoint(0, 0);
expect(polygon1.equals(polygon2)).toBe(true);
expect(polygon1.equals(geopoint)).toBe(false);
const newPoints = [
[0, 0],
[0, 10],
[10, 10],
[10, 0],
[0, 0],
];
polygon1.coordinates = newPoints;
expect(polygon1.equals(polygon2)).toBe(false);
});
it('containsPoint', () => {
const polygon = new ParsePolygon(points);
const outside = new ParseGeoPoint(10, 10);
const inside = new ParseGeoPoint(0.5, 0.5);
expect(polygon.containsPoint(inside)).toBe(true);
expect(polygon.containsPoint(outside)).toBe(false);
});
it('throws error on invalid input', () => {
expect(() => {
new ParsePolygon();
}).toThrow('Coordinates must be an Array');
expect(() => {
new ParsePolygon([]);
}).toThrow('Polygon must have at least 3 GeoPoints or Points');
expect(() => {
new ParsePolygon([1, 2, 3]);
}).toThrow('Coordinates must be an Array of GeoPoints or Points');
});
});