Skip to content

Commit

Permalink
Migrate from callbacks to async-await
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed Dec 15, 2024
1 parent fa13fcb commit 6f7ccf5
Show file tree
Hide file tree
Showing 29 changed files with 471 additions and 671 deletions.
8 changes: 4 additions & 4 deletions spec/__snapshots__/nominatim.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ exports[`L.Control.Geocoder.Nominatim > geocodes Innsbruck 1`] = `
"state": "Tyrol",
},
"boundingbox": [
47.2583715,
47.2808566,
11.3811871,
11.418183,
"47.2583715",
"47.2808566",
"11.3811871",
"11.418183",
],
"class": "boundary",
"display_name": "Innsbruck, Tyrol, Austria",
Expand Down
10 changes: 4 additions & 6 deletions spec/arcgis.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ describe('L.Control.Geocoder.ArcGis', () => {
afterEach(() => vi.clearAllMocks());
it('geocodes Innsbruck', async () => {
const geocoder = new ArcGis();
const callback = vi.fn();
mockFetchRequest(
const result = await mockFetchRequest(
'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?token=&SingleLine=Innsbruck&outFields=Addr_Type&forStorage=false&maxLocations=10&f=json',
{
spatialReference: { wkid: 4326, latestWkid: 4326 },
Expand All @@ -26,17 +25,16 @@ describe('L.Control.Geocoder.ArcGis', () => {
}
]
},
() => geocoder.geocode('Innsbruck', callback)
() => geocoder.geocode('Innsbruck')
);

await vi.waitUntil(() => callback.mock.calls.length);
const feature = callback.mock.calls[0][0][0];
const feature = result[0];
expect(feature.name).toBe('Innsbruck, Innsbruck-Stadt, Tirol');
expect(feature.center).toStrictEqual({ lat: 47.26800000000003, lng: 11.391300000000058 });
expect(feature.bbox).toStrictEqual({
_northEast: { lat: 47.34400000000003, lng: 11.467300000000058 },
_southWest: { lat: 47.19200000000003, lng: 11.315300000000057 }
});
expect(callback.mock.calls).toMatchSnapshot();
expect([[result]]).toMatchSnapshot();
});
});
10 changes: 4 additions & 6 deletions spec/google.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ describe('L.Control.Geocoder.Google', () => {
afterEach(() => vi.clearAllMocks());
it('geocodes Innsbruck', async () => {
const geocoder = new Google({ apiKey: '0123xyz' });
const callback = vi.fn();
mockFetchRequest(
const result = await mockFetchRequest(
'https://maps.googleapis.com/maps/api/geocode/json?key=0123xyz&address=Innsbruck',
{
results: [
Expand Down Expand Up @@ -69,17 +68,16 @@ describe('L.Control.Geocoder.Google', () => {
],
status: 'OK'
},
() => geocoder.geocode('Innsbruck', callback)
() => geocoder.geocode('Innsbruck')
);

await vi.waitUntil(() => callback.mock.calls.length);
const feature: GeocodingResult = callback.mock.calls[0][0][0];
const feature: GeocodingResult = result[0];
expect(feature.name).toBe('Innsbruck, Austria');
expect(feature.center).toStrictEqual({ lat: 47.2692124, lng: 11.4041024 });
expect(feature.bbox).toStrictEqual({
_northEast: { lat: 47.3599301, lng: 11.45593 },
_southWest: { lat: 47.21098000000001, lng: 11.3016499 }
});
expect(callback.mock.calls).toMatchSnapshot();
expect([[result]]).toMatchSnapshot();
});
});
20 changes: 8 additions & 12 deletions spec/here.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ describe('L.Control.Geocoder.HERE', () => {
afterEach(() => vi.clearAllMocks());
it('geocodes Innsbruck', async () => {
const geocoder = new HERE({ app_id: 'xxx', app_code: 'yyy' });
const callback = vi.fn();
mockFetchRequest(
const result = await mockFetchRequest(
'https://geocoder.api.here.com/6.2/geocode.json?searchtext=Innsbruck&gen=9&app_id=xxx&app_code=yyy&jsonattributes=1&maxresults=5',
{
response: {
Expand Down Expand Up @@ -75,27 +74,25 @@ describe('L.Control.Geocoder.HERE', () => {
]
}
},
() => geocoder.geocode('Innsbruck', callback)
() => geocoder.geocode('Innsbruck')
);

await vi.waitUntil(() => callback.mock.calls.length);
const feature: GeocodingResult = callback.mock.calls[0][0][0];
const feature: GeocodingResult = result[0];
expect(feature.name).toBe('Innsbruck, Tirol, Österreich');
expect(feature.center).toStrictEqual({ lat: 47.268, lng: 11.3913 });
expect(feature.bbox).toStrictEqual({
_northEast: { lat: 47.35922, lng: 11.45587 },
_southWest: { lat: 47.21082, lng: 11.30194 }
});
expect(callback.mock.calls).toMatchSnapshot();
expect([[result]]).toMatchSnapshot();
});
});

describe('L.Control.Geocoder.HEREv2', () => {
it('geocodes Innsbruck', async () => {
const geocodingParams = { at: '50.62925,3.057256' };
const geocoder = new HEREv2({ apiKey: 'xxx', geocodingQueryParams: geocodingParams });
const callback = vi.fn();
mockFetchRequest(
const result = await mockFetchRequest(
'https://geocode.search.hereapi.com/v1/discover?q=Innsbruck&apiKey=xxx&limit=10&at=50.62925%2C3.057256',
{
items: [
Expand Down Expand Up @@ -166,15 +163,14 @@ describe('L.Control.Geocoder.HEREv2', () => {
}
]
},
() => geocoder.geocode('Innsbruck', callback)
() => geocoder.geocode('Innsbruck')
);

await vi.waitUntil(() => callback.mock.calls.length);
const feature: GeocodingResult = callback.mock.calls[0][0][0];
const feature: GeocodingResult = result[0];
expect(feature.name).toBe(
'Salumeria Italiana, 151 Richmond St, Boston, MA 02109, United States'
);
expect(feature.center).toStrictEqual({ lat: 42.36355, lng: -71.05439 });
expect(callback.mock.calls).toMatchSnapshot();
expect([[result]]).toMatchSnapshot();
});
});
30 changes: 14 additions & 16 deletions spec/latlng.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import * as L from 'leaflet';
import { LatLng } from '../src/geocoders/latlng';
import { IGeocoder } from '../src/geocoders/api';

describe('LatLng', () => {
afterEach(() =>vi.clearAllMocks())
afterEach(() => vi.clearAllMocks());
// test cases from https://github.com/openstreetmap/openstreetmap-website/blob/master/test/controllers/geocoder_controller_test.rb
let expected;
beforeEach(() => {
Expand All @@ -17,21 +18,20 @@ describe('LatLng', () => {
geocode('+50.06773, +14.37742');
});

it('does not geocode no-lat-lng', () => {
it('does not geocode no-lat-lng', async () => {
const geocoder = new LatLng();
const callback = vi.fn();
geocoder.geocode('no-lat-lng', callback);
expect(callback).toHaveBeenCalledTimes(0);
const result = await geocoder.geocode('no-lat-lng');
expect(result).toHaveLength(0);
});

it('passes unsupported queries to the next geocoder', () => {
it('passes unsupported queries to the next geocoder', async () => {
const xxx = [Symbol()] as any;
const next = {
geocode: (_query, cb) => cb('XXX')
};
geocode: _query => xxx
} as IGeocoder;
const geocoder = new LatLng({ next: next });
const callback = vi.fn();
geocoder.geocode('no-lat-lng', callback);
expect(callback).toHaveBeenCalledWith('XXX');
const result = await geocoder.geocode('no-lat-lng');
expect(result).toBe(xxx);
});

it('geocodes lat/lon pairs using N/E with degrees', () => {
Expand Down Expand Up @@ -139,12 +139,10 @@ describe('LatLng', () => {
geocode('50°4\'3.828"S 14°22\'38.712"W');
});

function geocode(query) {
async function geocode(query) {
const geocoder = new LatLng();
const callback = vi.fn();
geocoder.geocode(query, callback);
expect(callback).toBeCalledTimes(1);
const feature = callback.mock.calls[0][0][0];
const result = await geocoder.geocode(query);
const feature = result[0];
expect(feature.name).toBe(query);
expect(feature.center.lat).toBeCloseTo(expected.lat);
expect(feature.center.lng).toBeCloseTo(expected.lng);
Expand Down
10 changes: 4 additions & 6 deletions spec/mapbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ describe('L.Control.Geocoder.Mapbox', () => {
afterEach(() => vi.clearAllMocks());
it('geocodes Milwaukee Ave', async () => {
const geocoder = new Mapbox({ apiKey: '0123' });
const callback = vi.fn();
mockFetchRequest(
const result = await mockFetchRequest(
'https://api.mapbox.com/geocoding/v5/mapbox.places/Milwaukee%20Ave.json?access_token=0123',
{
type: 'FeatureCollection',
Expand Down Expand Up @@ -64,17 +63,16 @@ describe('L.Control.Geocoder.Mapbox', () => {
attribution:
'NOTICE: © 2018 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare.'
},
() => geocoder.geocode('Milwaukee Ave', callback)
() => geocoder.geocode('Milwaukee Ave')
);

await vi.waitUntil(() => callback.mock.calls.length);
const feature = callback.mock.calls[0][0][0];
const feature = result[0];
expect(feature.name).toBe('825 Milwaukee Ave, Deerfield, Illinois 60015, United States');
expect(feature.center).toStrictEqual({ lat: 42.166602, lng: -87.921434 });
expect(feature.bbox).toStrictEqual({
_northEast: { lat: 42.166602, lng: -87.921434 },
_southWest: { lat: 42.166602, lng: -87.921434 }
});
expect(callback.mock.calls).toMatchSnapshot();
expect([[result]]).toMatchSnapshot();
});
});
9 changes: 7 additions & 2 deletions spec/mockFetchRequest.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { expect, vi } from 'vitest';

export function mockFetchRequest<T>(url: string, response: T, trigger: () => void) {
export async function mockFetchRequest<T>(
url: string,
response: unknown,
trigger: () => T
): Promise<T> {
const headers = { Accept: 'application/json' };
global.fetch = vi.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(response)
})
) as any;
trigger();
const result = await trigger();
expect(fetch).toBeCalledTimes(1);
expect(fetch).toBeCalledWith(url, { headers });
return result;
}
22 changes: 8 additions & 14 deletions spec/nominatim.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ describe('L.Control.Geocoder.Nominatim', () => {
const geocoder = new Nominatim();

it('geocodes Innsbruck', async () => {
const callback = vi.fn();

mockFetchRequest(
const result = await mockFetchRequest(
'https://nominatim.openstreetmap.org/search?q=innsbruck&limit=5&format=json&addressdetails=1',
[
{
Expand All @@ -35,11 +33,10 @@ describe('L.Control.Geocoder.Nominatim', () => {
}
}
],
() => geocoder.geocode('innsbruck', callback)
() => geocoder.geocode('innsbruck')
);

await vi.waitUntil(() => callback.mock.calls.length);
const feature = callback.mock.calls[0][0][0];
const feature = result[0];
expect(feature.name).toBe('Innsbruck, Tyrol, Austria');
expect(feature.html).toBe(
'<span class=""> Innsbruck </span><br/><span class="leaflet-control-geocoder-address-context">Tyrol Austria</span>'
Expand All @@ -52,13 +49,11 @@ describe('L.Control.Geocoder.Nominatim', () => {
country: 'Austria',
country_code: 'at'
});
expect(callback.mock.calls).toMatchSnapshot();
expect([[result]]).toMatchSnapshot();
});

it('reverse geocodes 47.3/11.3', async () => {
const callback = vi.fn();

mockFetchRequest(
const result = await mockFetchRequest(
'https://nominatim.openstreetmap.org/reverse?lat=47.3&lon=11.3&zoom=9&addressdetails=1&format=json',
{
place_id: 197718025,
Expand All @@ -76,11 +71,10 @@ describe('L.Control.Geocoder.Nominatim', () => {
},
boundingbox: ['46.9624854', '47.4499229', '10.9896868', '11.7051742']
},
() => geocoder.reverse({ lat: 47.3, lng: 11.3 }, 131000, callback)
() => geocoder.reverse({ lat: 47.3, lng: 11.3 }, 131000)
);

await vi.waitUntil(() => callback.mock.calls.length);
const feature = callback.mock.calls[0][0][0];
const feature = result[0];
expect(feature.name).toBe('Innsbruck-Land, Tyrol, Austria');
expect(feature.html).toBe('<span class="">Tyrol Austria</span>');
expect(feature.properties.address).toStrictEqual({
Expand All @@ -89,6 +83,6 @@ describe('L.Control.Geocoder.Nominatim', () => {
country: 'Austria',
country_code: 'at'
});
expect(callback.mock.calls).toMatchSnapshot();
expect([[result]]).toMatchSnapshot();
});
});
14 changes: 6 additions & 8 deletions spec/open-location-code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ import { OpenLocationCode as Geocoder } from '../src/geocoders/open-location-cod
describe('L.Control.Geocoder.OpenLocationCode', () => {
const geocoder = new Geocoder({ OpenLocationCode: OpenLocationCode });

it('geocodes 9C3XGW4F+5V', () => {
const callback = vi.fn();
geocoder.geocode('9C3XGW4F+5V', callback);
const feature = callback.mock.calls[0][0][0];
it('geocodes 9C3XGW4F+5V', async () => {
const result = await geocoder.geocode('9C3XGW4F+5V');
const feature = result[0];
expect(feature.name).toBe('9C3XGW4F+5V');
expect(feature.center.lat).toBeCloseTo(51.505437499999985);
expect(feature.center.lng).toBeCloseTo(-0.07531249999998124);
});

it('reverse geocodes 47.3/11.3', () => {
const callback = vi.fn();
geocoder.reverse({ lat: 47.3, lng: 11.3 }, 131000, callback);
const feature = callback.mock.calls[0][0][0];
it('reverse geocodes 47.3/11.3', async () => {
const result = await geocoder.reverse({ lat: 47.3, lng: 11.3 }, 131000);
const feature = result[0];
expect(feature.name).toBe('8FVH8822+22');
});
});
10 changes: 4 additions & 6 deletions spec/pelias.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ describe('L.Control.Geocoder.Openrouteservice', () => {
const geocoder = new Openrouteservice({ apiKey: '0123' });

it('geocodes Innsbruck', async () => {
const callback = vi.fn();
mockFetchRequest(
const result = await mockFetchRequest(
'https://api.openrouteservice.org/geocode/search?api_key=0123&text=innsbruck',
{
geocoding: {
Expand Down Expand Up @@ -47,17 +46,16 @@ describe('L.Control.Geocoder.Openrouteservice', () => {
],
bbox: [10.9896885523, 46.9624806033, 11.7051690163, 47.4499185397]
},
() => geocoder.geocode('innsbruck', callback)
() => geocoder.geocode('innsbruck')
);

await vi.waitUntil(() => callback.mock.calls.length);
const feature = callback.mock.calls[0][0][0];
const feature = result[0];
expect(feature.name).toBe('Innsbruck, Austria');
expect(feature.center).toStrictEqual({ lat: 47.272308, lng: 11.407851 });
expect(feature.bbox).toStrictEqual({
_southWest: { lat: 47.2470573997, lng: 11.3218091258 },
_northEast: { lat: 47.29398, lng: 11.452584553 }
});
expect(callback.mock.calls).toMatchSnapshot();
expect([[result]]).toMatchSnapshot();
});
});
Loading

0 comments on commit 6f7ccf5

Please sign in to comment.