Skip to content

Commit

Permalink
test: nominatim api call mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
nobkd committed Apr 5, 2023
1 parent de52d8c commit ea13f58
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/map/utils/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parsePB, tileTypes, type TileType } from './parsePB';
import { parseDMS } from './parseDMS';
import { getMapZoom } from './zoom';

const nominatimQ: string = 'https://nominatim.openstreetmap.org/search/?limit=1&format=json&q=';
export const nominatimQ: string = 'https://nominatim.openstreetmap.org/search/?limit=1&format=json&q=';
const cidMatch: RegExp = /^0x[\da-f]+:0x[\da-f]+$/i;
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;

Expand Down
77 changes: 71 additions & 6 deletions test/map/utils/read.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { describe, it } from 'vitest';
import { readPB, readQ } from '../../../src/map/utils/read';
import { describe, it, vi } from 'vitest';
import { readPB, readQ, nominatimQ } from '../../../src/map/utils/read';

const input = 'test position';
const result = [{ lat: '1.1', lon: '1.1' }];

function mockNominatimResponse(data: { lat: string; lon: string }[], status: boolean) {
return { ok: status, json: () => new Promise((resolve) => resolve(data)) };
}

describe.concurrent('read pb', () => {
it('read example', async ({ expect }) => {
Expand All @@ -18,9 +25,9 @@ describe.concurrent('read pb', () => {
});
});

it('pb markers', async ({ expect }) => {
it('pb base64 marker', async ({ expect }) => {
const res = await readPB(
'!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'
'!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'
);

expect(res).toStrictEqual({
Expand All @@ -39,10 +46,68 @@ describe.concurrent('read pb', () => {
zoom: 19,
});
});

it('pb id marker', async ({ expect }) => {
const res = await readPB(
'!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'
);

expect(res).toStrictEqual({
area: {
lat: 1.1,
lon: 1.1,
},
markers: [],
tile: 'roadmap',
zoom: 19,
});
});

it('pb markers to readQ', async ({ expect }) => {
global.fetch = vi.fn().mockResolvedValue(mockNominatimResponse(result, true));

const res = await readPB(
`!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`
);

expect(res).toStrictEqual({
area: {
lat: 1.1,
lon: 1.1,
},
markers: [
{
label: input,
lat: parseFloat(result[0].lat),
lon: parseFloat(result[0].lon),
},
],
tile: 'roadmap',
zoom: 19,
});
});
});

describe.concurrent('read query', () => {
it('', async ({ expect }) => {
const res = readQ(''); // TODO: Mocking requests
it('nominatim request', async ({ expect }) => {
global.fetch = vi.fn().mockResolvedValue(mockNominatimResponse(result, true));

const res = await readQ(input); // TODO: Mocking requests

expect(fetch).toBeCalledWith(encodeURI(nominatimQ + input));

expect(res).toStrictEqual({
label: input,
lat: parseFloat(result[0].lat),
lon: parseFloat(result[0].lon),
});
});

it('failing nominatim request', async ({ expect }) => {
global.fetch = vi.fn().mockResolvedValue(mockNominatimResponse(result, false));

const res = await readQ(input);

expect(res).null;
});
});

0 comments on commit ea13f58

Please sign in to comment.