-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.test.ts
200 lines (179 loc) · 5.38 KB
/
index.test.ts
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
/**
* @jest-environment jsdom
*/
/* globals describe, expect, jest, it */
/* eslint-disable @typescript-eslint/ban-ts-comment */
import * as ll from './index'
const lat = 38.13234
const lon = 70.01232
const Z = 9 // Zoom level to use
const pixel = { x: 91026.70779, y: 50497.026 }
const lonlat = { lat, lon }
const point = { x: lon, y: lat }
const coordinates = [lon, lat]
const str = `${lon},${lat}`
const strLatFirst = `${lat},${lon}`
const latlng = { lat, lng: lon }
describe('lonlat', () => {
describe('print', () => {
it('should print basic input', () => {
expect(ll.print(str)).toEqual('70.01232, 38.13234')
})
})
describe('isEqual', () => {
it('should not be equal for different coordinates', () => {
expect(ll.isEqual('123.456,78.9', '123.4567,78.9')).toEqual(false)
})
it('should be equal for different coordinates with allowable epsilon', () => {
expect(ll.isEqual('123.456,78.9', '123.4567,78.9', 0.001)).toEqual(true)
})
})
describe('toLeaflet', () => {
it('should create leaflet latLng', () => {
window.L = {
// Issue here is caused not by lonlat but by Leaflet
// @ts-ignore
latLng: jest.fn((lat, lng) => {
return { leaflet_lat: lat, leaflet_lng: lng }
})
}
expect(ll.toLeaflet('0,0')).toEqual({ leaflet_lat: 0, leaflet_lng: 0 })
window.L = undefined
})
})
describe('normalization', () => {
const testCases = [
{
calculated: ll.normalize(lonlat),
description: 'Object with lon and lat keys'
},
{
calculated: ll.normalize(point),
description: 'Object with x and y keys'
},
{
calculated: ll.normalize(coordinates),
description: 'Array of lon and lat'
},
{
calculated: ll.normalize(str),
description: 'String with comma separating lon and lat'
}
]
testCases.forEach((test) => {
it(`should normalize from ${test.description}`, () => {
expect(test.calculated).toEqual(lonlat)
})
})
})
describe('translations', () => {
const testCases = [
{
expected: coordinates,
method: 'toCoordinates'
},
{
expected: point,
method: 'toPoint'
},
{
expected: str,
method: 'toString'
},
{
expected: str,
method: 'toLonFirstString'
},
{
expected: strLatFirst,
method: 'toLatFirstString'
}
]
testCases.forEach((test) => {
it(`should translate using ${test.method}`, () => {
expect(ll[test.method](lonlat)).toEqual(test.expected)
})
})
})
describe('known type parsing', () => {
const testCases = [
{
calculated: ll.fromLatlng(latlng),
description: 'Object with lng and lat keys'
},
{
calculated: ll.fromCoordinates(coordinates),
description: 'Array of lon and lat (fromCoordinates)'
},
{
calculated: ll.fromGeoJSON(coordinates),
description: 'Array of lon and lat (fromGeoJSON)'
},
{
calculated: ll.fromPoint(point),
description: 'Object with x and y keys'
},
{
calculated: ll.fromString(str),
description:
'String with comma separating lon and lat, respectively (fromString)'
},
{
calculated: ll.fromLonFirstString(str),
description:
'String with comma separating lon and lat, respectively (fromLonFirstString)'
},
{
calculated: ll.fromLatFirstString(strLatFirst),
description: 'String with comma separating lat and lon, respectively'
}
]
testCases.forEach((test) => {
it(`should specifically parse from ${test.description}`, () => {
expect(test.calculated).toEqual(lonlat)
})
})
})
describe('errors', () => {
it('toLeaflet should throw error if leaflet is not loaded', () => {
expect(() => ll.toLeaflet('0,0')).toThrowErrorMatchingSnapshot()
})
describe('invalid coordinates', () => {
const badCoords = [
'-999,999',
'0,999',
{},
undefined,
{ latitude: 1234, lng: 1 }
]
badCoords.forEach((data) => {
it(`should throw error when parsing: ${JSON.stringify(data)}`, () => {
// This exception is prevented by Typescript! So need to disable it to allow the error to throw
// @ts-ignore
expect(() => ll.normalize(data)).toThrowErrorMatchingSnapshot()
})
})
})
})
describe('pixel', () => {
it('can convert to web mercator pixel coordinates', () => {
const p = ll.toPixel({ lat, lon }, Z)
expect(Math.round(p.x)).toBe(Math.round(pixel.x))
expect(Math.round(p.y)).toBe(Math.round(pixel.y))
})
it('can convert from web mercator pixel coordinates', () => {
const l = ll.fromPixel(pixel, Z)
expect(Math.round(l.lat)).toBe(Math.round(lat))
expect(Math.round(l.lon)).toBe(Math.round(lon))
})
it('should throw an error if converting a latitude > MAX_LAT', () => {
expect(() => ll.toPixel({ lat: 86, lon: 4 }, Z)).toThrow()
})
})
describe('issues', () => {
it('#3 - Does not parse coordinates with 0 for lat or lon', () => {
expect(ll.normalize({ lat: 0, lng: 0 })).toEqual({ lat: 0, lon: 0 })
expect(ll.normalize({ x: 0, y: 0 })).toEqual({ lat: 0, lon: 0 })
})
})
})