UNPKG

@yaga/indexed-db-tile-cache

Version:

Spatial tile cache that saves its data into the IndexedDB of your browser

202 lines 10.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var buffer_1 = require("buffer"); var chai_1 = require("chai"); var index_1 = require("./index"); describe("IndexedDbTileCache", function () { it("should create a store", function () { chai_1.expect(new index_1.IndexedDbTileCache()).to.instanceof(index_1.IndexedDbTileCache); }); it("should purge the store", function (done) { new index_1.IndexedDbTileCache().purgeStore().then(function () { done(); }, done); }); it("should fulfill the default values", function () { var tileCache = new index_1.IndexedDbTileCache(); chai_1.expect(tileCache.options.databaseName).to.equal("tile-cache-data"); chai_1.expect(tileCache.options.databaseVersion).to.equal(1); chai_1.expect(tileCache.options.objectStoreName).to.equal("OSM"); chai_1.expect(tileCache.options.tileUrl).to.equal("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"); chai_1.expect(tileCache.options.tileUrlSubDomains).to.deep.equal(["a", "b", "c"]); chai_1.expect(tileCache.options.crawlDelay).to.equal(500); chai_1.expect(tileCache.options.maxAge).to.equal(1000 * 60 * 60 * 24 * 7); }); describe(".downloadTile", function () { it("should download a tile", function (done) { var tileCache = new index_1.IndexedDbTileCache(); tileCache.downloadTile({ x: 0, y: 0, z: 0 }).then(function (value) { chai_1.expect(value).to.has.property("contentType"); chai_1.expect(value).to.has.property("data"); chai_1.expect(value).to.has.property("url"); chai_1.expect(value).to.has.property("timestamp"); chai_1.expect(value.contentType).to.equal("image/png"); chai_1.expect(value.data).to.be.instanceOf(Uint8Array); chai_1.expect(value.url).to.equal("http://{s}.tile.openstreetmap.org/0/0/0.png"); chai_1.expect(value.timestamp).to.be.a("number"); done(); }, done); }); it("should have stored the before downloaded tile", function (done) { var dbRequest = indexedDB.open("tile-cache-data", 1); dbRequest.addEventListener("success", function (dbEvent) { var database = dbEvent.target.result; var tx = database.transaction(["OSM"]) .objectStore("OSM").get("http://{s}.tile.openstreetmap.org/0/0/0.png"); tx.addEventListener("success", function (event) { chai_1.expect(event.target.result).to.has.property("data"); chai_1.expect(event.target.result.data).to.be.instanceOf(Uint8Array); done(); }); }); }); }); describe(".getTileEntry", function () { it("should get the before downloaded tile", function (done) { var tileCache = new index_1.IndexedDbTileCache(); tileCache.getTileEntry({ x: 0, y: 0, z: 0 }).then(function (tile) { chai_1.expect(tile.url).to.equal("http://{s}.tile.openstreetmap.org/0/0/0.png"); done(); }, done); }); it("should not get a new tile without the download flag", function (done) { var tileCache = new index_1.IndexedDbTileCache(); tileCache.getTileEntry({ x: 0, y: 0, z: 1 }) .then(/* istanbul ignore next */ function () { done(new Error("Received a tile")); }, function (err) { chai_1.expect(err.message).to.equal("Unable to find entry"); done(); }); }); it("should get a new tile with the download flag", function (done) { var tileCache = new index_1.IndexedDbTileCache(); tileCache.getTileEntry({ x: 0, y: 0, z: 2 }, true).then(function (tile) { chai_1.expect(tile.url).to.equal("http://{s}.tile.openstreetmap.org/2/0/0.png"); done(); }, done); }); it("should re-download an outdated tile", function (done) { var dbRequest = indexedDB.open("tile-cache-data", 1); dbRequest.addEventListener("success", function (dbEvent) { var database = dbEvent.target.result; var tx = database.transaction(["OSM"], "readwrite") .objectStore("OSM").put({ contentType: "wrong/one", data: new Uint8Array(0), timestamp: 123, url: "http://{s}.tile.openstreetmap.org/0/0/0.png", }); tx.addEventListener("success", function () { var tileCache = new index_1.IndexedDbTileCache(); tileCache.getTileEntry({ x: 0, y: 0, z: 0 }, true).then(function (tile) { chai_1.expect(tile.contentType).to.not.equal("wrong/one"); chai_1.expect(tile.contentType).to.equal("image/png"); chai_1.expect(tile.timestamp).to.be.not.equal(123); done(); }, done); }); }); }); }); describe(".createInternalTileUrl", function () { it("should get an url that still have the sub domain as placeholder", function () { var tileCache = new index_1.IndexedDbTileCache(); chai_1.expect(tileCache.createInternalTileUrl({ x: 1, y: 2, z: 3 })) .to.equal("http://{s}.tile.openstreetmap.org/3/1/2.png"); }); }); describe(".createTileUrl", function () { it("should get an url without any placeholder", function () { var tileCache = new index_1.IndexedDbTileCache(); chai_1.expect(tileCache.createTileUrl({ x: 1, y: 2, z: 3 })) .to.be.oneOf([ "http://a.tile.openstreetmap.org/3/1/2.png", "http://b.tile.openstreetmap.org/3/1/2.png", "http://c.tile.openstreetmap.org/3/1/2.png", ]); }); }); describe(".getTileAsBuffer", function () { it("should get the already fetched tile as Buffer and Uint8Array", function (done) { var tileCache = new index_1.IndexedDbTileCache(); tileCache.getTileAsBuffer({ x: 0, y: 0, z: 0 }).then(function (buffer) { chai_1.expect(buffer).to.be.instanceOf(buffer_1.Buffer); chai_1.expect(buffer).to.be.instanceOf(Uint8Array); done(); }, done); }); it("should get a tile that was not fetched before as Buffer and Uint8Array", function (done) { var tileCache = new index_1.IndexedDbTileCache(); tileCache.getTileAsBuffer({ x: 10, y: 10, z: 10 }).then(function (buffer) { chai_1.expect(buffer).to.be.instanceOf(buffer_1.Buffer); chai_1.expect(buffer).to.be.instanceOf(Uint8Array); done(); }, done); }); }); describe(".getTileAsDataUrl", function () { it("should get the already fetched tile as data-url", function (done) { var tileCache = new index_1.IndexedDbTileCache(); tileCache.getTileAsDataUrl({ x: 0, y: 0, z: 0 }).then(function (url) { chai_1.expect(url).to.be.a("string"); chai_1.expect(url.substr(0, 22)).to.equal("data:image/png;base64,"); chai_1.expect(url.length).to.be.greaterThan(100); done(); }, done); }); it("should get a tile that was not fetched before as Buffer and Uint8Array", function (done) { var tileCache = new index_1.IndexedDbTileCache(); tileCache.getTileAsDataUrl({ x: 20, y: 20, z: 10 }).then(function (url) { chai_1.expect(url).to.be.a("string"); chai_1.expect(url.substr(0, 22)).to.equal("data:image/png;base64,"); chai_1.expect(url.length).to.be.greaterThan(100); done(); }, done); }); }); describe(".seedBBox", function () { it("should seed a bounding-box", function (done) { var tileCache = new index_1.IndexedDbTileCache(); tileCache.seedBBox({ maxLat: 0, maxLng: 0, minLat: 0, minLng: 0 }, 0).then(function () { done(); }, done); }); it("should emit 'seed-progress' while seeding", function (done) { var tileCache = new index_1.IndexedDbTileCache(); var expectedEmits = 2; tileCache.on("seed-progress", function (progress) { chai_1.expect(progress).to.has.property("remains"); chai_1.expect(progress).to.has.property("total"); chai_1.expect(progress.total).to.equal(1); expectedEmits -= 1; if (expectedEmits === 0) { return done(); } }); tileCache.seedBBox({ maxLat: 0, maxLng: 0, minLat: 0, minLng: 0 }, 0).catch(done); }); }); describe(".purgeStore", function () { it("should purge the whole store", function (done) { new index_1.IndexedDbTileCache().purgeStore().then(function () { var dbRequest = indexedDB.open("tile-cache-data", 1); dbRequest.addEventListener("success", function (dbEvent) { var database = dbEvent.target.result; var tx = database.transaction(["OSM"]) .objectStore("OSM").get("http://{s}.tile.openstreetmap.org/0/0/0.png"); tx.addEventListener("success", function (event) { /* istanbul ignore else */ if (event.target.result === undefined) { return done(); } /* istanbul ignore next */ done(new Error("Found removed store")); }); tx.addEventListener("error", /* istanbul ignore next */ function () { done(); }); }); }, done); }); }); }); //# sourceMappingURL=tile-cache.spec.js.map