Skip to content

Commit 744f03e

Browse files
Add gridref helper tests
1 parent 9395a5a commit 744f03e

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

src/server/plugins/engine/components/helpers/geospatial.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('Geospatial validation helpers', () => {
99
expect(result.value).toBeDefined()
1010
expect(result.value).toHaveLength(4)
1111
})
12+
1213
test('it should not have errors for valid geojson string', () => {
1314
const result = geospatialSchema.validate(JSON.stringify(validState))
1415

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { getGridRef } from '~/src/server/plugins/engine/components/helpers/gridref.js'
2+
3+
describe('Gridref helpers', () => {
4+
test('it should return gridref for a point feature', () => {
5+
const result = getGridRef({
6+
id: 'id',
7+
type: 'Feature',
8+
geometry: {
9+
type: 'Point',
10+
coordinates: [-0.1356213, 51.5121161]
11+
},
12+
properties: {
13+
description: 'New point'
14+
}
15+
})
16+
17+
expect(result).toBe('TQ 29472 80890')
18+
})
19+
20+
test('it should return gridref for a linestring feature', () => {
21+
const result = getGridRef({
22+
id: 'id',
23+
type: 'Feature',
24+
geometry: {
25+
type: 'LineString',
26+
coordinates: [
27+
[-0.1356213, 51.5121161],
28+
[-0.0578579, 51.5182996]
29+
]
30+
},
31+
properties: {
32+
description: 'New line'
33+
}
34+
})
35+
36+
expect(result).toBe('TQ 29472 80890 to TQ 34850 81718')
37+
})
38+
39+
test('it should return gridref for a polygon feature', () => {
40+
const result = getGridRef({
41+
id: 'id',
42+
type: 'Feature',
43+
geometry: {
44+
type: 'Polygon',
45+
coordinates: [
46+
[
47+
[-0.1356213, 51.5121161],
48+
[-0.0578579, 51.5182996],
49+
[-0.1114282, 51.5602178],
50+
[-0.1356213, 51.5121161]
51+
]
52+
]
53+
},
54+
properties: {
55+
description: 'New polygon'
56+
}
57+
})
58+
59+
expect(result).toBe('TQ 31778 82963')
60+
})
61+
})

src/server/plugins/engine/components/helpers/gridref.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function getGridRef(feature: Feature) {
1010
const [long, lat] = feature.geometry.coordinates
1111
const point = new LatLon(lat, long)
1212

13-
return point.toOsGrid()
13+
return point.toOsGrid().toString()
1414
}
1515

1616
if (feature.geometry.type === 'LineString') {
@@ -21,13 +21,13 @@ export function getGridRef(feature: Feature) {
2121
const [long2, lat2] = feature.geometry.coordinates[numLinePoints - 1]
2222
const point2 = new LatLon(lat2, long2)
2323

24-
return `${point1.toOsGrid()} to ${point2.toOsGrid()}`
24+
return `${point1.toOsGrid().toString()} to ${point2.toOsGrid().toString()}`
2525
}
2626

2727
const shape = polygon(feature.geometry.coordinates)
2828
const centre = centroid(shape)
2929
const [long, lat] = centre.geometry.coordinates
3030
const point = new LatLon(lat, long)
3131

32-
return point.toOsGrid()
32+
return point.toOsGrid().toString()
3333
}

0 commit comments

Comments
 (0)