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 {
toggleSidePanel,
toggleModal,
toggleSidePanelCloseButton,
openDeleteModal,
openReplaceDatasetModal,
setExportImageSetting,
cleanupExportImage,
setExportVideoSetting,
toggleMapControl,
setMapControlVisibility,
setMapControlSettings,
receiveMapConfig,
setExportSelectedDataset,
setExportDataType,
setExportFiltered,
setExportMapBranding,
setExportMapFileName,
startExportingImage,
addNotification,
removeNotification,
loadFiles,
loadFilesErr,
keplerGlInit
} from '@kepler.gl/actions';
import {
uiStateReducer as reducer,
uiStateReducerFactory,
INITIAL_UI_STATE
} from '@kepler.gl/reducers';
import {
EXPORT_DATA_TYPE,
RESOLUTIONS,
DEFAULT_NOTIFICATION_TOPICS,
DEFAULT_NOTIFICATION_TYPES
} from '@kepler.gl/constants';
test('#uiStateReducer', t => {
t.deepEqual(
reducer(undefined, {}),
{...INITIAL_UI_STATE, initialState: {}},
'should return the initial state'
);
t.end();
});
test('#uiStateReducer -> INIT', t => {
const uiStateReducer = uiStateReducerFactory();
const newState = reducer(
uiStateReducer(undefined, {}),
keplerGlInit({
initialUiState: {readOnly: true}
})
);
t.deepEqual(
newState,
{...INITIAL_UI_STATE, readOnly: true, initialState: {}},
'should apply initialUiState'
);
t.end();
});
test('#uiStateReducerFactory', t => {
const uiStateReducer = uiStateReducerFactory({readOnly: true});
t.deepEqual(
uiStateReducer(undefined, {}),
{...INITIAL_UI_STATE, readOnly: true, initialState: {readOnly: true}},
'should return the initial state'
);
t.end();
});
test('#uiStateReducer -> TOGGLE_SIDE_PANEL', t => {
const newReducer = reducer(INITIAL_UI_STATE, toggleSidePanel('foo'));
const expectedState = {
...INITIAL_UI_STATE,
activeSidePanel: 'foo'
};
t.deepEqual(newReducer, expectedState, 'should update side panel');
const nextState2 = reducer(expectedState, toggleModal(null));
const expectedNextState2 = {
...expectedState,
currentModal: null
};
t.deepEqual(nextState2, expectedNextState2, 'should close modal');
const nextState3 = reducer(expectedState, toggleSidePanel(null));
const expectedNextState3 = {
...expectedState,
activeSidePanel: null
};
t.deepEqual(nextState3, expectedNextState3, 'should close panel');
t.end();
});
test('#uiStateReducer -> TOGGLE_SIDE_PANEL_CLOSE_BUTTON', t => {
const newReducer = reducer(INITIAL_UI_STATE, toggleSidePanelCloseButton(false));
const expectedState = {
...INITIAL_UI_STATE,
isSidePanelCloseButtonVisible: false
};
t.deepEqual(newReducer, expectedState, 'should hide side panel close button');
const nextState2 = reducer(expectedState, toggleSidePanelCloseButton(true));
const expectedNextState2 = {
...expectedState,
isSidePanelCloseButtonVisible: true
};
t.deepEqual(nextState2, expectedNextState2, 'should show side panel close button');
t.end();
});
test('#uiStateReducer -> OPEN_DELETE_MODAL', t => {
const newReducer = reducer(INITIAL_UI_STATE, openDeleteModal('chai'));
const expectedState = {
...INITIAL_UI_STATE,
currentModal: 'deleteData',
datasetKeyToRemove: 'chai'
};
t.deepEqual(newReducer, expectedState, 'should open delete data modal and save key to remove');
t.end();
});
test('#uiStateReducer -> OPEN_REPLACE_DATASET_MODAL', t => {
const opened = reducer(INITIAL_UI_STATE, openReplaceDatasetModal('chai'));
t.equal(opened.currentModal, 'addData', 'should open the add data modal');
t.equal(opened.datasetToReplaceId, 'chai', 'should remember which dataset to replace');
const closed = reducer(opened, toggleModal(null));
t.equal(closed.currentModal, null, 'should close the modal');
t.equal(closed.datasetToReplaceId, null, 'should leave replace mode when the modal closes');
const addData = reducer(opened, toggleModal('addData'));
t.equal(addData.datasetToReplaceId, null, 'Add Data should not stay in replace mode');
t.end();
});
test('#uiStateReducer -> SET_EXPORT_IMAGE_SETTING', t => {
const newReducer = reducer(
INITIAL_UI_STATE,
setExportImageSetting({resolution: RESOLUTIONS.TWO_X})
);
const expectedState = {
...INITIAL_UI_STATE,
exportImage: {
...INITIAL_UI_STATE.exportImage,
resolution: RESOLUTIONS.TWO_X
}
};
t.deepEqual(newReducer, expectedState, 'should set the resolution to TWO_X');
t.end();
});
test('#uiStateReducer -> transparent export image background', t => {
const newReducer = reducer(
INITIAL_UI_STATE,
setExportImageSetting({transparentBackground: true})
);
t.equal(
newReducer.exportImage.transparentBackground,
true,
'should enable the transparent background option'
);
const cleanedReducer = reducer(newReducer, cleanupExportImage());
t.equal(
cleanedReducer.exportImage.transparentBackground,
true,
'should preserve the transparent background option during cleanup'
);
t.end();
});
test('#uiStateReducer -> START_EXPORTING_IMAGE', t => {
const newReducer = reducer(INITIAL_UI_STATE, startExportingImage());
const expectedState = {
...INITIAL_UI_STATE,
exportImage: {
...INITIAL_UI_STATE.exportImage,
exporting: true
}
};
t.deepEqual(newReducer, expectedState, 'should set exporting to true and modal to export image');
t.end();
});
test('#uiStateReducer -> TOGGLE_MAP_CONTROL', t => {
const newReducer = reducer(INITIAL_UI_STATE, toggleMapControl('mapLegend'));
const expectedState = {
...INITIAL_UI_STATE,
mapControls: {
...INITIAL_UI_STATE.mapControls,
mapLegend: {
...INITIAL_UI_STATE.mapControls.mapLegend,
active: !INITIAL_UI_STATE.mapControls.mapLegend.active,
activeMapIndex: 0,
disableEdit: false,
disableClose: false
}
}
};
t.deepEqual(newReducer, expectedState, 'should set map legend to be seen');
t.end();
});
test('#uiStateReducer -> SET_MAP_CONTROL_VISIBILITY', t => {
const newReducer = reducer(INITIAL_UI_STATE, setMapControlVisibility('mapLegend', false));
const expectedMapControl = {
...INITIAL_UI_STATE.mapControls,
mapLegend: {
...INITIAL_UI_STATE.mapControls.mapLegend,
show: false
}
};
t.deepEqual(newReducer.mapControls, expectedMapControl, 'should set map legend show to be false');
const newReducer1 = reducer(newReducer, setMapControlVisibility('mapLegend', true));
const expectedMapControl1 = {
...INITIAL_UI_STATE.mapControls,
mapLegend: {
...INITIAL_UI_STATE.mapControls.mapLegend,
show: true
}
};
t.deepEqual(newReducer1.mapControls, expectedMapControl1, 'should set map legend show to true');
const newReducer2 = reducer(newReducer1, setMapControlVisibility('something', true));
t.equal(newReducer1, newReducer2, 'should note update state');
t.end();
});
test('#uiStateReducer -> SET_EXPORT_SELECTED_DATASET', t => {
const newReducer = reducer(INITIAL_UI_STATE, setExportSelectedDataset('a'));
const expectedState = {
...INITIAL_UI_STATE,
exportData: {
...INITIAL_UI_STATE.exportData,
selectedDataset: 'a'
}
};
t.deepEqual(newReducer, expectedState, 'should set the selectedDataset to a');
t.end();
});
test('#uiStateReducer -> SET_EXPORT_DATA_TYPE', t => {
const newReducer = reducer(INITIAL_UI_STATE, setExportDataType(EXPORT_DATA_TYPE.GEOJSON));
const expectedState = {
...INITIAL_UI_STATE,
exportData: {
...INITIAL_UI_STATE.exportData,
dataType: EXPORT_DATA_TYPE.GEOJSON
}
};
t.deepEqual(newReducer, expectedState, 'should set the dataType to json');
t.end();
});
test('#uiStateReducer -> SET_EXPORT_MAP_FILE_NAME', t => {
const newReducer = reducer(INITIAL_UI_STATE, setExportMapFileName('downtown-map'));
const expectedState = {
...INITIAL_UI_STATE,
exportMap: {
...INITIAL_UI_STATE.exportMap,
fileName: 'downtown-map'
}
};
t.deepEqual(newReducer, expectedState, 'should set the export map file name');
t.end();
});
test('#uiStateReducer -> SET_EXPORT_MAP_BRANDING', t => {
const withAppName = reducer(INITIAL_UI_STATE, setExportMapBranding({appName: 'Atlas Lab'}));
t.deepEqual(
withAppName.exportMap,
{
...INITIAL_UI_STATE.exportMap,
appName: 'Atlas Lab'
},
'should set the application name and preserve existing export fields'
);
const withWebsite = reducer(
withAppName,
setExportMapBranding({
appWebsite: 'https://atlas.example.com',
appIcon: 'https://atlas.example.com/icon.svg',
appNameColor: '#123456'
})
);
t.deepEqual(
withWebsite.exportMap,
{
...INITIAL_UI_STATE.exportMap,
appName: 'Atlas Lab',
appWebsite: 'https://atlas.example.com',
appIcon: 'https://atlas.example.com/icon.svg',
appNameColor: '#123456'
},
'should merge partial branding updates without replacing the application name'
);
t.end();
});
test('#uiStateReducer -> SET_EXPORT_FILTERED', t => {
const newReducer = reducer(INITIAL_UI_STATE, setExportFiltered(false));
const expectedState = {
...INITIAL_UI_STATE,
exportData: {
...INITIAL_UI_STATE.exportData,
filtered: false
}
};
t.deepEqual(newReducer, expectedState, 'should set the filtered to false');
t.end();
});
test('#uiStateReducer -> ADD_NOTIFICATION', t => {
const sharedNotificationId = 'test-notification-id';
const notification1 = {
type: DEFAULT_NOTIFICATION_TYPES.error,
message: 'TEST',
topic: DEFAULT_NOTIFICATION_TOPICS.global,
id: 'test-1',
count: 1
};
const state0 = reducer(INITIAL_UI_STATE, addNotification(notification1));
t.equal(state0.notifications.length, 1, 'AddNotification should add one new notification');
t.deepEqual(
state0.notifications[0],
notification1,
'AddNotification should have propagated data correctly'
);
const notification2 = {
type: DEFAULT_NOTIFICATION_TYPES.info,
message: 'TEST',
topic: DEFAULT_NOTIFICATION_TOPICS.file,
id: sharedNotificationId,
count: 1
};
const state1 = reducer(state0, addNotification(notification2));
t.equal(state1.notifications.length, 2, 'AddNotification should add second notification');
t.deepEqual(
state1.notifications[1],
notification2,
'AddNotification should have propagated data correctly '
);
const updatedNotification = {
type: DEFAULT_NOTIFICATION_TYPES.error,
message: 'TEST-updated-message',
topic: DEFAULT_NOTIFICATION_TOPICS.global,
id: sharedNotificationId,
count: 2
};
const state2 = reducer(state1, addNotification(updatedNotification));
t.equal(
state2.notifications.length,
2,
"addNotification shouldn't add new notification with same id"
);
t.deepEqual(
state2.notifications[1],
updatedNotification,
'AddNotification should have propagated data correctly '
);
t.end();
});
test('#uiStateReducer -> REMOVE_NOTIFICATION', t => {
const newState = reducer(
INITIAL_UI_STATE,
addNotification({
type: DEFAULT_NOTIFICATION_TYPES.error,
message: 'TEST',
topic: DEFAULT_NOTIFICATION_TOPICS.global,
id: 'test-1'
})
);
t.equal(newState.notifications.length, 1, 'AddNotification should add one new notification');
t.deepEqual(
newState.notifications[0],
{
type: DEFAULT_NOTIFICATION_TYPES.error,
message: 'TEST',
topic: DEFAULT_NOTIFICATION_TOPICS.global,
id: 'test-1',
count: 1
},
'AddNotification should have propagated data correctly '
);
const nextState = reducer(newState, removeNotification('test-1'));
t.equal(nextState.notifications.length, 0, 'RemoveNotification removed one notification');
t.end();
});
test('#uiStateReducer -> LOAD_FILES_ERR', t => {
const newState = reducer(INITIAL_UI_STATE, loadFiles());
t.equal(newState.loadFiles.fileLoading, true, 'should set fileLoading to true');
const newState1 = reducer(newState, loadFilesErr('file.csv', new Error('this is an error')));
const expectedId = newState1.notifications.length ? newState1.notifications[0].id : 'error';
t.equal(
newState1.loadFiles.fileLoading,
false,
'should set fileLoading to false when loadFilesErr'
);
t.deepEqual(
newState1.notifications,
[
{
type: 'error',
topic: 'global',
message: 'this is an error',
id: expectedId,
count: 1
}
],
'should add an error notification'
);
t.end();
});
test('#uiStateReducer -> SET_EXPORT_VIDEO_SETTING', t => {
const newState = reducer(INITIAL_UI_STATE, setExportVideoSetting({mediaType: 'webm'}));
const expectedState = {
...INITIAL_UI_STATE,
exportVideo: {
...INITIAL_UI_STATE.exportVideo,
mediaType: 'webm'
}
};
t.deepEqual(newState, expectedState, 'should update mediaType in exportVideo');
const nextState = reducer(
newState,
setExportVideoSetting({fileName: 'my-video', durationMs: 5000})
);
t.deepEqual(
nextState.exportVideo,
{
...INITIAL_UI_STATE.exportVideo,
mediaType: 'webm',
fileName: 'my-video',
durationMs: 5000
},
'should merge multiple settings into exportVideo'
);
const emptyUpdateState = reducer(nextState, setExportVideoSetting({}));
t.deepEqual(
emptyUpdateState.exportVideo,
nextState.exportVideo,
'should keep existing values when updating with empty object'
);
t.end();
});
test('#uiStateReducer -> bookmark visibility preserves navigation layout settings', t => {
const state = reducer(INITIAL_UI_STATE, setMapControlVisibility('cameraBookmarks', false));
t.equal(state.mapControls.cameraBookmarks.show, false, 'hides bookmark navigation');
t.deepEqual(
state.mapControls.cameraBookmarks.settings,
INITIAL_UI_STATE.mapControls.cameraBookmarks.settings,
'keeps drag/resize settings'
);
t.deepEqual(
state.mapControls.mapLegend,
INITIAL_UI_STATE.mapControls.mapLegend,
'does not alter the layer legend'
);
const shown = reducer(state, setMapControlVisibility('cameraBookmarks', true));
t.equal(shown.mapControls.cameraBookmarks.show, true, 'can show it again');
t.end();
});
test('#uiStateReducer -> bookmarks default off and legacy controls can opt in', t => {
const {cameraBookmarks, ...legacyControls} = INITIAL_UI_STATE.mapControls;
t.equal(cameraBookmarks.show, false, 'default bookmark visibility is off');
const initialStates = [
reducer(undefined, {}),
uiStateReducerFactory({mapControls: legacyControls})(undefined, {}),
reducer(INITIAL_UI_STATE, keplerGlInit({initialUiState: {mapControls: legacyControls}}))
];
for (const initial of initialStates) {
t.equal(
initial.mapControls.cameraBookmarks?.show === true,
false,
'missing/default visibility is off'
);
for (const config of [{}, {uiState: {}}, {uiState: {mapControls: {}}}]) {
const loaded = reducer(initial, receiveMapConfig(config));
t.equal(
loaded.mapControls.cameraBookmarks?.show === true,
false,
'legacy load does not opt in'
);
}
const positioned = reducer(
initial,
setMapControlSettings('cameraBookmarks', {contentHeight: 80})
);
t.equal(
positioned.mapControls.cameraBookmarks.show,
false,
'settings alone do not enable bookmarks'
);
t.equal(
positioned.mapControls.cameraBookmarks.settings.contentHeight,
80,
'legacy settings updates work'
);
const enabled = reducer(initial, setMapControlVisibility('cameraBookmarks', true));
t.equal(
enabled.mapControls.cameraBookmarks.show,
true,
'legacy control can be enabled directly'
);
const moved = reducer(enabled, setMapControlSettings('cameraBookmarks', {contentHeight: 90}));
t.equal(moved.mapControls.cameraBookmarks.show, true, 'settings preserve explicit opt-in');
const disabled = reducer(moved, setMapControlVisibility('cameraBookmarks', false));
t.equal(disabled.mapControls.cameraBookmarks.show, false, 'can disable it again');
t.equal(
disabled.mapControls.cameraBookmarks.settings.contentHeight,
90,
'disable keeps layout settings'
);
t.deepEqual(
disabled.mapControls.mapLegend,
initial.mapControls.mapLegend,
'legend is unchanged'
);
}
t.end();
});
test('#uiStateReducer -> explicit bookmark opt-in survives initialization and legacy loads', t => {
const mapControls = {
...INITIAL_UI_STATE.mapControls,
cameraBookmarks: {...INITIAL_UI_STATE.mapControls.cameraBookmarks, show: true}
};
for (const initial of [
uiStateReducerFactory({mapControls})(undefined, {}),
reducer(INITIAL_UI_STATE, keplerGlInit({initialUiState: {mapControls}}))
]) {
t.equal(initial.mapControls.cameraBookmarks.show, true, 'honors explicit enabled state');
for (const config of [{}, {uiState: {}}, {uiState: {mapControls: {}}}]) {
t.equal(
reducer(initial, receiveMapConfig(config)).mapControls.cameraBookmarks.show,
true,
'missing saved preference does not reset explicit opt-in'
);
}
}
t.end();
});
test('#uiStateReducer -> camera animation visibility is independent of bookmarks', t => {
const {cameraTour, ...legacyControls} = INITIAL_UI_STATE.mapControls;
t.equal(cameraTour.show, true, 'enabled initially');
for (const initial of [
INITIAL_UI_STATE,
uiStateReducerFactory({mapControls: legacyControls})(undefined, {}),
reducer(INITIAL_UI_STATE, keplerGlInit({initialUiState: {mapControls: legacyControls}}))
]) {
t.equal(
initial.mapControls.cameraTour?.show !== false,
true,
'missing/default control is enabled'
);
const state = reducer(initial, setMapControlVisibility('cameraTour', false));
const {cameraTour: disabledTour, ...otherControls} = state.mapControls;
t.equal(disabledTour.show, false, 'disables camera animation');
t.deepEqual(otherControls, legacyControls, 'does not change other controls');
const enabled = reducer(state, setMapControlVisibility('cameraTour', true));
t.equal(enabled.mapControls.cameraTour.show, true, 'can enable it again');
}
t.end();
});