@panoramax/web-viewer
Version:
Panoramax web viewer for geolocated pictures
155 lines (132 loc) • 4.7 kB
JavaScript
import {
decodeKey, decodeBasicTag, computeDiffTags, groupByPrefix
} from "../../src/utils/semantics";
describe("decodeBasicTag", () => {
test("should return null if no equal sign is present", () => {
expect(decodeBasicTag("key")).toBeNull();
});
test("should correctly decode a tag with an equal sign", () => {
expect(decodeBasicTag("key=value")).toEqual({
key: { key: "key", prefix: "", subkey: "key", qualifies: null },
value: "value"
});
});
test("should correctly decode a tag with a prefix", () => {
expect(decodeBasicTag("osm|key=value")).toEqual({
key: { key: "osm|key", prefix: "osm", subkey: "key", qualifies: null },
value: "value"
});
});
});
describe("decodeKey", () => {
test("should return default structure if key does not match regex", () => {
expect(decodeKey("panoKey")).toEqual({
key: "panoKey",
prefix: "",
subkey: "panoKey",
qualifies: null
});
});
test("should correctly decode a key with a prefix", () => {
expect(decodeKey("osm|traffic_sign")).toEqual({
key: "osm|traffic_sign",
prefix: "osm",
subkey: "traffic_sign",
qualifies: null
});
});
test("should correctly decode a key with qualifiers", () => {
expect(decodeKey("detection_model[camera_mount=pole]")).toEqual({
key: "detection_model[camera_mount=pole]",
prefix: "",
subkey: "detection_model",
qualifies: { key: { key: "camera_mount", prefix: "", subkey: "camera_mount", qualifies: null }, value: "pole" }
});
});
test("should correctly decode a key with a prefix and qualifiers", () => {
expect(decodeKey("osm|source[osm|traffic_sign=stop]")).toEqual({
key: "osm|source[osm|traffic_sign=stop]",
prefix: "osm",
subkey: "source",
qualifies: { key: { key: "osm|traffic_sign", prefix: "osm", subkey: "traffic_sign", qualifies: null }, value: "stop" }
});
});
});
describe("computeDiffTags", () => {
it("should return new set of tags when no tag is missing in next", () => {
const prev = [{key: "tag1", value: "value1"}, {key: "tag2", value: "value2"}];
const next = [{key: "tag1", value: "value1"}, {key: "tag2", value: "value2"}, {key: "tag3", value: "value3"}];
expect(computeDiffTags(prev, next)).toEqual([{key: "tag3", value: "value3", action: "add"}]);
});
it("should return new set of tags with missing tags marked as delete when some tag is missing in next", () => {
const prev = [{key: "tag1", value: "value1"}, {key: "tag2", value: "value2"}];
const next = [{key: "tag1", value: "value1"}];
expect(computeDiffTags(prev, next)).toEqual([{key: "tag2", value: "value2", action: "delete"}]);
});
it("should return new set of tags with all tags marked as delete when all tags are missing in next", () => {
const prev = [{key: "tag1", value: "value1"}, {key: "tag2", value: "value2"}];
const next = [];
expect(computeDiffTags(prev, next)).toEqual([{key: "tag1", value: "value1", action: "delete"}, {key: "tag2", value: "value2", action: "delete"}]);
});
});
describe("groupByPrefix", () => {
test("should group tags by prefix", () => {
const tags = [
{ key: "osm|highway", value: "residential" },
{ key: "wd|P31", value: "Q5" },
{ key: "exif|model", value: "CameraModel" }
];
const expected = [
{
prefix: "osm",
title: "OpenStreetMap",
tags: [{ key: "highway", value: "residential" }],
key_transform: expect.any(Function),
logo: "osm.svg",
value_transform: expect.any(Function)
},
{
prefix: "wd",
title: "Wikidata",
tags: [{ key: "P31", value: "Q5" }],
key_transform: expect.any(Function),
logo: "wd.svg",
value_transform: expect.any(Function)
},
{
prefix: "exif",
title: "EXIF",
tags: [{ key: "model", value: "CameraModel" }]
}
];
expect(groupByPrefix(tags)).toEqual(expected);
});
test("should handle qualifiers correctly", () => {
const tags = [
{ key: "osm|highway", value: "residential" },
{ key: "osm|source[osm|highway=residential]", value: "opendata" }
];
const expected = [{
prefix: "osm",
title: "OpenStreetMap",
tags: [{
key: "highway",
value: "residential",
qualifiers: [{ key: "osm|source[osm|highway=residential]", prefix: "osm", subkey: "source", value: "opendata" }]
}],
key_transform: expect.any(Function),
logo: "osm.svg",
value_transform: expect.any(Function)
}];
expect(groupByPrefix(tags)).toEqual(expected);
});
test("should handle unknown prefixes", () => {
const tags = [ { key: "unknown|key", value: "value" } ];
const expected = [{
prefix: "unknown",
title: "unknown",
tags: [{ key: "key", value: "value" }]
}];
expect(groupByPrefix(tags)).toEqual(expected);
});
});