Skip to content

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

21.3 KB
Raw Blame
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project

import test from 'tape';
import cloneDeep from 'es-toolkit/compat/cloneDeep';
import {cmpFilters, cmpSavedLayers} from 'test/helpers/comparison-utils';
import SchemaManager, {CURRENT_VERSION, visStateSchema} from '@kepler.gl/schemas';

import {
  StateWArcNeighbors,
  StateWEffects,
  StateWFiles,
  StateWFilesFiltersLayerColor,
  StateWLayerCustomColorBreaks,
  StateWTooltipFormat,
  StateWTripGeojson,
  StateWTripTable,
  expectedSavedLayer0,
  expectedLoadedLayer0,
  expectedSavedLayer1,
  expectedLoadedLayer1,
  expectedSavedLayer2,
  expectedLoadedLayer2,
  expectedSavedTripLayer,
  mockColorRange,
  testCsvDataId,
  testGeoJsonDataId
} from 'test/helpers/mock-state';
import {keplerGlReducerCore as keplerGlReducer, validateLayerWithData} from '@kepler.gl/reducers';
import {VisStateActions} from '@kepler.gl/actions';
import {FILTER_VIEW_TYPES} from '@kepler.gl/constants';

const expectedVisStateEntries = [
  'filters',
  'layers',
  'effects',
  'annotations',
  'interactionConfig',
  'layerBlending',
  'overlayBlending',
  'splitMaps',
  'animationConfig',
  'editor',
  'layerOrder',
  'groupBys',
  'joins'
];

test('#visStateSchema -> camera bookmarks are optional and persist only authored fields', t => {
  const camera = {longitude: 10, latitude: 20, zoom: 6, bearing: 30, pitch: 45};
  const bookmarks = [
    {id: 'second', name: 'Second', camera},
    {id: 'first', name: 'First', camera: {...camera, zoom: 8}}
  ];
  const state = cloneDeep(StateWFilesFiltersLayerColor);
  const empty = SchemaManager.getConfigToSave(state);
  t.notOk('cameraBookmarks' in empty.config.visState, 'omits unused bookmarks');
  t.notOk(
    'cameraBookmarks' in SchemaManager.parseSavedConfig(empty).visState,
    'old maps need no bookmarks'
  );
  state.visState.cameraBookmarks = bookmarks.map(bookmark => ({
    ...bookmark,
    selected: true,
    camera: {...bookmark.camera, width: 800, transitionDuration: 1000}
  }));
  const saved = SchemaManager.getConfigToSave(state);
  t.deepEqual(
    saved.config.visState.cameraBookmarks,
    bookmarks,
    'saves names/order and only camera values'
  );
  t.deepEqual(
    SchemaManager.parseSavedConfig(saved).visState.cameraBookmarks,
    bookmarks,
    'round trips'
  );
  saved.config.visState.cameraBookmarks.push(null, {id: 'invalid', camera: {}});
  t.deepEqual(
    SchemaManager.parseSavedConfig(saved).visState.cameraBookmarks,
    bookmarks,
    'ignores invalid entries'
  );
  t.end();
});

test('#visStateSchema -> camera tour is optional and excludes transient state', t => {
  const state = cloneDeep(StateWFilesFiltersLayerColor);
  t.notOk(
    'cameraTour' in SchemaManager.getConfigToSave(state).config.visState,
    'omits unused tours'
  );
  const stop = {
    id: 'first',
    name: 'First',
    camera: {longitude: 10, latitude: 20, zoom: 6, bearing: 30, pitch: 45},
    holdDurationMs: 2000,
    travelDurationMs: 3000
  };
  state.visState.cameraTour = {stops: [{...stop, selected: true}], elapsedMs: 1000, playing: true};
  const saved = SchemaManager.getConfigToSave(state);
  t.deepEqual(
    saved.config.visState.cameraTour,
    {stops: [stop]},
    'saves authored stops and timing only'
  );
  t.deepEqual(
    SchemaManager.parseSavedConfig(saved).visState.cameraTour,
    {stops: [stop]},
    'round trips the tour'
  );
  state.visState.cameraTour = {stops: []};
  t.notOk(
    'cameraTour' in SchemaManager.getConfigToSave(state).config.visState,
    'omits an empty tour'
  );
  t.end();
});

