terriajs
Version:
Geospatial data visualization platform.
129 lines (111 loc) • 4.18 kB
JavaScript
;
import KmlCatalogItem from "../../../../lib/Models/Catalog/CatalogItems/KmlCatalogItem";
import TerriaError from "../../../../lib/Core/TerriaError";
import Terria from "../../../../lib/Models/Terria";
import loadBlob from "../../../../lib/Core/loadBlob";
import loadText from "../../../../lib/Core/loadText";
import loadXML from "../../../../lib/Core/loadXML";
// KML requires support for Blob. See https://github.com/TerriaJS/terriajs/issues/508
var describeIfSupported = typeof Blob !== "undefined" ? describe : xdescribe;
describeIfSupported("KmlCatalogItem", function () {
var terria;
var kml;
beforeEach(function () {
terria = new Terria({
baseUrl: "./"
});
kml = new KmlCatalogItem(terria);
});
it("can load a KML file by URL", async function () {
kml.url = "test/KML/vic_police.kml";
await kml.load();
expect(kml.dataSource.entities.values.length).toBeGreaterThan(0);
});
it("use provided dataUrl", async function () {
kml.url = "test/KML/vic_police.kml";
kml.dataUrl = "test/test.html";
await kml.load();
expect(kml.dataSource.entities.values.length).toBeGreaterThan(0);
expect(kml.dataUrl).toBe("test/test.html");
});
it("have default dataUrl and dataUrlType", function () {
kml.updateFromJson({
url: "test/KML/vic_police.kml"
});
expect(kml.dataUrl).toBe("test/KML/vic_police.kml");
expect(kml.dataUrlType).toBe("direct");
});
it("can load a KML file by provided XML data", async function () {
const xml = await loadXML("test/KML/vic_police.kml");
kml.data = xml;
kml.dataSourceUrl = "anything.kml";
await kml.load();
expect(kml.dataSource.entities.values.length).toBeGreaterThan(0);
});
it("can load a KML file by provided Blob", async function () {
const blob = await loadBlob("test/KML/vic_police.kml");
kml.data = blob;
kml.dataSourceUrl = "anything.kml";
await kml.load();
expect(kml.dataSource.entities.values.length).toBeGreaterThan(0);
});
it("can load a KML file by provided string", async function () {
const text = await loadText("test/KML/vic_police.kml");
kml.data = text;
kml.dataSourceUrl = "anything.kml";
await kml.load();
expect(kml.dataSource.entities.values.length).toBeGreaterThan(0);
});
it("can load a KMZ file by URL", async function () {
// eslint-disable-next-line @typescript-eslint/no-require-imports
kml.url = require("file-loader!../../../../wwwroot/test/KML/vic_police.kmz");
await kml.load();
expect(kml.dataSource.entities.values.length).toBeGreaterThan(0);
});
it("can load a KMZ file by provided Blob", async function () {
const blob = await loadBlob("test/KML/vic_police.kmz");
kml.data = blob;
kml.dataSourceUrl = "anything.kmz";
kml.load();
expect(kml.dataSource.entities.values.length).toBeGreaterThan(0);
});
describe("error handling", function () {
it("fails gracefully when the data at a URL is not XML", async function () {
kml.url = "test/CZML/simple.czml";
await kml
.load()
.then(function () {
throw new Error("Load should not succeed.");
})
.catch(function (e) {
expect(e instanceof TerriaError).toBe(true);
});
});
it("fails gracefully when the provided string is not XML", async function () {
const text = await loadText("test/CZML/simple.czml");
kml.data = text;
kml.dataSourceUrl = "anything.czml";
await kml
.load()
.then(function () {
throw new Error("Load should not succeed.");
})
.catch(function (e) {
expect(e instanceof TerriaError).toBe(true);
});
});
it("fails gracefully when the provided blob is not XML", async function () {
const blob = await loadBlob("test/CZML/simple.czml");
kml.data = blob;
kml.dataSourceUrl = "anything.czml";
await kml
.load()
.then(function () {
throw new Error("Load should not succeed.");
})
.catch(function (e) {
expect(e instanceof TerriaError).toBe(true);
});
});
});
});