Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
import test from 'tape';
import {mergeAnnotations, INITIAL_VIS_STATE} from '@kepler.gl/reducers';
import {AnnotationKind} from '@kepler.gl/constants';
const makeState = (annotations = []) => ({
...INITIAL_VIS_STATE,
annotations
});
const makeAnnotation = (overrides = {}) => ({
id: 'ann-1',
kind: AnnotationKind.POINT,
isVisible: true,
autoSize: true,
autoSizeY: true,
anchorPoint: [10, 20],
label: 'Test',
lineColor: '#FFFFFF',
lineWidth: 2,
textWidth: 0,
textHeight: 0,
armLength: 50,
angle: -45,
...overrides
});
test('#mergeAnnotations -> should merge valid annotations', t => {
const state = makeState();
const annotations = [makeAnnotation({id: 'a1'}), makeAnnotation({id: 'a2', label: 'Second'})];
const result = mergeAnnotations(state, annotations);
t.equal(result.annotations.length, 2, 'should merge 2 annotations');
t.equal(result.annotations[0].id, 'a1', 'first annotation id should match');
t.equal(result.annotations[1].id, 'a2', 'second annotation id should match');
t.end();
});
test('#mergeAnnotations -> should return same state for empty input', t => {
const state = makeState();
t.equal(mergeAnnotations(state, []), state, 'should return same state for empty array');
t.equal(mergeAnnotations(state, null), state, 'should return same state for null');
t.equal(mergeAnnotations(state, undefined), state, 'should return same state for undefined');
t.end();
});
test('#mergeAnnotations -> should filter out invalid annotations', t => {
const state = makeState();
const annotations = [
makeAnnotation({id: 'valid'}),
{id: null, kind: AnnotationKind.POINT, anchorPoint: [0, 0]},
{id: 'no-kind', kind: null, anchorPoint: [0, 0]},
{id: 'no-anchor', kind: AnnotationKind.POINT, anchorPoint: 'invalid'},
{id: 'bad-kind', kind: 'INVALID_KIND', anchorPoint: [0, 0]},
null,
undefined
];
const result = mergeAnnotations(state, annotations);
t.equal(result.annotations.length, 1, 'should only include valid annotation');
t.equal(result.annotations[0].id, 'valid', 'should keep the valid one');
t.end();
});
test('#mergeAnnotations -> should reject duplicate IDs', t => {
const existing = makeAnnotation({id: 'existing-1'});
const state = makeState([existing]);
const annotations = [
makeAnnotation({id: 'existing-1', label: 'Duplicate'}),
makeAnnotation({id: 'new-1', label: 'New'})
];
const result = mergeAnnotations(state, annotations);
t.equal(result.annotations.length, 2, 'should have 2 annotations (1 existing + 1 new)');
t.equal(result.annotations[0].id, 'existing-1', 'existing should remain');
t.equal(result.annotations[0].label, 'Test', 'existing should not be overwritten');
t.equal(result.annotations[1].id, 'new-1', 'new annotation should be appended');
t.end();
});
test('#mergeAnnotations -> should validate annotation kind', t => {
const state = makeState();
const annotations = [
makeAnnotation({id: 'text', kind: AnnotationKind.TEXT}),
makeAnnotation({id: 'point', kind: AnnotationKind.POINT}),
makeAnnotation({id: 'arrow', kind: AnnotationKind.ARROW}),
makeAnnotation({id: 'circle', kind: AnnotationKind.CIRCLE}),
makeAnnotation({id: 'ruler', kind: AnnotationKind.RULER, endPoint: [1, 1]}),
makeAnnotation({id: 'invalid', kind: 'HEXAGON'})
];
const result = mergeAnnotations(state, annotations);
t.equal(result.annotations.length, 5, 'should accept 5 valid kinds');
const ids = result.annotations.map(a => a.id);
t.ok(ids.includes('text'), 'TEXT is valid');
t.ok(ids.includes('point'), 'POINT is valid');
t.ok(ids.includes('arrow'), 'ARROW is valid');
t.ok(ids.includes('circle'), 'CIRCLE is valid');
t.ok(ids.includes('ruler'), 'RULER is valid');
t.notOk(ids.includes('invalid'), 'HEXAGON is invalid');
t.end();
});
test('#mergeAnnotations -> should validate RULER endPoint', t => {
const state = makeState();
const annotations = [
makeAnnotation({id: 'valid-ruler', kind: AnnotationKind.RULER, endPoint: [1, 2, 3]}),
makeAnnotation({id: 'missing-end', kind: AnnotationKind.RULER}),
makeAnnotation({id: 'bad-end', kind: AnnotationKind.RULER, endPoint: [1, Number.NaN]})
];
const result = mergeAnnotations(state, annotations);
t.equal(result.annotations.length, 1, 'only ruler with a finite 2D/3D endpoint should merge');
t.equal(result.annotations[0].id, 'valid-ruler', 'valid ruler should be preserved');
t.deepEqual(result.annotations[0].endPoint, [1, 2, 3], '3D endpoint should be preserved');
t.equal(result.annotations[0].showCustomText, true, 'missing custom text flag should default on');
t.equal(result.annotations[0].showDistance, true, 'missing distance flag should default on');
t.end();
});
test('#mergeAnnotations -> should not stamp default text placement on legacy annotations', t => {
const state = makeState();
const annotations = [
makeAnnotation({id: 'legacy', angle: 180, textSide: undefined, textVerticalPosition: undefined})
];
// omit the new fields the way saved maps without them look
delete annotations[0].textSide;
delete annotations[0].textVerticalPosition;
const result = mergeAnnotations(state, annotations);
t.equal(result.annotations.length, 1, 'should merge the legacy annotation');
t.notOk(result.annotations[0].textSide, 'should not default textSide');
t.notOk(result.annotations[0].textVerticalPosition, 'should not default textVerticalPosition');
t.equal(result.annotations[0].angle, 180, 'should keep saved angle');
t.end();
});
test('#mergeAnnotations -> should return same state when all annotations are invalid', t => {
const state = makeState();
const annotations = [
{id: 'bad1', kind: 'INVALID', anchorPoint: [0, 0]},
{id: null, kind: AnnotationKind.POINT, anchorPoint: [0, 0]}
];
const result = mergeAnnotations(state, annotations);
t.equal(result, state, 'should return same state reference when no valid annotations');
t.end();
});
test('#mergeAnnotations -> should accept 3D anchorPoint', t => {
const state = makeState();
const annotations = [
makeAnnotation({id: 'xyz', anchorPoint: [-122.4, 37.8, 45]}),
makeAnnotation({id: 'too-long', anchorPoint: [1, 2, 3, 4]}),
makeAnnotation({id: 'nan-z', anchorPoint: [1, 2, Number.NaN]})
];
const result = mergeAnnotations(state, annotations);
t.equal(result.annotations.length, 1, 'should only accept [lng, lat] or [lng, lat, alt]');
t.equal(result.annotations[0].id, 'xyz', 'should keep the 3D annotation');
t.deepEqual(result.annotations[0].anchorPoint, [-122.4, 37.8, 45], 'should keep altitude');
t.end();
});