test('#visStateSchema -> v1 -> save layers', t => {
  const initialState = cloneDeep(StateWFilesFiltersLayerColor);

  // save state
  const vsToSave = SchemaManager.getConfigToSave(initialState).config.visState;

  t.deepEqual(
    Object.keys(vsToSave),
    expectedVisStateEntries,
    `visState should have all ${expectedVisStateEntries.length} entries`
  );

  const exptectedSavedLayers = [expectedSavedLayer0, expectedSavedLayer1, expectedSavedLayer2];

  const layersToSave = vsToSave.layers;

  cmpSavedLayers(t, exptectedSavedLayers, layersToSave);
  t.end();
});

test('#visStateSchema -> v1 -> load layers', t => {
  const initialState = cloneDeep(StateWFilesFiltersLayerColor);

  // save state
  const savedState = SchemaManager.getConfigToSave(initialState);
  const vsLoaded = SchemaManager.parseSavedConfig(savedState).visState;

  t.deepEqual(
    Object.keys(vsLoaded),
    expectedVisStateEntries,
    `visState should have all ${expectedVisStateEntries.length} entries`
  );

  const loadedLayers = vsLoaded.layers;

  const expectedLoadedLayers = [expectedLoadedLayer0, expectedLoadedLayer1, expectedLoadedLayer2];

  cmpSavedLayers(t, expectedLoadedLayers, loadedLayers, {id: true});
  t.end();
});

test('#visStateSchema -> v1 -> save load filters', t => {
  const initialState = cloneDeep(StateWFilesFiltersLayerColor);
  initialState.visState.filters[0].inverted = true;
  const savedState = SchemaManager.getConfigToSave(initialState);

  // save state
  const vsToSave = savedState.config.visState;
  const vsLoaded = SchemaManager.parseSavedConfig(savedState).visState;
  const loadedFilters = vsLoaded.filters;

  // test saved filters
  const filtersToSave = vsToSave.filters;

  const expectedSavedFilters = [
    {
      dataId: [testCsvDataId],
      id: 'hjpn8frza',
      enabled: true,
      inverted: true,
      name: ['time'],
      type: 'timeRange',
      value: [1474606800000, 1474617600000],
      view: 'enlarged',
      plotType: {
        interval: '1-hour',
        defaultTimeFormat: 'L  H A',
        type: 'histogram',
        aggregation: 'average'
      },
      yAxis: null,
      animationWindow: 'free',
      speed: 4
    },
    {
      dataId: [testGeoJsonDataId],
      id: 'vpk2466o',
      enabled: true,
      name: ['RATE'],
      type: 'multiSelect',
      value: ['a'],
      view: 'side',
      plotType: {
        type: 'histogram'
      },
      yAxis: null,
      animationWindow: 'free',
      speed: 1
    }
  ];

  cmpFilters(t, expectedSavedFilters, filtersToSave);
  cmpFilters(t, expectedSavedFilters, loadedFilters);

  t.end();
});

test('#visStateSchema -> v1 -> persists opt-in numeric timeline configuration', t => {
  let state = keplerGlReducer(
    cloneDeep(StateWFilesFiltersLayerColor),
    VisStateActions.setFilter(0, 'name', 'gps_data.lat')
  );
  state = keplerGlReducer(
    state,
    VisStateActions.setFilterPlot(0, {plotType: {numericInterval: 0.01}})
  );
  state = keplerGlReducer(state, VisStateActions.setFilterView(0, FILTER_VIEW_TYPES.enlarged));

  const savedState = SchemaManager.getConfigToSave(state);
  const savedFilter = savedState.config.visState.filters[0];
  const loadedFilter = SchemaManager.parseSavedConfig(savedState).visState.filters[0];

  t.equal(savedFilter.view, FILTER_VIEW_TYPES.enlarged, 'should save numeric timeline enablement');
  t.equal(savedFilter.plotType.numericInterval, 0.01, 'should save the numeric bin size');
  t.equal(loadedFilter.view, FILTER_VIEW_TYPES.enlarged, 'should restore numeric enablement');
  t.equal(loadedFilter.plotType.numericInterval, 0.01, 'should restore numeric binning');
  t.notOk(loadedFilter.timezone, 'should not load time-only configuration');
  t.end();
});

