// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
/* eslint-disable max-statements */
import React from 'react';
import {fireEvent, within} from '@testing-library/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,
ChartControlFactory,
EffectControlFactory,
MapControlToolbarFactory,
Icons,
RootContext,
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 {getApplicationConfig, initApplicationConfig} from '@kepler.gl/utils';
import EffectAnimationPreviewProvider from '../../../../src/components/src/effects/effect-animation-preview';
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 - registered animation action opens without existing animations', t => {
const previous = getApplicationConfig().enableAnimationEditor;
initApplicationConfig({enableAnimationEditor: true});
const root = document.createElement('div');
document.body.appendChild(root);
const view = renderWithThemeRTL(
,
{container: root, baseElement: root}
);
try {
const map = within(root);
const buttons = map.queryAllByRole('button', {name: 'Animation'});
t.equal(buttons.length, 1, 'the app injector includes one Animation action in this map');
if (buttons.length === 1) {
fireEvent.click(buttons[0]);
const panel = map.queryByRole('region', {name: 'Animation panel'});
t.ok(panel, 'opens the animation panel through the registered factory dependencies');
if (panel) {
t.ok(
within(panel).queryByRole('button', {name: 'Add action'}),
'the editor can create the first action on a map without animations'
);
}
}
} finally {
initApplicationConfig({enableAnimationEditor: previous});
view.unmount();
root.remove();
}
t.end();
});
test('MapControlFactory - memo preserves custom actions and their camera props', t => {
let renders = 0;
const Action = ({mapState, layers, extra}) => {
renders++;
return ;
};
const props = {
mapState: initialProps.mapState,
layers: [],
mapControls: {},
actionComponents: [Action],
extra: 'custom'
};
const tree = next => (
);
const view = renderWithThemeRTL(tree(props), {
baseElement: document.createElement('div')
});
t.ok(
Array.isArray(MapControl.defaultActionComponents),
'retains defaultActionComponents for extensions'
);
view.rerender(tree(props));
t.equal(renders, 1, 'skips unchanged action props');
const layers = [{id: 'a', config: {visConfig: {opacity: 0.4}}}];
view.rerender(tree({...props, mapState: {...props.mapState, longitude: 42}, layers}));
t.deepEqual(
JSON.parse(view.container.querySelector('output').textContent),
{longitude: 42, layers, extra: 'custom'},
'forwards full camera, layer and custom inputs'
);
view.rerender(tree({...props, actionComponents: []}));
t.equal(view.container.querySelector('output'), null, 'accepts action removal');
view.unmount();
t.end();
});
test('map action buttons - ignore camera props and keep visibility and callbacks live', t => {
initApplicationConfig({enableChartsPanel: true});
for (const [factory, id] of [
[ChartControlFactory, 'chart'],
[EffectControlFactory, 'effect']
]) {
let renders = 0;
const Tooltip = ({children, message}) => {
renders++;
return
{children}
;
};
const Control = factory(Tooltip);
const onToggleMapControl = sinon.spy();
const props = {mapControls: {[id]: {show: true, active: false}}, onToggleMapControl};
const view = renderWithThemeRTL(, {
baseElement: document.createElement('div')
});
view.rerender();
t.equal(renders, 1, `${id}: camera changes do not render the button`);
fireEvent.click(view.container.querySelector('button'));
t.deepEqual(onToggleMapControl.firstCall.args, [id], `${id}: toggles the right control`);
const replacement = sinon.spy();
view.rerender(
);
t.equal(renders, 2, `${id}: active state and callback changes reach the button`);
fireEvent.click(view.container.querySelector('button'));
t.deepEqual(replacement.firstCall.args, [id], `${id}: calls the replacement handler`);
if (id === 'chart') {
initApplicationConfig({enableChartsPanel: false});
view.rerender();
t.equal(view.container.querySelector('button'), null, 'honors chart feature flag changes');
initApplicationConfig({enableChartsPanel: true});
}
view.rerender();
t.equal(view.container.querySelector('button'), null, `${id}: hides a removed control`);
view.unmount();
}
t.end();
});
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(
);
t.equal($.find('.map-control-action').length, 11, 'Should show 11 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(
{}}
onTogglePerspective={() => {}}
onToggleMapControl={() => {}}
onSetEditorMode={() => {}}
onToggleEditorVisibility={() => {}}
onSetLocale={() => {}}
charts={[]}
/>
);
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(
{}}
onTogglePerspective={() => {}}
onToggleMapControl={() => {}}
onSetEditorMode={() => {}}
onToggleEditorVisibility={() => {}}
onSetLocale={() => {}}
/>
);
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(
);
}, '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: (
)
});
// 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: (
)
});
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(
);
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(
);
}, '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(
);
}, '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: (
)
});
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: (
)
});
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(
);
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();
});