Skip to content

Commit fe751be

Browse files
committed
Migrate from XMLHttpRequest to fetch API
1 parent ce88eab commit fe751be

File tree

11 files changed

+69
-82
lines changed

11 files changed

+69
-82
lines changed

spec/arcgis.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { describe, expect, it, vi } from 'vitest';
2-
import { testXMLHttpRequest } from './mockXMLHttpRequest';
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
2+
import { mockFetchRequest } from './mockFetchRequest';
33
import { ArcGis } from '../src/geocoders/arcgis';
44

55
describe('L.Control.Geocoder.ArcGis', () => {
6-
it('geocodes Innsbruck', () => {
6+
afterEach(() => vi.clearAllMocks());
7+
it('geocodes Innsbruck', async () => {
78
const geocoder = new ArcGis();
89
const callback = vi.fn();
9-
testXMLHttpRequest(
10+
mockFetchRequest(
1011
'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?token=&SingleLine=Innsbruck&outFields=Addr_Type&forStorage=false&maxLocations=10&f=json',
1112
{
1213
spatialReference: { wkid: 4326, latestWkid: 4326 },
@@ -28,6 +29,7 @@ describe('L.Control.Geocoder.ArcGis', () => {
2829
() => geocoder.geocode('Innsbruck', callback)
2930
);
3031

32+
await vi.waitUntil(() => callback.mock.calls.length);
3133
const feature = callback.mock.calls[0][0][0];
3234
expect(feature.name).toBe('Innsbruck, Innsbruck-Stadt, Tirol');
3335
expect(feature.center).toStrictEqual({ lat: 47.26800000000003, lng: 11.391300000000058 });

spec/google.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { describe, expect, it, vi } from 'vitest';
2-
import { testXMLHttpRequest } from './mockXMLHttpRequest';
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
2+
import { mockFetchRequest } from './mockFetchRequest';
33
import { Google } from '../src/geocoders/google';
44
import { GeocodingResult } from '../src/geocoders/api';
55

66
describe('L.Control.Geocoder.Google', () => {
7-
it('geocodes Innsbruck', () => {
7+
afterEach(() => vi.clearAllMocks());
8+
it('geocodes Innsbruck', async () => {
89
const geocoder = new Google({ apiKey: '0123xyz' });
910
const callback = vi.fn();
10-
testXMLHttpRequest(
11+
mockFetchRequest(
1112
'https://maps.googleapis.com/maps/api/geocode/json?key=0123xyz&address=Innsbruck',
1213
{
1314
results: [
@@ -71,6 +72,7 @@ describe('L.Control.Geocoder.Google', () => {
7172
() => geocoder.geocode('Innsbruck', callback)
7273
);
7374

75+
await vi.waitUntil(() => callback.mock.calls.length);
7476
const feature: GeocodingResult = callback.mock.calls[0][0][0];
7577
expect(feature.name).toBe('Innsbruck, Austria');
7678
expect(feature.center).toStrictEqual({ lat: 47.2692124, lng: 11.4041024 });

spec/here.spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { describe, expect, it, vi } from 'vitest';
2-
import { testXMLHttpRequest } from './mockXMLHttpRequest';
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
2+
import { mockFetchRequest } from './mockFetchRequest';
33
import { HERE } from '../src/geocoders/here';
44
import { HEREv2 } from '../src/geocoders/here';
55
import { GeocodingResult } from '../src/geocoders/api';
66

77
describe('L.Control.Geocoder.HERE', () => {
8-
it('geocodes Innsbruck', () => {
8+
afterEach(() => vi.clearAllMocks());
9+
it('geocodes Innsbruck', async () => {
910
const geocoder = new HERE({ app_id: 'xxx', app_code: 'yyy' });
1011
const callback = vi.fn();
11-
testXMLHttpRequest(
12+
mockFetchRequest(
1213
'https://geocoder.api.here.com/6.2/geocode.json?searchtext=Innsbruck&gen=9&app_id=xxx&app_code=yyy&jsonattributes=1&maxresults=5',
1314
{
1415
response: {
@@ -77,6 +78,7 @@ describe('L.Control.Geocoder.HERE', () => {
7778
() => geocoder.geocode('Innsbruck', callback)
7879
);
7980

81+
await vi.waitUntil(() => callback.mock.calls.length);
8082
const feature: GeocodingResult = callback.mock.calls[0][0][0];
8183
expect(feature.name).toBe('Innsbruck, Tirol, Österreich');
8284
expect(feature.center).toStrictEqual({ lat: 47.268, lng: 11.3913 });
@@ -89,11 +91,11 @@ describe('L.Control.Geocoder.HERE', () => {
8991
});
9092

9193
describe('L.Control.Geocoder.HEREv2', () => {
92-
it('geocodes Innsbruck', () => {
94+
it('geocodes Innsbruck', async () => {
9395
const geocodingParams = { at: '50.62925,3.057256' };
9496
const geocoder = new HEREv2({ apiKey: 'xxx', geocodingQueryParams: geocodingParams });
9597
const callback = vi.fn();
96-
testXMLHttpRequest(
98+
mockFetchRequest(
9799
'https://geocode.search.hereapi.com/v1/discover?q=Innsbruck&apiKey=xxx&limit=10&at=50.62925%2C3.057256',
98100
{
99101
items: [
@@ -167,6 +169,7 @@ describe('L.Control.Geocoder.HEREv2', () => {
167169
() => geocoder.geocode('Innsbruck', callback)
168170
);
169171

172+
await vi.waitUntil(() => callback.mock.calls.length);
170173
const feature: GeocodingResult = callback.mock.calls[0][0][0];
171174
expect(feature.name).toBe(
172175
'Salumeria Italiana, 151 Richmond St, Boston, MA 02109, United States'

spec/latlng.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { beforeEach, describe, expect, it, vi } from 'vitest';
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
22
import * as L from 'leaflet';
33
import { LatLng } from '../src/geocoders/latlng';
44

55
describe('LatLng', () => {
6+
afterEach(() =>vi.clearAllMocks())
67
// test cases from https://github.com/openstreetmap/openstreetmap-website/blob/master/test/controllers/geocoder_controller_test.rb
78
let expected;
89
beforeEach(() => {

spec/mapbox.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { describe, expect, it, vi } from 'vitest';
2-
import { testXMLHttpRequest } from './mockXMLHttpRequest';
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
2+
import { mockFetchRequest } from './mockFetchRequest';
33
import { Mapbox } from '../src/geocoders/mapbox';
44

55
describe('L.Control.Geocoder.Mapbox', () => {
6-
it('geocodes Milwaukee Ave', () => {
6+
afterEach(() => vi.clearAllMocks());
7+
it('geocodes Milwaukee Ave', async () => {
78
const geocoder = new Mapbox({ apiKey: '0123' });
89
const callback = vi.fn();
9-
testXMLHttpRequest(
10+
mockFetchRequest(
1011
'https://api.mapbox.com/geocoding/v5/mapbox.places/Milwaukee%20Ave.json?access_token=0123',
1112
{
1213
type: 'FeatureCollection',
@@ -66,6 +67,7 @@ describe('L.Control.Geocoder.Mapbox', () => {
6667
() => geocoder.geocode('Milwaukee Ave', callback)
6768
);
6869

70+
await vi.waitUntil(() => callback.mock.calls.length);
6971
const feature = callback.mock.calls[0][0][0];
7072
expect(feature.name).toBe('825 Milwaukee Ave, Deerfield, Illinois 60015, United States');
7173
expect(feature.center).toStrictEqual({ lat: 42.166602, lng: -87.921434 });

spec/mockFetchRequest.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { expect, vi } from 'vitest';
2+
3+
export function mockFetchRequest<T>(url: string, response: T, trigger: () => void) {
4+
const headers = { Accept: 'application/json' };
5+
global.fetch = vi.fn(() =>
6+
Promise.resolve({
7+
ok: true,
8+
json: () => Promise.resolve(response)
9+
})
10+
) as any;
11+
trigger();
12+
expect(fetch).toBeCalledTimes(1);
13+
expect(fetch).toBeCalledWith(url, { headers });
14+
}

spec/mockXMLHttpRequest.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

spec/nominatim.spec.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { describe, expect, it, vi } from 'vitest';
2-
import { testXMLHttpRequest } from './mockXMLHttpRequest';
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
2+
import { mockFetchRequest } from './mockFetchRequest';
33
import { Nominatim } from '../src/geocoders/nominatim';
44

55
describe('L.Control.Geocoder.Nominatim', () => {
6+
afterEach(() => vi.clearAllMocks());
67
const geocoder = new Nominatim();
78

8-
it('geocodes Innsbruck', () => {
9+
it('geocodes Innsbruck', async () => {
910
const callback = vi.fn();
1011

11-
testXMLHttpRequest(
12+
mockFetchRequest(
1213
'https://nominatim.openstreetmap.org/search?q=innsbruck&limit=5&format=json&addressdetails=1',
1314
[
1415
{
@@ -23,8 +24,7 @@ describe('L.Control.Geocoder.Nominatim', () => {
2324
class: 'boundary',
2425
type: 'administrative',
2526
importance: 0.763909048330467,
26-
icon:
27-
'https://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png',
27+
icon: 'https://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png',
2828
address: {
2929
city_district: 'Innsbruck',
3030
city: 'Innsbruck',
@@ -38,6 +38,7 @@ describe('L.Control.Geocoder.Nominatim', () => {
3838
() => geocoder.geocode('innsbruck', callback)
3939
);
4040

41+
await vi.waitUntil(() => callback.mock.calls.length);
4142
const feature = callback.mock.calls[0][0][0];
4243
expect(feature.name).toBe('Innsbruck, Tyrol, Austria');
4344
expect(feature.html).toBe(
@@ -54,10 +55,10 @@ describe('L.Control.Geocoder.Nominatim', () => {
5455
expect(callback.mock.calls).toMatchSnapshot();
5556
});
5657

57-
it('reverse geocodes 47.3/11.3', () => {
58+
it('reverse geocodes 47.3/11.3', async () => {
5859
const callback = vi.fn();
5960

60-
testXMLHttpRequest(
61+
mockFetchRequest(
6162
'https://nominatim.openstreetmap.org/reverse?lat=47.3&lon=11.3&zoom=9&addressdetails=1&format=json',
6263
{
6364
place_id: 197718025,
@@ -78,6 +79,7 @@ describe('L.Control.Geocoder.Nominatim', () => {
7879
() => geocoder.reverse({ lat: 47.3, lng: 11.3 }, 131000, callback)
7980
);
8081

82+
await vi.waitUntil(() => callback.mock.calls.length);
8183
const feature = callback.mock.calls[0][0][0];
8284
expect(feature.name).toBe('Innsbruck-Land, Tyrol, Austria');
8385
expect(feature.html).toBe('<span class="">Tyrol Austria</span>');

spec/pelias.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { describe, expect, it, vi } from 'vitest';
2-
import { testXMLHttpRequest } from './mockXMLHttpRequest';
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
2+
import { mockFetchRequest } from './mockFetchRequest';
33
import { Openrouteservice } from '../src/geocoders/pelias';
44

55
describe('L.Control.Geocoder.Openrouteservice', () => {
6+
afterEach(() => vi.clearAllMocks());
67
const geocoder = new Openrouteservice({ apiKey: '0123' });
78

8-
it('geocodes Innsbruck', () => {
9+
it('geocodes Innsbruck', async () => {
910
const callback = vi.fn();
10-
testXMLHttpRequest(
11+
mockFetchRequest(
1112
'https://api.openrouteservice.org/geocode/search?api_key=0123&text=innsbruck',
1213
{
1314
geocoding: {
@@ -49,6 +50,7 @@ describe('L.Control.Geocoder.Openrouteservice', () => {
4950
() => geocoder.geocode('innsbruck', callback)
5051
);
5152

53+
await vi.waitUntil(() => callback.mock.calls.length);
5254
const feature = callback.mock.calls[0][0][0];
5355
expect(feature.name).toBe('Innsbruck, Austria');
5456
expect(feature.center).toStrictEqual({ lat: 47.272308, lng: 11.407851 });

spec/photon.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { describe, expect, it, vi } from 'vitest';
2-
import { testXMLHttpRequest } from './mockXMLHttpRequest';
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
2+
import { mockFetchRequest } from './mockFetchRequest';
33
import { Photon } from '../src/geocoders/photon';
44
import { GeocodingResult } from '../src/geocoders/api';
55

66
describe('L.Control.Geocoder.Photon', () => {
7-
it('geocodes Innsbruck', () => {
7+
afterEach(() => vi.clearAllMocks());
8+
it('geocodes Innsbruck', async () => {
89
const geocoder = new Photon();
910
const callback = vi.fn();
10-
testXMLHttpRequest(
11+
mockFetchRequest(
1112
'https://photon.komoot.io/api/?q=Innsbruck',
1213
{
1314
features: [
@@ -54,6 +55,7 @@ describe('L.Control.Geocoder.Photon', () => {
5455
() => geocoder.geocode('Innsbruck', callback)
5556
);
5657

58+
await vi.waitUntil(() => callback.mock.calls.length);
5759
const feature: GeocodingResult = callback.mock.calls[0][0][0];
5860
expect(feature.name).toBe('Innsbruck, Innsbruck, Tyrol, Austria');
5961
expect(feature.center).toStrictEqual({ lat: 47.2654296, lng: 11.3927685 });

0 commit comments

Comments
 (0)