test('#visStateSchema -> v1 -> save and validate filters', t => {
  const initialState = cloneDeep(StateWFilesFiltersLayerColor);

  // add empty filter
  const nextStte = keplerGlReducer(initialState, VisStateActions.addFilter(testCsvDataId));
  const savedState = SchemaManager.getConfigToSave(nextStte);

  t.equal(nextStte.visState.filters.length, 3, 'should have 3 filters');
  t.equal(savedState.config.visState.filters.length, 2, 'should only save 2 filters');

  t.end();
});

test('#visStateSchema -> v1 -> save load interaction', t => {
  const initialState = cloneDeep(StateWFilesFiltersLayerColor);
  const savedState = SchemaManager.getConfigToSave(initialState);

  // save state
  const interactionToSave = savedState.config.visState.interactionConfig;
  const interactionLoaded = SchemaManager.parseSavedConfig(savedState).visState.interactionConfig;

  const expectedSaved = {
    tooltip: {
      enabled: true,
      timezone: null,
      compareMode: false,
      compareType: 'absolute',
      fieldsToShow: {
        [testCsvDataId]: [
          {
            name: 'gps_data.utc_timestamp',
            format: 'L LTS'
          },
          {
            name: 'gps_data.types',
            format: null
          },
          {
            name: 'epoch',
            format: 'L LTS'
          },
          {
            name: 'has_result',
            format: null
          },
          {
            name: 'uid',
            format: null
          }
        ],
        [testGeoJsonDataId]: [
          {
            name: 'OBJECTID',
            format: null
          },
          {
            name: 'ZIP_CODE',
            format: null
          },
          {
            name: 'ID',
            format: null
          },
          {
            name: 'TRIPS',
            format: null
          },
          {
            name: 'RATE',
            format: null
          }
        ]
      }
    },
    brush: {
      enabled: false,
      size: 2.5
    },
    coordinate: {
      enabled: false
    },
    geocoder: {
      enabled: false,
      limitSearch: false,
      color: [255, 0, 0]
    },
    legend: {
      enabled: false,
      hideInvisibleLayers: false
    },
    annotation: {
      enabled: false
    },
    zoomOpacity: {
      enabled: false,
      controllers: []
    }
  };

  t.deepEqual(interactionToSave, expectedSaved);
  t.deepEqual(interactionLoaded, expectedSaved);

  t.end();
});

test('#visStateSchema -> v1 -> save load interaction -> tooltip format', t => {
  const baseState = cloneDeep(StateWTooltipFormat);
  const tooltip = baseState.visState.interactionConfig.tooltip;
  const initialState = {
    ...baseState,
    visState: {
      ...baseState.visState,
      interactionConfig: {
        ...baseState.visState.interactionConfig,
        tooltip: {
          ...tooltip,
          config: {
            ...tooltip.config,
            timezone: 'America/Guayaquil'
          }
        }
      }
    }
  };
  const savedState = SchemaManager.getConfigToSave(initialState);

  // save state
  const interactionToSave = savedState.config.visState.interactionConfig;
  const interactionLoaded = SchemaManager.parseSavedConfig(savedState).visState.interactionConfig;

  const expectedSaved = {
    tooltip: {
      enabled: true,
      timezone: 'America/Guayaquil',
      compareMode: true,
      compareType: 'relative',
      fieldsToShow: {
        [testCsvDataId]: [{name: 'gps_data.utc_timestamp', format: 'LL'}],
        [testGeoJsonDataId]: [
          {
            name: 'OBJECTID',
            format: null
          },
          {
            name: 'ZIP_CODE',
            format: null
          },
          {
            name: 'ID',
            format: null
          },
          {
            name: 'TRIPS',
            format: '.3f'
          },
          {
            name: 'RATE',
            format: null
          }
        ]
      }
    },
    brush: {
      enabled: false,
      size: 2.5
    },
    coordinate: {
      enabled: false
    },
    geocoder: {
      enabled: false,
      limitSearch: false,
      color: [255, 0, 0]
    },
    legend: {
      enabled: false,
      hideInvisibleLayers: false
    },
    annotation: {
      enabled: false
    },
    zoomOpacity: {
      enabled: false,
      controllers: []
    }
  };

  t.deepEqual(interactionToSave, expectedSaved);
  t.deepEqual(interactionLoaded, expectedSaved);

  t.end();
});

