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 React from 'react';
import test from 'tape';
import {mount} from 'enzyme';
import cloneDeep from 'es-toolkit/compat/cloneDeep';
import sinon from 'sinon';
import {
MapLegendFactory,
StyledMapControlLegend,
VisualChannelMetric,
LayerDefaultLegend,
SingleColorLegendFactory,
LayerColorLegendFactory,
LegendRowFactory,
ResetColorLabelFactory,
appInjector
} from '@kepler.gl/components';
import {
StateWFilesFiltersLayerColor,
expectedSavedLayer1 as pointLayer,
expectedSavedLayer0 as hexagonLayer,
expectedSavedLayer2 as geojsonLayer
} from 'test/helpers/mock-state';
import {IntlWrapper, mountWithTheme} from 'test/helpers/component-utils';
import {KeplerGlLayers} from '@kepler.gl/layers';
const {PointLayer, WMSLayer, HeatmapLayer} = KeplerGlLayers;
const MapLegend = appInjector.get(MapLegendFactory);
const LayerColorLegend = appInjector.get(LayerColorLegendFactory);
const SingleColorLegend = appInjector.get(SingleColorLegendFactory);
const LegendRow = appInjector.get(LegendRowFactory);
const ResetColorLabel = appInjector.get(ResetColorLabelFactory);
test('Components -> MapLegend.render', t => {
t.doesNotThrow(() => {
mount(<MapLegend />);
}, 'Show not fail without data');
t.end();
});
test('Components -> MapLegend.render -> with layers', t => {
const initialState = cloneDeep(StateWFilesFiltersLayerColor);
const onLayerVisConfigChange = sinon.spy();
const {layers} = initialState.visState;
let wrapper;
t.doesNotThrow(() => {
wrapper = mountWithTheme(
<IntlWrapper>
<MapLegend layers={layers} onLayerVisConfigChange={onLayerVisConfigChange} />
</IntlWrapper>
);
}, 'Show not fail with layers');
t.equal(
wrapper.find(StyledMapControlLegend).length,
layers.length,
'should render 3 layer legends'
);
const geojsonLegend = wrapper.find(StyledMapControlLegend).at(1);
const hexagonLegend = wrapper.find(StyledMapControlLegend).at(2);
const pointLegend = wrapper.find(StyledMapControlLegend).at(0);
testGeojsonLegend(t, geojsonLegend);
testHexagonLayerLegend(t, hexagonLegend);
testPointLayerLegend(t, pointLegend, onLayerVisConfigChange);
t.end();
});
function testGeojsonLegend(t, geojsonLegend) {
t.equal(
geojsonLegend.find('.legend--layer_name').at(0).text(),
geojsonLayer.config.label,
'geojson layer legend should render label'
);
t.equal(
geojsonLegend.find(LayerColorLegend).length,
2,
'geojson layer legend should render 2 color legend'
);
t.equal(
geojsonLegend.find(LayerDefaultLegend).length,
0,
'geojson layer legend should render 1 size legend'
);
const fillColorLegend = geojsonLegend.find(LayerColorLegend).at(0);
const strokeColorLegend = geojsonLegend.find(LayerColorLegend).at(1);
t.equal(
fillColorLegend.find(SingleColorLegend).length,
1,
'geojson fill color should render SingleColorLegend'
);
t.deepEqual(
fillColorLegend.find(SingleColorLegend).at(0).props().color,
geojsonLayer.config.color,
'geojson color legend should be correct color'
);
t.equal(
strokeColorLegend.find(SingleColorLegend).length,
1,
'geojson stroke color should render SingleColorLegend'
);
t.deepEqual(
strokeColorLegend.find(SingleColorLegend).at(0).props().color,
geojsonLayer.config.visConfig.strokeColor,
'geojson color legend should be correct color'
);
}
function testPointLayerLegend(t, pointLegend, onLayerVisConfigChange) {
// layer[0] point layer
// color by: gps_data.types
// point layer has 2 color channels, only fill is enabled
t.equal(
pointLegend.find('.legend--layer_name').at(0).text(),
pointLayer.config.label,
'point layer legend should render point label'
);
t.equal(
pointLegend.find(LayerColorLegend).length,
1,
'point layer legend should render 1 point layer color legend'
);
t.equal(
pointLegend.find(LayerDefaultLegend).length,
0,
'point layer legend should render 0 point layer size legend'
);
const pointColorLegend = pointLegend.find(LayerColorLegend).at(0);
t.equal(
pointColorLegend.find(VisualChannelMetric).length,
1,
'point layer legend should render metric name'
);
t.equal(
pointColorLegend
.find(VisualChannelMetric)
.at(0)
.find('.legend--layer_color_field')
.at(0)
.text(),
pointLayer.visualChannels.colorField.name,
'point layer legend should render color by measure: gps_data.types'
);
// colors: ['#00939C', '#6BB5B9', '#AAD7D9', '#E6FAFA']
// colorDomain ["driver_analytics", "driver_analytics_0", "driver_gps"]
t.equal(pointLegend.find(LegendRow).length, 3, 'Should render 3 legends');
const expectedLegend = [
['#00939C', 'driver_analytics'],
['#6BB5B9', 'driver_analytics_0'],
['#AAD7D9', 'driver_gps']
];
for (let i = 0; i < 3; i++) {
const rect = pointLegend.find(LegendRow).at(i).find('.legend-row-color').at(0);
t.equal(
rect.props().style.backgroundColor,
expectedLegend[i][0],
'should render correct legend color'
);
const input = pointLegend.find(LegendRow).at(i).find('input').at(0);
t.equal(input.props().value, expectedLegend[i][1], 'should render correct legend label');
}
// test change input
const firstLegendInput = pointLegend.find(LegendRow).at(0).find('input');
const event = {target: {name: 'input-legend-label', value: 'taro'}};
firstLegendInput.simulate('change', event);
t.ok(onLayerVisConfigChange.calledOnce, 'should call onLayerVisConfigChange');
t.ok(onLayerVisConfigChange.args[0][0] instanceof PointLayer, 'first arg should be Layer');
t.deepEqual(
Object.keys(onLayerVisConfigChange.args[0][1]),
['colorRange'],
'second arg should be colorRange'
);
t.deepEqual(
onLayerVisConfigChange.args[0][1].colorRange.colorLegends,
{'#00939C': 'taro'},
'second arg should contain colorLegends'
);
}
function testHexagonLayerLegend(t, hexagonLegend) {
t.equal(
hexagonLegend.find('.legend--layer_name').at(0).text(),
hexagonLayer.config.label,
'hexagon layer legend should render label'
);
t.equal(
hexagonLegend.find(LayerColorLegend).length,
1,
'hexagon layer legend should render 1 color legend'
);
t.equal(
hexagonLegend.find(LayerDefaultLegend).length,
0,
'hexagon layer legend should render 0 size legend'
);
const hexagonColorLegend = hexagonLegend.find(LayerColorLegend).at(0);
t.equal(
hexagonColorLegend.find(VisualChannelMetric).length,
1,
'hexagon layer legend should render metric name'
);
t.equal(
hexagonColorLegend
.find(VisualChannelMetric)
.at(0)
.find('.legend--layer_color_field')
.at(0)
.text(),
'Point Count',
'hexagon layer legend should render color by Point Count'
);
}
test('Components -> MapLegend.render -> with colorLegends', t => {
const initialState = cloneDeep(StateWFilesFiltersLayerColor);
const onLayerVisConfigChange = sinon.spy();
const {layers} = initialState.visState;
const ptLayer = layers[0];
ptLayer.config.visConfig.colorRange = {
...ptLayer.config.visConfig.colorRange,
colorLegends: {
'#00939C': 'taro'
}
};
let wrapper;
t.doesNotThrow(() => {
wrapper = mountWithTheme(
<IntlWrapper>
<MapLegend layers={[ptLayer]} onLayerVisConfigChange={onLayerVisConfigChange} />
</IntlWrapper>
);
}, 'Show not fail with layers');
t.equal(wrapper.find(StyledMapControlLegend).length, 1, 'should render 1 layer legends');
const pointLegend = wrapper.find(StyledMapControlLegend).at(0);
const firstLegend = pointLegend.find(LegendRow).at(0);
const input = firstLegend.find('input').at(0);
t.equal(input.props().value, 'taro', 'should render custom legend label');
t.equal(firstLegend.find(ResetColorLabel).length, 1, 'should render reset');
// click reset
firstLegend.find(ResetColorLabel).at(0).simulate('click');
t.ok(onLayerVisConfigChange.calledOnce, 'should call onLayerVisConfigChange');
t.ok(onLayerVisConfigChange.args[0][0] instanceof PointLayer, 'first arg should be Layer');
t.deepEqual(
Object.keys(onLayerVisConfigChange.args[0][1]),
['colorRange'],
'second arg should be colorRange'
);
t.deepEqual(
onLayerVisConfigChange.args[0][1].colorRange.colorLegends,
{},
'second arg should be empty'
);
t.end();
});
test('Components -> MapLegend.render -> WMS legend image', t => {
const wmsLayer = new WMSLayer({
id: 'wms-legend',
dataId: 'wms-dataset',
label: 'WMS legend layer',
isVisible: true
});
wmsLayer.updateLayerVisConfig({
wmsLayer: {
name: 'OSM-WMS',
title: 'OpenStreetMap',
boundingBox: [
[-180, -90],
[180, 90]
],
queryable: true,
legendUrl: 'https://example.com/legend.png'
}
});
let wrapper;
t.doesNotThrow(() => {
wrapper = mountWithTheme(
<IntlWrapper>
<MapLegend layers={[wmsLayer]} />
</IntlWrapper>
);
}, 'Should not fail with a WMS layer');
t.equal(wrapper.find(StyledMapControlLegend).length, 1, 'should render 1 layer legend');
t.equal(
wrapper.find('.legend--layer_name').at(0).text(),
'WMS legend layer',
'should render WMS layer label'
);
t.equal(wrapper.find('img.legend--layer_image').length, 1, 'should render WMS legend image');
t.equal(
wrapper.find('img.legend--layer_image').at(0).props().src,
'https://example.com/legend.png',
'should use GetLegendGraphic / LegendURL'
);
t.equal(wrapper.find(LayerColorLegend).length, 0, 'should not render fill color legend for WMS');
t.end();
});
test('Components -> MapLegend.render -> heatmap color scale', t => {
const heatmapLayer = new HeatmapLayer({
id: 'heatmap-legend',
dataId: 'heatmap-dataset',
label: 'Heatmap legend layer',
isVisible: true
});
heatmapLayer.updateLayerConfig({
columns: {
lat: {value: 'lat', fieldIdx: 0},
lng: {value: 'lng', fieldIdx: 1}
}
});
let wrapper;
t.doesNotThrow(() => {
wrapper = mountWithTheme(
<IntlWrapper>
<MapLegend layers={[heatmapLayer]} />
</IntlWrapper>
);
}, 'Should not fail with a heatmap layer');
t.equal(wrapper.find(StyledMapControlLegend).length, 1, 'should render 1 layer legend');
t.equal(wrapper.find(LayerColorLegend).length, 1, 'should render heatmap color legend');
t.equal(
wrapper.find('.legend--layer_color_field').at(0).text().toLowerCase(),
'density',
'should label heatmap legend as density'
);
t.ok(
wrapper.find('.legend--color-ramp__segment').length >= 2,
'should render a horizontal color ramp'
);
t.equal(
wrapper.find('.legend--color-ramp__min').at(0).text(),
'Min',
'left end of the ramp should be labeled Min'
);
t.equal(
wrapper.find('.legend--color-ramp__max').at(0).text(),
'Max',
'right end of the ramp should be labeled Max'
);
t.equal(
wrapper.find(LayerDefaultLegend).length,
0,
'should not render weight by density when no weight field is set'
);
t.end();
});
test('Components -> MapLegend.render -> heatmap weight field', t => {
const heatmapLayer = new HeatmapLayer({
id: 'heatmap-legend-weight',
dataId: 'heatmap-dataset',
label: 'Heatmap weight legend',
isVisible: true
});
heatmapLayer.updateLayerConfig({
columns: {
lat: {value: 'lat', fieldIdx: 0},
lng: {value: 'lng', fieldIdx: 1}
},
weightField: {name: 'trip_distance', type: 'real'}
});
let wrapper;
t.doesNotThrow(() => {
wrapper = mountWithTheme(
<IntlWrapper>
<MapLegend layers={[heatmapLayer]} />
</IntlWrapper>
);
}, 'Should not fail with a weighted heatmap layer');
t.equal(wrapper.find(LayerColorLegend).length, 1, 'should still render heatmap color legend');
t.equal(wrapper.find(LayerDefaultLegend).length, 1, 'should render weight legend');
t.ok(
wrapper.find(LayerDefaultLegend).at(0).text().toLowerCase().includes('trip_distance'),
'weight legend should show the selected field name'
);
t.end();
});
test('Components -> MapLegend.render -> legend inclusion', t => {
const initialState = cloneDeep(StateWFilesFiltersLayerColor);
const {layers} = initialState.visState;
const excluded = Object.assign(Object.create(Object.getPrototypeOf(layers[0])), layers[0], {
config: {...layers[0].config, isIncludedInLegend: false}
});
const hiddenLayerWrapper = mountWithTheme(
<IntlWrapper>
<MapLegend layers={[excluded, layers[1], layers[2]]} />
</IntlWrapper>
);
t.equal(
hiddenLayerWrapper.find(StyledMapControlLegend).length,
2,
'should omit a layer excluded from the legend'
);
const names = hiddenLayerWrapper.find('.legend--layer_name').map(node => node.text());
t.equal(names.includes(excluded.config.label), false, 'excluded layer label should not render');
t.equal(
hiddenLayerWrapper.find('.legend__include-toggle').hostNodes().length,
0,
'should not render a hide-from-legend icon'
);
const groupOrder = [
{
id: 'legend-group',
label: 'Hidden group',
isVisible: true,
isIncludedInLegend: false,
layerOrder: [layers[1].id]
},
layers[2].id
];
const groupWrapper = mountWithTheme(
<IntlWrapper>
<MapLegend layers={layers} layerOrder={groupOrder} />
</IntlWrapper>
);
t.equal(
groupWrapper.find(StyledMapControlLegend).length,
1,
'should omit layers inside a group excluded from the legend'
);
t.equal(
groupWrapper.find('.legend--layer_name').at(0).text(),
layers[2].config.label,
'should keep layers outside the excluded group'
);
const invisible = Object.assign(Object.create(Object.getPrototypeOf(layers[1])), layers[1], {
config: {...layers[1].config, isVisible: false}
});
const withInvisible = mountWithTheme(
<IntlWrapper>
<MapLegend layers={[invisible, layers[2]]} />
</IntlWrapper>
);
t.equal(
withInvisible.find(StyledMapControlLegend).length,
2,
'should keep invisible layers in the legend by default'
);
const hidingInvisible = mountWithTheme(
<IntlWrapper>
<MapLegend layers={[invisible, layers[2]]} hideInvisibleLayers />
</IntlWrapper>
);
t.equal(
hidingInvisible.find(StyledMapControlLegend).length,
1,
'should omit invisible layers when hideInvisibleLayers is on'
);
t.equal(
hidingInvisible.find('.legend--layer_name').at(0).text(),
layers[2].config.label,
'should keep visible layers when hideInvisibleLayers is on'
);
t.end();
});