Skip to content

Commit ea13f58

Browse files
committed
test: nominatim api call mocking
1 parent de52d8c commit ea13f58

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

src/map/utils/read.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { parsePB, tileTypes, type TileType } from './parsePB';
22
import { parseDMS } from './parseDMS';
33
import { getMapZoom } from './zoom';
44

5-
const nominatimQ: string = 'https://nominatim.openstreetmap.org/search/?limit=1&format=json&q=';
5+
export const nominatimQ: string = 'https://nominatim.openstreetmap.org/search/?limit=1&format=json&q=';
66
const cidMatch: RegExp = /^0x[\da-f]+:0x[\da-f]+$/i;
77
const dmsMatch: RegExp = /^\d{1,2}°\d{1,2}'\d{1,2}\.\d"(N|S) \d{1,2}°\d{1,2}'\d{1,2}\.\d"(E|W)$/i;
88

test/map/utils/read.test.ts

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import { describe, it } from 'vitest';
2-
import { readPB, readQ } from '../../../src/map/utils/read';
1+
import { describe, it, vi } from 'vitest';
2+
import { readPB, readQ, nominatimQ } from '../../../src/map/utils/read';
3+
4+
const input = 'test position';
5+
const result = [{ lat: '1.1', lon: '1.1' }];
6+
7+
function mockNominatimResponse(data: { lat: string; lon: string }[], status: boolean) {
8+
return { ok: status, json: () => new Promise((resolve) => resolve(data)) };
9+
}
310

411
describe.concurrent('read pb', () => {
512
it('read example', async ({ expect }) => {
@@ -18,9 +25,9 @@ describe.concurrent('read pb', () => {
1825
});
1926
});
2027

21-
it('pb markers', async ({ expect }) => {
28+
it('pb base64 marker', async ({ expect }) => {
2229
const res = await readPB(
23-
'!1m18!1m12!1m3!1d1.1!2d1.1!3d1.1!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0:0x0!2zMTDCsDYwJzM2LjAiTiAxMMKwNjAnMzYuMCJF!5e0!3m2!1sde!2sde!4v1557583694739!5m2!1sde!2sde'
30+
'!1m17!1m12!1m3!1d1.1!2d1.1!3d1.1!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m2!1m1!1zMTDCsDYwJzM2LjAiTiAxMMKwNjAnMzYuMCJF!5e0!3m2!1sde!2sde!4v1557583694739!5m2!1sde!2sde'
2431
);
2532

2633
expect(res).toStrictEqual({
@@ -39,10 +46,68 @@ describe.concurrent('read pb', () => {
3946
zoom: 19,
4047
});
4148
});
49+
50+
it('pb id marker', async ({ expect }) => {
51+
const res = await readPB(
52+
'!1m17!1m12!1m3!1d1.1!2d1.1!3d1.1!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m2!1m1!1s0x0:0x0!5e0!3m2!1sde!2sde!4v1557583694739!5m2!1sde!2sde'
53+
);
54+
55+
expect(res).toStrictEqual({
56+
area: {
57+
lat: 1.1,
58+
lon: 1.1,
59+
},
60+
markers: [],
61+
tile: 'roadmap',
62+
zoom: 19,
63+
});
64+
});
65+
66+
it('pb markers to readQ', async ({ expect }) => {
67+
global.fetch = vi.fn().mockResolvedValue(mockNominatimResponse(result, true));
68+
69+
const res = await readPB(
70+
`!1m17!1m12!1m3!1d1.1!2d1.1!3d1.1!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m2!1m1!1s${input}!5e0!3m2!1sde!2sde!4v1557583694739!5m2!1sde!2sde`
71+
);
72+
73+
expect(res).toStrictEqual({
74+
area: {
75+
lat: 1.1,
76+
lon: 1.1,
77+
},
78+
markers: [
79+
{
80+
label: input,
81+
lat: parseFloat(result[0].lat),
82+
lon: parseFloat(result[0].lon),
83+
},
84+
],
85+
tile: 'roadmap',
86+
zoom: 19,
87+
});
88+
});
4289
});
4390

4491
describe.concurrent('read query', () => {
45-
it('', async ({ expect }) => {
46-
const res = readQ(''); // TODO: Mocking requests
92+
it('nominatim request', async ({ expect }) => {
93+
global.fetch = vi.fn().mockResolvedValue(mockNominatimResponse(result, true));
94+
95+
const res = await readQ(input); // TODO: Mocking requests
96+
97+
expect(fetch).toBeCalledWith(encodeURI(nominatimQ + input));
98+
99+
expect(res).toStrictEqual({
100+
label: input,
101+
lat: parseFloat(result[0].lat),
102+
lon: parseFloat(result[0].lon),
103+
});
104+
});
105+
106+
it('failing nominatim request', async ({ expect }) => {
107+
global.fetch = vi.fn().mockResolvedValue(mockNominatimResponse(result, false));
108+
109+
const res = await readQ(input);
110+
111+
expect(res).null;
47112
});
48113
});

0 commit comments

Comments
 (0)