test('#visStateSchema -> v1 -> save load layerBlending', t => {
  const initialState = cloneDeep(StateWFilesFiltersLayerColor);
  const savedState = SchemaManager.getConfigToSave(initialState);

  // save state
  const layerBlendingToSave = savedState.config.visState.layerBlending;
  const layerBlendingLoaded = SchemaManager.parseSavedConfig(savedState).visState.layerBlending;

  const expectedSaved = 'normal';

  t.deepEqual(layerBlendingToSave, expectedSaved);
  t.deepEqual(layerBlendingLoaded, expectedSaved);

  t.end();
});

test('#visStateSchema -> v1 -> save animation', t => {
  const initialState = cloneDeep(StateWTripGeojson);

  // save state
  const vsToSave = SchemaManager.getConfigToSave(initialState).config.visState;

  t.deepEqual(
    Object.keys(vsToSave),
    expectedVisStateEntries,
    `visState should have all ${expectedVisStateEntries.length} entries`
  );

  const expectedSavedLayers = [expectedSavedTripLayer];
  const expectedAnimationConfig = {
    currentTime: 1565577261000,
    speed: 1,
    timeFormat: null,
    timezone: null
  };
  cmpSavedLayers(t, expectedSavedLayers, vsToSave.layers);

  t.deepEqual(vsToSave.animationConfig, expectedAnimationConfig, 'should save animationConfig');
  t.end();
});

test('visStateSchema -> saving with a null column value does not throw an error', t => {
  const testLayer = cloneDeep(StateWFiles).visState.layers[0];

  // set a column to null
  testLayer.config.columns.altitude = null;

  // test that it doesn't fail ColumnSchemaV1.save
  t.doesNotThrow(() => {
    visStateSchema[CURRENT_VERSION].save({
      layers: [testLayer],
      layerOrder: [testLayer.id]
    });
  }, 'saving with a null column value should not fail');

  t.end();
});

test('#visStateSchema -> v1 -> save load effects', t => {
  const expectedEffects = [
    {
      id: 'e_4',
      type: 'lightAndShadow',
      isEnabled: true,
      parameters: {
        timestamp: 100,
        timezone: 'UTC',
        timeMode: 'pick',
        shadowIntensity: 0.5,
        shadowColor: [0, 0, 0],
        sunLightColor: [255, 255, 255],
        sunLightIntensity: 1,
        ambientLightColor: [255, 255, 255],
        ambientLightIntensity: 1
      }
    },
    {
      id: 'e_3',
      type: 'magnify',
      isEnabled: true,
      parameters: {
        screenXY: [0.5, 0.5],
        radiusPixels: 200,
        zoom: 2,
        borderWidthPixels: 3,
        borderColor: [255, 255, 255, 255]
      }
    },
    {
      id: 'e_2',
      type: 'sepia',
      isEnabled: true,
      parameters: {amount: 0.5}
    },
    {
      id: 'e_1',
      type: 'ink',
      isEnabled: true,
      parameters: {strength: 0.25}
    }
  ];

  const initialState = cloneDeep(StateWEffects);

  const savedState = SchemaManager.getConfigToSave(initialState);
  const savedEffects = savedState.config.visState.effects;

  const loadedState = SchemaManager.parseSavedConfig(savedState);
  const loadedEffects = loadedState.visState.effects;

  t.deepEqual(savedEffects, expectedEffects, 'Effects should be saved as expected');
  t.deepEqual(loadedEffects, expectedEffects, 'Effects should be loaded as expected');

  t.end();
});

