diff --git a/package-lock.json b/package-lock.json index f743d44..36d6dc8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,6 +33,7 @@ "cypress": "^13.15.2", "esm": "^3.2.25", "express": "^4.19.2", + "fake-indexeddb": "^6.0.0", "mocha": "^10.8.2", "morgan": "^1.10.0", "start-server-and-test": "^2.0.4", @@ -10948,6 +10949,15 @@ "node >=0.6.0" ] }, + "node_modules/fake-indexeddb": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/fake-indexeddb/-/fake-indexeddb-6.0.0.tgz", + "integrity": "sha512-YEboHE5VfopUclOck7LncgIqskAqnv4q0EWbYCaxKKjAvO93c+TJIaBuGy8CBFdbg9nKdpN3AuPRwVBJ4k7NrQ==", + "dev": true, + "engines": { + "node": ">=18" + } + }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", diff --git a/package.json b/package.json index 163550b..126f9bf 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "cypress": "^13.15.2", "esm": "^3.2.25", "express": "^4.19.2", + "fake-indexeddb": "^6.0.0", "mocha": "^10.8.2", "morgan": "^1.10.0", "start-server-and-test": "^2.0.4", diff --git a/src/state/cache.test.ts b/src/state/cache.test.ts index c926064..726c3c5 100644 --- a/src/state/cache.test.ts +++ b/src/state/cache.test.ts @@ -1,38 +1,49 @@ -import { cache } from "./cache"; +// Mock out IndexedDB for testing +import 'fake-indexeddb/auto'; + +import { cache, SoundscapeFeature } from "./cache"; import { expect } from "chai"; +// Real feature extracted from tile +const testFeature: SoundscapeFeature = { + "feature_type": "amenity", + "feature_value": "cafe", + "geometry": { + "coordinates": [-80.52166, 43.463198], + "type": "Point" + }, + "osm_ids": [503048818], + "properties": { + "amenity": "cafe", + "cuisine": "bubble_tea", + "name": "The Bingsu" + }, + "type": "Feature" +}; + describe("cache", () => { - it('should be empty for unloaded tiles', () => { - cache.getFeatures("16/18109/23965").then((result) => { - expect(result.length).to.equal(0); - }); + it('should be empty for unloaded tiles', async () => { + const result = await cache.getFeatures("16/18109/23965"); + expect(result.length).to.equal(0); }); - it('should store and retrieve features', () => { - // Real feature extracted from tile - cache.addFeature({ - "feature_type": "amenity", - "feature_value": "cafe", - "geometry": { - "coordinates": [-80.52166, 43.463198], - "type": "Point" - }, - "osm_ids": [503048818], - "properties": { - "amenity": "cafe", - "cuisine": "bubble_tea", - "name": "The Bingsu" - }, - "type": "Feature" - }, - "16/18109/23965"); + it('should store and retrieve features', async () => { + cache.addFeature(testFeature, "16/18109/23965"); + const result = await cache.getFeatures("16/18109/23965"); + expect(result.length).to.equal(1); + // Original feature will be annotated with an id + expect(result[0]).to.deep.equal({...testFeature, id: 1}); + }); + + it ('should fetch features by OSM ID', async () => { + cache.addFeature(testFeature, "16/18109/23965"); + const result = await cache.getFeatureByOsmId(testFeature.osm_ids[0]); + // Original feature will be annotated with an id + expect(result).to.deep.equal({...testFeature, id: 1}); + }); - cache.getFeatures("16/18109/23965").then((result) => { - expect(result.length).to.equal(1); - expect(result[0]).to.include({ - "feature_type": "amenity", - "feature_value": "cafe", - }); - }); + it ('should return no results for invalid OSM ID', async () => { + const result = await cache.getFeatureByOsmId(9999999); + expect(result).to.equal(null); }); }); \ No newline at end of file