diff --git a/package.json b/package.json index 6a8950e..58d3b38 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/building.js b/src/building.js index 3031323..fd8d0e9 100644 --- a/src/building.js +++ b/src/building.js @@ -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'); diff --git a/test/building.test.js b/test/building.test.js new file mode 100644 index 0000000..d2c69f6 --- /dev/null +++ b/test/building.test.js @@ -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 = ` + + + + + + + + + + + + + + + + + + + + +`; + +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']); +});