Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[Bug] Fixed json map export and added tests (#956)
Giuseppe Macrìf0d6a7c59b58
diff --git a/src/components/modal-container.js b/src/components/modal-container.jsindex b9b731c39..1c741ce9d 100644--- a/src/components/modal-container.js+++ b/src/components/modal-container.js@@ -177,22 +177,22 @@ export default function ModalContainerFactory(onSuccess: this.props.onExportToCloudSuccess,onError: this.props.onExportToCloudError});- }+ };_onSaveMap = () => {const {currentProvider} = this.props.providerState;const provider = this.props.cloudProviders.find(p => p.name === currentProvider);this._exportFileToCloud({provider, isPublic: false, closeModal: true});- }+ };_onShareMapUrl = (provider) => {this._exportFileToCloud({provider, isPublic: true, closeModal: false});- }+ };_onCloseSaveMap = () => {this.props.providerActions.resetProviderStatus();this._closeModal();- }+ };/* eslint-disable complexity */render() {diff --git a/src/utils/export-utils.js b/src/utils/export-utils.jsindex e1f2202a1..7b6db7a6a 100644--- a/src/utils/export-utils.js+++ b/src/utils/export-utils.js@@ -47,6 +47,14 @@ export const DEFAULT_HTML_NAME = 'kepler.gl.html';export const DEFAULT_JSON_NAME = 'keplergl.json';export const DEFAULT_DATA_NAME = 'kepler-gl';+/**+ * Default json export settings+ * @type {{hasData: boolean}}+ */+export const DEFAULT_EXPORT_JSON_SETTINGS = {+ hasData: true+};+const defaultResolution = EXPORT_IMG_RESOLUTION_OPTIONS.find(op => op.id === RESOLUTIONS.ONE_X);@@ -134,22 +142,35 @@ export function downloadFile(fileBlob, filename) {URL.revokeObjectURL(url);}-export function exportImage(state, options) {- const {imageDataUri} = state.uiState.exportImage+export function exportImage(state) {+ const {imageDataUri} = state.uiState.exportImage;if (imageDataUri) {const file = dataURItoBlob(imageDataUri);downloadFile(file, DEFAULT_IMAGE_NAME);}}-export function exportJson(state, options) {+export function exportToJsonString(data) {+ try {+ return JSON.stringify(data)+ } catch (e) {+ return e.description;+ }+}++export function getMapJSON(state, options = DEFAULT_EXPORT_JSON_SETTINGS) {const {hasData} = options;- const data = hasData+ return hasData? KeplerGlSchema.save(state): KeplerGlSchema.getConfigToSave(state);+}++export function exportJson(state, options = {}) {++ const map = getMapJSON(state, options);- const fileBlob = new Blob([data], {type: 'application/json'});+ const fileBlob = new Blob([exportToJsonString(map)], {type: 'application/json'});downloadFile(fileBlob, DEFAULT_JSON_NAME);}@@ -157,7 +178,7 @@ export function exportHtml(state, options) {const {userMapboxToken, exportMapboxAccessToken, mode} = options;const data = {- ...KeplerGlSchema.save(state),+ ...getMapJSON(state),mapboxApiAccessToken:(userMapboxToken || '') !== ''? userMapboxToken@@ -202,11 +223,11 @@ export function exportData(state, option) {});}-export function exportMap(state, option) {- const mapToState = KeplerGlSchema.save(state);+export function exportMap(state) {+ const mapToState = getMapJSON(state);const {mapInfo} = state.visState;const {imageDataUri} = state.uiState.exportImage;- const thumbnail = imageDataUri ? dataURItoBlob(imageDataUri) : null+ const thumbnail = imageDataUri ? dataURItoBlob(imageDataUri) : null;return {map: mapToState,diff --git a/test/node/reducers/composer-state-test.js b/test/node/reducers/composer-state-test.jsindex 17d2bc3d7..0a0e32e4a 100644--- a/test/node/reducers/composer-state-test.js+++ b/test/node/reducers/composer-state-test.js@@ -40,7 +40,6 @@ import {cmpInteraction} from 'test/helpers/comparison-utils';import {INITIAL_UI_STATE} from 'reducers/ui-state-updaters';-import {toggleMapControlUpdater} from 'reducers/map-state-updaters';const mockRawData = {fields: [diff --git a/test/node/utils/export-utils-test.js b/test/node/utils/export-utils-test.jsnew file mode 100644index 000000000..0e3d23772--- /dev/null+++ b/test/node/utils/export-utils-test.js@@ -0,0 +1,21 @@+import test from 'tape';+import {registerEntry} from 'actions/identity-actions';+import keplerGlReducer from 'reducers';+import {getMapJSON, exportToJsonString} from 'utils/export-utils';++test('exportUtils -> ExportJson', t => {+ const state = keplerGlReducer(undefined, registerEntry({id: 'test'})).test;+ const body = exportToJsonString(getMapJSON(state));++ t.equal(+ typeof body,+ 'string',+ 'Should validate the type of body to be a string'+ );++ t.doesNotThrow(() => {+ JSON.parse(body)+ }, 'Should not throw when trying to parse body');++ t.end();+});diff --git a/test/node/utils/index.js b/test/node/utils/index.jsindex 2a4354813..97e6dbc1f 100644--- a/test/node/utils/index.js+++ b/test/node/utils/index.js@@ -30,3 +30,4 @@ import './notifications-utils-test';import './aggregate-utils-test';import './color-util-test';import './util-test';+import './export-utils-test';