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
/* eslint-disable max-statements */
import React from 'react';
import {mount} from 'enzyme';
import sinon from 'sinon';
import test from 'tape';
import configureStore from 'redux-mock-store';
import {Provider} from 'react-redux';
import {
MapControlButton,
ToolbarItem,
mapFieldsSelector,
appInjector,
MapContainerFactory,
MapLegendFactory,
MapControlFactory,
MapControlToolbarFactory,
Icons,
MapViewStateContextProvider
} from '@kepler.gl/components';
import {LOCALE_CODES, LOCALES} from '@kepler.gl/localization';
import {toggleMapControl} from '@kepler.gl/actions';
import {keplerGlReducerCore} from '@kepler.gl/reducers';
import {IntlWrapper, mountWithTheme} from '../../../helpers/component-utils';
import {renderWithTheme as renderWithThemeRTL} from '../../../helpers/rtl-utils';
import {
mockKeplerProps,
mockKeplerPropsWithState,
StateWSplitMaps,
StateWFiles
} from '../../../helpers/mock-state';
import {initApplicationConfig} from '@kepler.gl/utils';
const {Cube3d, Split, Legend, DrawPolygon, Delete} = Icons;
const MapControl = appInjector.get(MapControlFactory);
const MapContainer = appInjector.get(MapContainerFactory);
const MapLegend = appInjector.get(MapLegendFactory);
const MapControlToolbar = appInjector.get(MapControlToolbarFactory);
const initialProps = mapFieldsSelector(mockKeplerProps);
const mockStore = configureStore();
test('MapControlFactory - display all options', t => {
const onToggleSplitMap = sinon.spy();
const onTogglePerspective = sinon.spy();
const onToggleMapControl = sinon.spy();
const onSetEditorMode = sinon.spy();
const onToggleEditorVisibility = sinon.spy();
const onSetLocale = sinon.spy();
const $ = mountWithTheme(
<IntlWrapper>
<MapViewStateContextProvider mapState={{latitude: 0, longitude: 0, zoom: 1}}>
<MapControl
mapControls={{
splitMap: {show: true},
visibleLayers: {show: true},
toggle3d: {show: true},
mapLegend: {show: true},
mapDraw: {show: true},
mapLocale: {show: true},
effect: {show: true}
}}
datasets={{}}
layers={[]}
locale={'en'}
layersToRender={{}}
dragRotate={true}
mapIndex={0}
onToggleSplitMap={onToggleSplitMap}
onTogglePerspective={onTogglePerspective}
onToggleMapControl={onToggleMapControl}
onSetEditorMode={onSetEditorMode}
onToggleEditorVisibility={onToggleEditorVisibility}
onSetLocale={onSetLocale}
/>
</MapViewStateContextProvider>
</IntlWrapper>
);
t.equal($.find('.map-control-action').length, 10, 'Should show 10 action panels');
t.equal(
$.find('.toggle-chart-panel').length,
0,
'Should not show a charts button on default MapControl'
);
t.end();
});
test('MapControlFactory - chart control is not a default action', t => {
initApplicationConfig({enableChartsPanel: true});
const $ = mountWithTheme(
<IntlWrapper>
<MapViewStateContextProvider mapState={{latitude: 0, longitude: 0, zoom: 1}}>
<MapControl
mapControls={{
splitMap: {show: true},
visibleLayers: {show: true},
toggle3d: {show: true},
mapLegend: {show: true},
mapDraw: {show: true},
mapLocale: {show: true},
effect: {show: true},
chart: {show: true, active: true}
}}
datasets={{}}
layers={[]}
locale={'en'}
layersToRender={{}}
dragRotate={true}
mapIndex={0}
onToggleSplitMap={() => {}}
onTogglePerspective={() => {}}
onToggleMapControl={() => {}}
onSetEditorMode={() => {}}
onToggleEditorVisibility={() => {}}
onSetLocale={() => {}}
charts={[]}
/>
</MapViewStateContextProvider>
</IntlWrapper>
);
t.equal(
$.find('.toggle-chart-panel').length,
0,
'Should not show a charts button on default MapControl'
);
t.equal(
$.find('.chart-manager').length,
0,
'Should not mount a charts panel on default MapControl'
);
initApplicationConfig({enableChartsPanel: true});
t.end();
});
test('MapControlFactory - viewport JSON control', t => {
initApplicationConfig({enableViewportJsonEditor: true});
const $ = mountWithTheme(
<IntlWrapper>
<MapViewStateContextProvider mapState={{latitude: 0, longitude: 0, zoom: 1}}>
<MapControl
mapControls={{
splitMap: {show: true},
visibleLayers: {show: true},
toggle3d: {show: true},
mapLegend: {show: true},
mapDraw: {show: true},
mapLocale: {show: true},
effect: {show: true},
viewportJson: {show: true}
}}
datasets={{}}
layers={[]}
locale={'en'}
layersToRender={{}}
dragRotate={true}
mapIndex={0}
onToggleSplitMap={() => {}}
onTogglePerspective={() => {}}
onToggleMapControl={() => {}}
onSetEditorMode={() => {}}
onToggleEditorVisibility={() => {}}
onSetLocale={() => {}}
/>
</MapViewStateContextProvider>
</IntlWrapper>
);
t.equal(
$.find('.viewport-json-editor-controls').hostNodes().length,
1,
'Should show the viewport JSON control when enabled'
);
initApplicationConfig({enableViewportJsonEditor: false});
t.end();
});
test('MapControlFactory - display options', t => {
const store = mockStore({
uiState: StateWSplitMaps.uiState
});
let wrapper;
t.doesNotThrow(() => {
wrapper = mountWithTheme(
<Provider store={store}>
<IntlWrapper>
<MapViewStateContextProvider mapState={initialProps.mapState}>
<MapContainer {...initialProps} />
</MapViewStateContextProvider>
</IntlWrapper>
</Provider>
);
}, 'Map Container should not fail without props');
// wrapper
// MapControl
t.equal(wrapper.find(MapControl).length, 1, 'Should render MapControl');
// layer selector is not active
t.equal(wrapper.find(MapControlButton).length, 6, 'Should show 6 MapControlButton');
t.equal(wrapper.find(Split).length, 1, 'Should show 1 split map button');
t.equal(wrapper.find(Cube3d).length, 1, 'Should show 1 toggle 3d button');
t.equal(wrapper.find(Legend).length, 1, 'Should show 1 map legend button');
t.equal(wrapper.find(DrawPolygon).length, 1, 'Should show 1 map draw button');
t.equal(wrapper.find('.map-control-button__locale').length, 1, 'Should show 1 locale button');
// with split map and active layer selector
const propsWithSplitMap = mapFieldsSelector(mockKeplerPropsWithState({state: StateWSplitMaps}));
wrapper.setProps({
children: (
<Provider store={store}>
<IntlWrapper>
<MapViewStateContextProvider mapState={propsWithSplitMap.mapState}>
<MapContainer {...propsWithSplitMap} />
</MapViewStateContextProvider>
</IntlWrapper>
</Provider>
)
});
// legend is opened automatically in split map mode
t.equal(wrapper.find(MapControlButton).length, 6, 'Should show 6 MapControlButton');
t.equal(wrapper.find(Split).length, 0, 'Should show 0 split map split button');
t.equal(wrapper.find(Delete).length, 1, 'Should show 1 split map delete button');
// with 0 mapcontrols
wrapper.setProps({
children: (
<MapViewStateContextProvider mapState={propsWithSplitMap.mapState}>
<MapContainer {...propsWithSplitMap} mapControls={{}} />
</MapViewStateContextProvider>
)
});
t.equal(wrapper.find(MapControlButton).length, 0, 'Should show 0 MapControlButton');
t.end();
});
test('MapContainerFactory - map scale ignores hidden read-only side panel', t => {
const getScaleLeft = readOnly => {
const props = {
...initialProps,
activeSidePanel: 'layer',
sidePanelWidth: 300,
readOnly,
mapStyle: {
bottomMapStyle: {layers: [], name: 'foo'},
visibleLayerGroups: {}
},
mapboxApiAccessToken: 'pyx-11'
};
const store = mockStore({uiState: StateWSplitMaps.uiState});
const {container, unmount} = renderWithThemeRTL(
<Provider store={store}>
<IntlWrapper>
<MapViewStateContextProvider mapState={props.mapState}>
<MapContainer {...props} />
</MapViewStateContextProvider>
</IntlWrapper>
</Provider>
);
const scale = container.querySelector('.map-scale');
const left = window.getComputedStyle(scale.parentElement).left;
unmount();
return left;
};
t.equal(getScaleLeft(true), '12px', 'read-only scale uses the map margin');
t.equal(getScaleLeft(false), '312px', 'editable scale retains the panel offset');
t.end();
});
test('MapControlFactory - click options', t => {
const onToggleSplitMap = sinon.spy();
const onTogglePerspective = sinon.spy();
const onToggleMapControl = sinon.spy();
const onSetEditorMode = sinon.spy();
const onToggleEditorVisibility = sinon.spy();
const onSetLocale = sinon.spy();
const visStateActions = {
setEditorMode: onSetEditorMode,
toggleEditorVisibility: onToggleEditorVisibility
};
const mapStateActions = {
toggleSplitMap: onToggleSplitMap,
togglePerspective: onTogglePerspective
};
const uiStateActions = {
toggleMapControl: onToggleMapControl,
setLocale: onSetLocale
};
// const updateState = keplerGlReducerCore(StateWSplitMaps, toggleMapControl('mapLegend', 0));
const mapContainerProps = mapFieldsSelector(
mockKeplerPropsWithState({
state: StateWSplitMaps,
visStateActions,
mapStateActions,
uiStateActions
})
);
const store = mockStore({
uiState: StateWSplitMaps.uiState
});
let wrapper;
t.doesNotThrow(() => {
wrapper = mountWithTheme(
<IntlWrapper>
<Provider store={store}>
<MapViewStateContextProvider mapState={mapContainerProps.mapState}>
<MapContainer {...mapContainerProps} />
</MapViewStateContextProvider>
</Provider>
</IntlWrapper>
);
}, 'MapContainer should not fail without props');
// layer selector is not active
t.equal(wrapper.find(MapControlButton).length, 6, 'Should show 6 MapControlButton');
t.equal(wrapper.find(Delete).length, 1, 'Should show 1 delete split map button');
// click split Map - now opens mode menu since enableSwipeMode is true
wrapper.find('.map-control-button.split-map').at(0).simulate('click');
// onToggleSplitMap is not called directly when mode menu is available
t.notOk(
onToggleSplitMap.calledOnce,
'should not call onToggleSplitMap when mode menu is enabled'
);
// click toggle3d - now opens the view-mode menu (Top/3D/Globe) instead of
// toggling perspective directly, mirroring the split-map mode menu above.
wrapper.find('.map-control-button.toggle-3d').at(0).simulate('click');
// onTogglePerspective is not called directly when the view-mode menu is available
t.notOk(
onTogglePerspective.calledOnce,
'should not call onTogglePerspective directly when view-mode menu is enabled'
);
// click map legend
wrapper.find('.map-control-button.show-legend').at(0).simulate('click');
t.equal(wrapper.find(MapLegend).length, 1, 'should render MapLegend');
// click map draw
wrapper.find('.map-control-button.map-draw').at(0).simulate('click');
t.equal(wrapper.find(MapLegend).length, 1, 'should render MapLegend');
/*
t.ok(onToggleMapControl.calledOnce, 'should call onToggleMapControl');
t.deepEqual(
onToggleMapControl.args[0],
['mapDraw', 0],
'should call onToggleMapControl with mapDraw'
);
// click locale
wrapper.find('.map-control-button.locale-panel').at(0).simulate('click');
t.ok(onToggleMapControl.calledTwice, 'should call onToggleMapControl');
t.deepEqual(
onToggleMapControl.args[1],
['mapLocale', 0],
'should call onToggleMapControl with mapLocale'
);
// click layer selector
wrapper.find('.map-control-button.toggle-layer').at(0).simulate('click');
t.ok(onToggleMapControl.calledThrice, 'should call onToggleMapControl');
t.deepEqual(
onToggleMapControl.args[2],
['visibleLayers', 0],
'should call onToggleMapControl with visibleLayers'
);
*/
t.end();
});
test('MapControlFactory - show panels', t => {
// show legend
let updateState = keplerGlReducerCore(StateWFiles, toggleMapControl('mapLegend', 0));
let mapContainerProps = mapFieldsSelector(mockKeplerPropsWithState({state: updateState}));
const store = mockStore({
uiState: updateState.uiState
});
let wrapper;
t.doesNotThrow(() => {
wrapper = mountWithTheme(
<IntlWrapper>
<Provider store={store}>
<MapViewStateContextProvider mapState={mapContainerProps.mapState}>
<MapContainer {...mapContainerProps} />
</MapViewStateContextProvider>
</Provider>
</IntlWrapper>
);
}, 'MapContainer should not fail without props');
// show legend
t.equal(wrapper.find(MapLegend).length, 1, 'should render 1 MapLegend');
// show map draw panel
updateState = keplerGlReducerCore(StateWSplitMaps, toggleMapControl('mapDraw', 1));
mapContainerProps = mapFieldsSelector(mockKeplerPropsWithState({state: updateState}));
wrapper.setProps({
children: (
<Provider store={store}>
<MapViewStateContextProvider mapState={mapContainerProps.mapState}>
<MapContainer {...mapContainerProps} index={1} />
</MapViewStateContextProvider>
</Provider>
)
});
t.equal(wrapper.find(MapControlToolbar).length, 1, 'should render 1 MapControlToolbar');
t.equal(
wrapper.find(MapControlToolbar).at(0).find(ToolbarItem).length,
8,
'should render 8 ToolbarItem'
);
// show locale
updateState = keplerGlReducerCore(StateWSplitMaps, toggleMapControl('mapLocale', 1));
mapContainerProps = mapFieldsSelector(mockKeplerPropsWithState({state: updateState}));
wrapper.setProps({
children: (
<Provider store={store}>
<MapViewStateContextProvider mapState={mapContainerProps.mapState}>
<MapContainer {...mapContainerProps} index={1} />
</MapViewStateContextProvider>
</Provider>
)
});
t.equal(wrapper.find(MapControlToolbar).length, 1, 'should render 1 MapControlToolbar');
t.equal(
wrapper.find(MapControlToolbar).at(0).find(ToolbarItem).length,
Object.keys(LOCALE_CODES).length,
`should render ${Object.keys(LOCALE_CODES).length} LOCALE_CODES`
);
Object.keys(LOCALE_CODES).forEach((locale, index) => {
t.equal(
wrapper
.find(MapControlToolbar)
.at(0)
.find(ToolbarItem)
.at(index)
.find('.toolbar-item__title')
.text(),
LOCALES[locale],
'should render correct locale'
);
});
t.end();
});
test('MapControlFactory - map info panel', t => {
const onToggleMapControl = sinon.spy();
const base = {
datasets: {},
layers: [],
locale: 'en',
layersToRender: {},
dragRotate: true,
mapIndex: 0,
onToggleSplitMap: () => {},
onTogglePerspective: () => {},
onToggleMapControl,
onSetEditorMode: () => {},
onToggleEditorVisibility: () => {},
onSetLocale: () => {}
};
const mountControl = extra =>
mountWithTheme(
<IntlWrapper>
<MapViewStateContextProvider mapState={{latitude: 0, longitude: 0, zoom: 1}}>
<MapControl {...base} {...extra} />
</MapViewStateContextProvider>
</IntlWrapper>
);
const empty = mountControl({
mapControls: {mapInfo: {show: true, active: true}},
mapInfo: {title: '', description: ''}
});
t.equal(
empty.find('.map-info-panel').length,
0,
'hides the panel when title and description are empty'
);
t.equal(empty.find('.map-info').length, 0, 'hides the map info button when empty');
t.equal(empty.find('textarea').length, 0, 'does not edit map info on the map');
const shown = mountControl({
mapControls: {mapInfo: {show: true, active: true}},
mapInfo: {title: 'Harbor', description: 'See [routes](https://example.com)'}
});
t.ok(
shown.find('[data-testid="map-info-panel"]').hostNodes().length > 0,
'shows the panel when a title is set'
);
t.ok(shown.text().includes('Harbor'), 'renders the map title');
t.ok(shown.text().includes('routes'), 'renders the description');
t.equal(
shown.find('a[href="https://example.com"]').hostNodes().length,
1,
'renders description links'
);
t.equal(shown.find('textarea').length, 0, 'imported map info is display only');
const withImage = mountControl({
mapControls: {mapInfo: {show: true, active: true}},
mapInfo: {description: ''}
});
t.equal(withImage.find('img').length, 0, 'an imported description does not fetch images');
t.ok(withImage.text().includes('pixel'), 'keeps the image alt text as plain text');
shown.find('button[aria-label="Minimize map info"]').hostNodes().simulate('click');
t.equal(onToggleMapControl.callCount, 1, 'closing the panel toggles map info');
t.equal(onToggleMapControl.firstCall.args[0], 'mapInfo');
const collapsed = mountControl({
mapControls: {mapInfo: {show: true, active: false}},
mapInfo: {title: 'Harbor', description: 'Ferry routes'}
});
const descriptionOnly = mountControl({
mapControls: {mapInfo: {show: true, active: true}},
mapInfo: {description: 'Ferry routes'}
});
t.equal(
descriptionOnly.find('.map-info-title').length,
0,
'a description without a name does not reserve a title row'
);
t.ok(
descriptionOnly.find('.map-info-description--only').hostNodes().length > 0,
'a description without a name starts at the top of the card'
);
t.ok(descriptionOnly.text().includes('Ferry routes'), 'renders a description without a name');
t.equal(collapsed.find('.map-info-panel').length, 0, 'a collapsed panel hides the card');
t.ok(
collapsed.find('.map-info').hostNodes().length > 0,
'a collapsed panel shows the docs button'
);
t.end();
});