Skip to content

Commit 86c99aa

Browse files
authored
test: add coverage for PR #385 (nominatim endpoint & geocoder) (#443)
1 parent c7b5379 commit 86c99aa

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

test/geocoder.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
describe('geocoder.coordPreserving', () => {
4+
beforeEach(() => {
5+
jest.resetModules();
6+
});
7+
8+
test('passes serviceUrl to nominatim and preserves exact coordinates for coordinate input', async () => {
9+
const reverseMock = jest.fn(() => Promise.resolve([{ name: 'nom-res', center: { lat: 99, lng: 88 } }]));
10+
const geocodeMock = jest.fn(() => Promise.resolve([]));
11+
const nominatimFactory = jest.fn(() => ({ reverse: reverseMock, geocode: geocodeMock }));
12+
13+
jest.doMock('leaflet', () => ({
14+
Control: { Geocoder: { nominatim: nominatimFactory } },
15+
CRS: { EPSG3857: { scale: () => 1 } },
16+
latLng: (lat, lng) => ({ lat: +lat, lng: +lng, toBounds: () => ({}) }),
17+
extend: Object.assign
18+
}));
19+
20+
const geocoder = require('../src/geocoder');
21+
const L = require('leaflet');
22+
23+
const g = geocoder.coordPreserving('https://nominatim.example/');
24+
expect(L.Control.Geocoder.nominatim).toHaveBeenCalledWith({ serviceUrl: 'https://nominatim.example/' });
25+
26+
const results = await g.geocode('34.129382,-118.141254');
27+
expect(results).toHaveLength(1);
28+
expect(results[0].center.lat).toBeCloseTo(34.129382);
29+
expect(results[0].center.lng).toBeCloseTo(-118.141254);
30+
expect(reverseMock).toHaveBeenCalled();
31+
});
32+
});

test/replace_nominatim.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const { applyReplacements } = require('../scripts/replace');
4+
5+
describe('applyReplacements — nominatim endpoint override', () => {
6+
test('replaces Nominatim endpoint when NOMINATIM_ENDPOINT env var is set', () => {
7+
const content = "nominatim: { path: 'https://nominatim.openstreetmap.org/' }";
8+
const result = applyReplacements(content, { NOMINATIM_ENDPOINT: 'https://example.com/nominatim/' });
9+
expect(result).toContain('https://example.com/nominatim/');
10+
expect(result).not.toContain('nominatim.openstreetmap.org');
11+
});
12+
13+
test('leaves content unchanged when NOMINATIM_ENDPOINT not provided', () => {
14+
const content = "nominatim: { path: 'https://nominatim.openstreetmap.org/' }";
15+
const result = applyReplacements(content, {});
16+
expect(result).toContain('nominatim.openstreetmap.org');
17+
});
18+
});

0 commit comments

Comments
 (0)