Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

fix(export): preserve config-only JSON exports (#3850)

Igor Dykhta1f139a677d85
diff --git a/src/utils/src/export-utils.spec.ts b/src/utils/src/export-utils.spec.ts
new file mode 100644
index 000000000..f03448744
--- /dev/null
+++ b/src/utils/src/export-utils.spec.ts
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import {exportJson} from './export-utils';
+
+function readJsonBlob(blob: Blob): Promise<unknown> {
+ return new Promise((resolve, reject) => {
+ const reader = new FileReader();
+ reader.addEventListener('load', () => resolve(JSON.parse(reader.result as string)));
+ reader.addEventListener('error', () => reject(reader.error));
+ reader.readAsText(blob);
+ });
+}
+
+describe('exportJson', () => {
+ test('exports config-only and data-inclusive maps with their intended schemas', async () => {
+ const configOnlyMap = {
+ version: 'v1',
+ config: {visState: {layers: []}}
+ };
+ const dataInclusiveMap = {
+ datasets: [],
+ config: configOnlyMap,
+ info: {app: 'kepler.gl'}
+ };
+ const state = {
+ visState: {
+ schema: {
+ getConfigToSave: jest.fn(() => configOnlyMap),
+ save: jest.fn(() => dataInclusiveMap)
+ }
+ }
+ };
+ const saveBlob = jest.fn();
+ Object.defineProperty(window.navigator, 'msSaveOrOpenBlob', {
+ configurable: true,
+ value: saveBlob
+ });
+
+ exportJson(state, {hasData: false});
+
+ expect(await readJsonBlob(saveBlob.mock.calls[0][0])).toEqual(configOnlyMap);
+
+ exportJson(state, {hasData: true});
+
+ expect(await readJsonBlob(saveBlob.mock.calls[1][0])).toEqual({
+ ...dataInclusiveMap,
+ info: {
+ ...dataInclusiveMap.info,
+ appName: 'kepler.gl',
+ appWebsite: 'http://kepler.gl/',
+ source: 'kepler.gl'
+ }
+ });
+ });
+});
diff --git a/src/utils/src/export-utils.ts b/src/utils/src/export-utils.ts
index 697dc5815..afe2972a5 100644
--- a/src/utils/src/export-utils.ts
+++ b/src/utils/src/export-utils.ts
@@ -264,7 +264,9 @@ export function exportJson(state, options: any = {}) {
const appName = options.appName || state.appName || KEPLER_GL_NAME;
const appWebsite = options.appWebsite ?? state.appWebsite ?? KEPLER_GL_WEBSITE;
const map = getMapJSON(state, {...options, appName, appWebsite});
- map.info.source = 'kepler.gl';
+ if (map.info) {
+ map.info.source = 'kepler.gl';
+ }
const fileBlob = new Blob([exportToJsonString(map)], {type: 'application/json'});
const fileName = getExportFileName(
options.fileName,