Skip to content

Commit

Permalink
Use fake-indexeddb to fix cache unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steinbro committed Nov 25, 2024
1 parent c80cf4f commit 514db99
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 30 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
71 changes: 41 additions & 30 deletions src/state/cache.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 514db99

Please sign in to comment.