Skip to content

Commit fef1ab8

Browse files
committed
Nominatim: test that identical queries are cached
Geocode the same query twice and assert a single request reaches the network. Verified to fail without the cache: bypassing the lookup makes it fetch twice. Uses a fresh geocoder, as the cache is per instance.
1 parent a481158 commit fef1ab8

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

spec/nominatim.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,36 @@ describe('L.Control.Geocoder.Nominatim', () => {
1414
);
1515
});
1616

17+
it('sends a single request for repeated identical queries', async () => {
18+
// a fresh geocoder, as the cache is per instance
19+
const geocoder = new Nominatim();
20+
// mockFetchRequest asserts that fetch is called exactly once
21+
const [first, second] = await mockFetchRequest(
22+
'https://nominatim.openstreetmap.org/search?q=graz&limit=5&format=json&addressdetails=1',
23+
[
24+
{
25+
place_id: 258691884,
26+
licence:
27+
'Data © OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright',
28+
osm_type: 'relation',
29+
osm_id: 45341,
30+
boundingbox: ['46.9987601', '47.1394862', '15.349939', '15.5470191'],
31+
lat: '47.0708678',
32+
lon: '15.4382786',
33+
display_name: 'Graz, Styria, Austria',
34+
address: {
35+
city: 'Graz',
36+
state: 'Styria',
37+
country: 'Austria',
38+
country_code: 'at'
39+
}
40+
}
41+
] satisfies NominatimResponse,
42+
async () => [await geocoder.geocode('graz'), await geocoder.geocode('graz')]
43+
);
44+
expect(second).toEqual(first);
45+
});
46+
1747
it('geocodes Innsbruck', async () => {
1848
const result = await mockFetchRequest(
1949
'https://nominatim.openstreetmap.org/search?q=innsbruck&limit=5&format=json&addressdetails=1',

0 commit comments

Comments
 (0)