Skip to content

Commit

Permalink
Api mock (#52)
Browse files Browse the repository at this point in the history
* added test
  • Loading branch information
Beakerboy authored May 16, 2024
1 parent 488f110 commit 96efc5b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
"coveralls": "*",
"c8": "*",
"eslint": "^8.13",
"jsdom": "*",
"jest": "*",
"jest-environment-jsdom": "*",
"jest-fetch-mock": "*",
"jest-matcher-deep-close-to": "*",
"jsdom": "*",
"pyramid": "file:../pyramid",
"ramp": "file:../ramp",
"wedge": "file:../wedge"
Expand Down
11 changes: 7 additions & 4 deletions src/building.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,18 @@ class Building {
}

/**
* build an array of all the lat/long values of the nodes
* Extract all nodes from an XML file.
*
* @param {DOM.Element} fullXmlData - OSM XML with nodes
*
* @return {Object} dictionary of nodes
*/
static buildNodeList(fullXmlData) {
const nodeElements = fullXmlData.getElementsByTagName('node');
let id = 0;
var node;
var coordinates = [];
var nodeList = [];
// create a BuildingShape object from the outer and inner elements.
let coordinates = [];
const nodeList = {};
for (let j = 0; j < nodeElements.length; j++) {
node = nodeElements[j];
id = node.getAttribute('id');
Expand Down
69 changes: 69 additions & 0 deletions test/building.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @jest-environment jsdom
*/

import { Shape, Mesh } from 'three';
import { TextEncoder } from 'node:util';
global.TextEncoder = TextEncoder;

let apis = {
bounding: {
api:'https://api.openstreetmap.org/api/0.6/map?bbox=',
url: (left, bottom, right, top) => {
return apis.bounding.api + left + ',' + bottom + ',' + right + ',' + top;
},
},
getRelation: {
api:'https://api.openstreetmap.org/api/0.6/relation/',
parameters:'/full',
url: (relationId) => {
return apis.getRelation.api + relationId + apis.getRelation.parameters;
},
},
};
global.apis = apis;

import { Building } from '../src/building.js';

import fetchMock from 'jest-fetch-mock';
fetchMock.enableMocks();

const data = `
<osm>
<node id="3" lat="4" lon="4"/>
<node id="5" lat="4" lon="4.001"/>
<node id="6" lat="4.001" lon="4.001"/>
<node id="7" lat="4.001" lon="4"/>
<relation id="4">
<member ref="1" role="outer"/>
<tag k="type" v="multipolygon"/>
<tag k="building" v="yes"/>
<tag k="roof:shape" v="skillion"/>
<tag k="roof:direction" v="0"/>
<tag k="roof:angle" v="45"/>
</relation>
<way id="1">
<nd ref="3"/>
<nd ref="5"/>
<nd ref="6"/>
<nd ref="7"/>
<nd ref="3"/>
</way>
</osm>`;

beforeEach(() => {
fetch.resetMocks();
});

// test('Test Factory', async() => {
// fetch.mockResponseOnce(data);
// const shape = await Building.create('relation', '4');
// expect(fetch).toHaveBeenCalledTimes(2);
//});

test('Create Nodelist', () => {
let xmlData = new window.DOMParser().parseFromString(data, 'text/xml');
const list = Building.buildNodeList(xmlData);
expect(Object.keys(list).length).toBe(4);
expect(list['3']).toStrictEqual(['4', '4']);
});

0 comments on commit 96efc5b

Please sign in to comment.