Skip to content

Commit

Permalink
general: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz committed May 18, 2021
1 parent ee12f6a commit 52e560e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 45 deletions.
7 changes: 0 additions & 7 deletions jest-eslint.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
// setupFiles: ['raf/polyfill', './etc/jestSetup.js'],
// setupFilesAfterEnv: ['<rootDir>/etc/jestSetupFramework.js'],
// snapshotSerializers: ['enzyme-to-json/serializer'],
testMatch: ['**/__tests__/**/?(*.)+(spec|test).js'],
testMatch: ['**/__tests__/**/?(*.)+(spec|test).ts{,x}'],
testPathIgnorePatterns: ['.next', 'node_modules', 'dist'],
coverageReporters: ['json', 'lcov', 'text-summary'],
};
55 changes: 32 additions & 23 deletions src/services/__tests__/fixture.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// https://www.openstreetmap.org/api/0.6/${type}/${id} --> xml2js
export const node = {
geojson: {
center: [14.3931537, 50.1004964],
geometry: {
type: 'Point',
coordinates: [14.3931537, 50.1004964],
},
osmMeta: {
Expand All @@ -17,6 +19,10 @@ export const node = {
visible: 'true',
},
properties: {
class: 'library',
subclass: 'books',
},
tags: {
level: '-1',
name: 'Neoluxor',
opening_hours: 'Mo-Fr 08:00-19:00; Sa 09:00-14:00',
Expand Down Expand Up @@ -91,31 +97,30 @@ export const overpassWay = {
query: 'way(7663781);(._;>;);out;',

geojson: {
center: [14.389852900000001, 50.0995943],
geometry: {
coordinates: [
[
[14.390274, 50.0999117],
[14.3902903, 50.0997806],
[14.390343, 50.0993561],
[14.3902647, 50.0993519],
[14.3902536, 50.0993434],
[14.3901622, 50.0993383],
[14.3901505, 50.099345],
[14.3901466, 50.0993785],
[14.3896467, 50.0993525],
[14.3896196, 50.0993511],
[14.3896273, 50.0993174],
[14.3896159, 50.0993092],
[14.3895082, 50.0993047],
[14.3895111, 50.0992809],
[14.3894357, 50.0992769],
[14.3893791, 50.0997338],
[14.3893628, 50.0998652],
[14.3896757, 50.0998812],
[14.3897352, 50.0998844],
[14.3897965, 50.0998874],
[14.390274, 50.0999117],
],
[14.390274, 50.0999117],
[14.3902903, 50.0997806],
[14.390343, 50.0993561],
[14.3902647, 50.0993519],
[14.3902536, 50.0993434],
[14.3901622, 50.0993383],
[14.3901505, 50.099345],
[14.3901466, 50.0993785],
[14.3896467, 50.0993525],
[14.3896196, 50.0993511],
[14.3896273, 50.0993174],
[14.3896159, 50.0993092],
[14.3895082, 50.0993047],
[14.3895111, 50.0992809],
[14.3894357, 50.0992769],
[14.3893791, 50.0997338],
[14.3893628, 50.0998652],
[14.3896757, 50.0998812],
[14.3897352, 50.0998844],
[14.3897965, 50.0998874],
[14.390274, 50.0999117],
],
type: 'LineString',
},
Expand All @@ -124,6 +129,10 @@ export const overpassWay = {
type: 'way',
},
properties: {
class: 'lodging',
subclass: 'hotel',
},
tags: {
'addr:city': 'Praha',
'addr:housenumber': '15',
'addr:postcode': '16000',
Expand Down
10 changes: 4 additions & 6 deletions src/services/__tests__/osmApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ describe('osmToGeojson()', () => {
expect(geojson).toEqual(node.geojson);
});

// it('should convert osm_api way', async () => {
// const geojson = await osmToGeojson(way.xml);
// expect(geojson).toEqual(way.geojson);
// });

it('should convert overpassWay', async () => {
const geojson = await osmToGeojson(overpassWay.xml);
expect(geojson).toEqual(overpassWay.geojson);
Expand All @@ -29,6 +24,9 @@ describe('osmToGeojson()', () => {

it('should convert overpassBuggyWay', async () => {
const geojson = await osmToGeojson(overpassBuggyWay.xml);
expect(geojson).toMatchObject({ properties: { leisure: 'playground' } });
expect(geojson).toMatchObject({
tags: { leisure: 'playground' },
properties: { subclass: 'playground' },
});
});
});
9 changes: 2 additions & 7 deletions src/services/osmApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { isBrowser } from '../components/helpers';
import { fetchText } from './fetch';
import { Feature } from './types';
import { osmToGeojson } from './osmToGeojson';
import { getCenter } from './getCenter';

export const OSM_API = 'https://www.openstreetmap.org/api/0.6';
export const OSM_FEATURE_URL = ({ type, id }) => `${OSM_API}/${type}/${id}`;
Expand All @@ -24,11 +23,7 @@ export const getFeatureFromApi = async (featureId): Promise<Feature> => {
const url = isNode ? OSM_FEATURE_URL(apiId) : OP_FEATURE_URL(apiId);

const response = await fetchText(url, { putInAbortableQueue: true });
const geojson = await osmToGeojson(response);
return {
...geojson,
center: getCenter(geojson.geometry),
};
return osmToGeojson(response);
};

export const fetchFromApi = async (osmApiId) => {
Expand All @@ -41,6 +36,6 @@ export const fetchFromApi = async (osmApiId) => {
} catch (e) {
// eslint-disable-next-line no-console
console.warn(e);
return null;
return null; // TODO show some user error message
}
};
5 changes: 4 additions & 1 deletion src/services/osmToGeojson.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LineString, Point, Position } from './types';
import { parseXmlString } from './helpers';
import { getPoiClass } from './getPoiClass';
import { getCenter } from './getCenter';

const coords = (x): Position => [parseFloat(x.$.lon), parseFloat(x.$.lat)];
const lookupNode = (osmXml, id) => osmXml.node.find((x) => x.$.id === id);
Expand Down Expand Up @@ -32,6 +33,7 @@ export const osmToGeojson = async (osmXmlStr) => {
if (!item) {
throw new Error('Empty osmXml result');
}
const geometry = getGeometry[type](osmXml, item);

const osmMeta = { type, ...item.$ };
const tagItems = item.tag.length ? item.tag : [item.tag];
Expand All @@ -42,7 +44,8 @@ export const osmToGeojson = async (osmXmlStr) => {

return {
type: 'Feature' as const,
geometry: getGeometry[type](osmXml, item),
geometry,
center: getCenter(geometry),
osmMeta,
tags,
properties: getPoiClass(tags),
Expand Down

0 comments on commit 52e560e

Please sign in to comment.