test('#visStateSchema -> v1 -> save load effects (deprecated beta config)', t => {
  const deprecatedEffects = [
    {
      id: 'e_5',
      config: {
        type: 'magnify',
        name: 'Magnify',
        isEnabled: false,
        isConfigActive: true,
        params: {
          screenXY: [0, 0],
          radiusPixels: 200,
          zoom: 2,
          borderWidthPixels: 0,
          borderColor: [255, 255, 255, 255]
        }
      }
    }
  ];

  const expectedLoadedEffects = [
    {
      id: 'e_5',
      type: 'magnify',
      isEnabled: false,
      parameters: {
        screenXY: [0, 0],
        radiusPixels: 200,
        zoom: 2,
        borderWidthPixels: 0,
        borderColor: [255, 255, 255, 255]
      }
    }
  ];

  const initialState = cloneDeep(StateWEffects);
  const deprecatedSavedState = SchemaManager.getConfigToSave(initialState);
  deprecatedSavedState.config.visState.effects = deprecatedEffects;
  deprecatedSavedState.config.visState.effectOrder = [deprecatedEffects[0].id];

  const loadedState = SchemaManager.parseSavedConfig(deprecatedSavedState);

  t.deepEqual(
    loadedState.visState.effects,
    expectedLoadedEffects,
    'Effects should be loaded as expected'
  );

  t.end();
});

test('#visStateSchema -> auto create layers', t => {
  const state = cloneDeep(StateWFiles);
  t.equal(state.visState.layers.length, 2, 'should auto create 2 layers');
  t.end();
});

test('#visStateSchema -> color properties', t => {
  const state = cloneDeep(StateWLayerCustomColorBreaks);
  t.equal(state.visState.layers.length, 1, 'should load 1 layer');
  t.deepEqual(
    state.visState.layers[0].config.visConfig.colorRange,
    mockColorRange,
    'should load layer colorRange correctly'
  );
  t.deepEqual(
    state.visState.layers[0].config.colorScale,
    'custom',
    'should load layer color Scale correctly'
  );

  t.deepEqual(
    state.visState.layers[0].config.colorField.name,
    'uid',
    'should load colorField correctly'
  );

  t.end();
});

test('#visStateSchema -> create trip layers', t => {
  const state = cloneDeep(StateWTripTable);
  t.equal(state.visState.layers.length, 1, 'should create 1 trip layer');
  t.equal(state.visState.layers[0].type, 'trip', 'should create trip layer');
  t.end();
});

test('#visStateSchema -> create point and arc layers', t => {
  const state = cloneDeep(StateWArcNeighbors);
  t.equal(state.visState.layers.length, 3, 'should create 3 layers');
  t.equal(state.visState.layers[0].type, 'point', 'should create point layer');
  t.equal(state.visState.layers[1].type, 'arc', 'should create arc layer');
  t.equal(state.visState.layers[2].type, 'arc', 'should create arc layer');
  t.end();
});

test('#visStateSchema -> v1 -> charts are optional', t => {
  const initialState = cloneDeep(StateWFiles);
  const vsToSave = SchemaManager.getConfigToSave(initialState).config.visState;
  t.false('charts' in vsToSave, 'should omit empty charts from saved config');

  const stateWithChart = {
    ...initialState,
    visState: {
      ...initialState.visState,
      charts: [
        {
          id: 'c1',
          type: 'bigNumber',
          title: 'Count',
          dataId: testCsvDataId,
          applyFilters: true,
          display: {isConfigActive: true, isJsonEditorActive: true}
        }
      ]
    }
  };
  const savedConfig = SchemaManager.getConfigToSave(stateWithChart);
  const savedWithChart = savedConfig.config.visState;
  t.equal(savedWithChart.charts.length, 1, 'should persist charts when present');
  t.equal(savedWithChart.charts[0].id, 'c1');
  t.equal(
    savedWithChart.charts[0].display.isConfigActive,
    false,
    'should collapse chart config on save'
  );
  t.equal(
    savedWithChart.charts[0].display.isJsonEditorActive,
    false,
    'should collapse chart JSON editor on save'
  );

  const loaded = SchemaManager.parseSavedConfig(savedConfig).visState;
  t.equal(loaded.charts.length, 1, 'should load saved charts');
  t.end();
});

test('#visStateSchema -> v1 -> save load layer isIncludedInLegend', t => {
  const initialState = cloneDeep(StateWFilesFiltersLayerColor);
  const layer = initialState.visState.layers[0];
  layer.config.isIncludedInLegend = false;

  const savedState = SchemaManager.getConfigToSave(initialState);
  const savedLayers = savedState.config.visState.layers;
  const savedLayer = savedLayers.find(item => item.id === layer.id);
  t.equal(savedLayer.config.isIncludedInLegend, false, 'should persist legend exclusion');

  const untouched = savedLayers.find(item => item.id !== layer.id);
  t.equal(
    Object.prototype.hasOwnProperty.call(untouched.config, 'isIncludedInLegend'),
    false,
    'layers left at the default should omit the flag'
  );

  const loadedLayer = SchemaManager.parseSavedConfig(savedState).visState.layers.find(
    item => item.id === layer.id
  );
  t.equal(loadedLayer.config.isIncludedInLegend, false, 'should load legend exclusion');

  const dataset = initialState.visState.datasets[layer.config.dataId];
  const instance = validateLayerWithData(dataset, loadedLayer, initialState.visState.layerClasses);
  t.ok(instance, 'should rebuild the layer');
  t.equal(instance.config.isIncludedInLegend, false, 'rebuilt layer stays out of the legend');
  t.equal(instance.config.isVisible, true, 'legend exclusion does not hide the layer on the map');
  t.end();
});

test('#visStateSchema -> v1 -> save load hideInvisibleLayers', t => {
  const initialState = cloneDeep(StateWFilesFiltersLayerColor);
  initialState.visState.interactionConfig.legend.config.hideInvisibleLayers = true;

  const savedState = SchemaManager.getConfigToSave(initialState);
  t.equal(
    savedState.config.visState.interactionConfig.legend.hideInvisibleLayers,
    true,
    'should persist hideInvisibleLayers'
  );

  const loaded = SchemaManager.parseSavedConfig(savedState).visState.interactionConfig;
  t.equal(loaded.legend.hideInvisibleLayers, true, 'should load hideInvisibleLayers');
  t.end();
});

test('#visStateSchema -> v1 -> save load zoomOpacity controllers', t => {
  const initialState = cloneDeep(StateWFilesFiltersLayerColor);
  const layerId = initialState.visState.layers[0].id;
  const controllers = [
    {
      id: 'c1',
      layerIds: [layerId],
      stops: {appear: 2, full: 4, fade: 9, gone: 14}
    }
  ];
  initialState.visState.interactionConfig.zoomOpacity.enabled = true;
  initialState.visState.interactionConfig.zoomOpacity.config.controllers = controllers;

  const savedState = SchemaManager.getConfigToSave(initialState);
  const savedZoom = savedState.config.visState.interactionConfig.zoomOpacity;
  t.deepEqual(savedZoom, {enabled: true, controllers}, 'should persist fade-on-zoom controllers');

  const loaded = SchemaManager.parseSavedConfig(savedState).visState.interactionConfig.zoomOpacity;
  t.deepEqual(loaded, {enabled: true, controllers}, 'should load fade-on-zoom controllers');
  t.end();
});