Skip to content

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

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

/* eslint-disable max-statements */
import React from 'react';
import sinon from 'sinon';
import test from 'tape';
import {IntlWrapper, mountWithTheme} from 'test/helpers/component-utils';
import {
  ColorSelectorFactory,
  GeocoderConfigFactory,
  GeocoderPanelFactory,
  appInjector,
  testForCoordinates
} from '@kepler.gl/components';
import {cmpDatasetData, cmpObjectKeys} from '../../helpers/comparison-utils';

import {InitialState} from 'test/helpers/mock-state';

const GeocoderPanel = appInjector.get(GeocoderPanelFactory);
const GeocoderConfig = appInjector.get(GeocoderConfigFactory);
const ColorSelector = appInjector.get(ColorSelectorFactory);
const MAPBOX_TOKEN = 'pk.eyJ1Ijoi.d9YD6z';

test('GeocoderPanel - render', t => {
  const enabled = true;
  const updateVisData = sinon.spy();
  const removeDataset = sinon.spy();
  const updateMap = sinon.spy();
  const mockMapState = {
    latitude: 33,
    longitude: 127,
    zoom: 8,
    width: 800,
    height: 800
  };
  const layerOrder = ['layer_1', 'layer_2'];
  const pinColor = [0, 128, 255];
  const mockUiState = InitialState.uiState;

  const mockGeoItem = {
    center: [1, 55],
    text: 'mock',
    bbox: [-1, 50, 4, 65]
  };

  const mockGeoItemWithOutBbox = {
    center: [1, 55],
    text: 'mock'
  };

  const mockPayload = [
    [
      {
        data: {
          fields: [
            {
              name: 'lt',
              id: 'lt',
              displayName: 'lt',
              format: '',
              fieldIdx: 0,
              type: 'integer',
              analyzerType: 'INT',
              valueAccessor: values => values[0]
            },
            {
              name: 'ln',
              id: 'ln',
              displayName: 'ln',
              format: '',
              fieldIdx: 1,
              type: 'integer',
              analyzerType: 'INT',
              valueAccessor: values => values[1]
            },
            {
              name: 'icon',
              id: 'icon',
              displayName: 'icon',
              format: '',
              fieldIdx: 2,
              type: 'string',
              analyzerType: 'STRING',
              valueAccessor: values => values[2]
            },
            {
              name: 'text',
              id: 'text',
              displayName: 'text',
              format: '',
              fieldIdx: 3,
              type: 'string',
              analyzerType: 'STRING',
              valueAccessor: values => values[3]
            }
          ],
          rows: [[55, 1, 'place', 'mock']]
        },
        info: {
          hidden: true,
          id: 'geocoder_dataset',
          label: 'geocoder_dataset'
        }
      }
    ],
    {
      keepExistingConfig: true
    },
    {
      visState: {
        layers: [
          {
            id: 'geocoder_layer',
            type: 'icon',
            config: {
              label: 'Geocoder Layer',
              color: pinColor,
              dataId: 'geocoder_dataset',
              columns: {
                lat: 'lt',
                lng: 'ln',
                icon: 'icon',
                label: 'text'
              },
              isVisible: true,
              hidden: true,
              visConfig: {
                radius: 160
              }
            }
          }
        ],
        layerOrder: ['geocoder_layer']
      }
    }
  ];

  let wrapper;

  t.doesNotThrow(() => {
    wrapper = mountWithTheme(
      <IntlWrapper>
        <GeocoderPanel
          isGeocoderEnabled={enabled}
          mapboxApiAccessToken={MAPBOX_TOKEN}
          mapState={mockMapState}
          uiState={mockUiState}
          updateVisData={updateVisData}
          removeDataset={removeDataset}
          updateMap={updateMap}
          layerOrder={layerOrder}
          color={pinColor}
        />
      </IntlWrapper>
    );
  }, 'Should render');

  t.equal(wrapper.find(GeocoderPanel).length, 1, 'Should display 1 GeoCoderPanel');
  t.equal(wrapper.find('GeoCoder').length, 1, 'Should display 1 Geocoder');

  const geoCoderInstance = wrapper.find('GeoCoder');
  const onSelected = geoCoderInstance.props().onSelected;
  const onDeleteMarker = geoCoderInstance.props().onDeleteMarker;

  onSelected(null, mockGeoItem);
  t.deepEqual(
    removeDataset.args,
    [['geocoder_dataset']],
    'Should call removeDataset on onSelected'
  );

  cmpObjectKeys(
    t,
    [mockPayload],
    updateVisData.args,
    'onSelected payload should have the correct params'
  );
  const actualDatasets = updateVisData.args[0][0];
  const mockDatasets = mockPayload[0];

  mockDatasets.forEach(mockDataset => {
    const {data: mockDatasetData, ...restMockDataset} = mockDataset;
    const {data: actualDatasetData, ...restActualDataset} = actualDatasets[0];
    cmpDatasetData(t, mockDatasetData, actualDatasetData, mockDataset.id);
    t.deepEqual(
      restActualDataset,
      restMockDataset,
      `onSelected options dataset.${mockDataset.id} should be the same`
    );
  });

  t.deepEqual(updateVisData.args[0][1], mockPayload[1], 'onSelected options should be correct');
  t.deepEqual(
    updateVisData.args[0][2],
    mockPayload[2],
    'onSelected configuration should be correct'
  );
  const newVP = updateMap.args[0][0];

  t.deepEqual(
    {latitude: newVP.latitude, longitude: newVP.longitude, zoom: newVP.zoom},
    {latitude: 57.5, longitude: 1.5, zoom: 4.307606395110668},
    'Should call updateMap action on onSelected w/ new viewport'
  );

  t.ok(newVP.transitionInterpolator, 'Should call updateMap action with transitionInterpolator');

  onSelected(null, mockGeoItemWithOutBbox);

  const newVP2 = updateMap.args[1][0];
  t.deepEqual(
    {latitude: newVP2.latitude, longitude: newVP2.longitude, zoom: newVP2.zoom},
    {latitude: 55, longitude: 1, zoom: 11.655698543267773},
    'Should call updateMapaction on onSelected w/o bbox'
  );

  onDeleteMarker();
  t.deepEqual(
    removeDataset.args[1],
    ['geocoder_dataset'],
    'Should be dispatching removeDataset action on removeMarker'
  );

  t.end();
});

test('GeocoderConfig - change pin color', t => {
  const onChange = sinon.spy();
  const color = [255, 0, 0];
  const nextColor = [0, 128, 255];
  const wrapper = mountWithTheme(
    <IntlWrapper>
      <GeocoderConfig config={{limitSearch: false, color}} onChange={onChange} />
    </IntlWrapper>
  );

  const colorSelector = wrapper.find(ColorSelector);
  t.equal(colorSelector.length, 1, 'Should render a pin color selector');
  t.deepEqual(
    colorSelector.props().colorSets[0].selectedColor,
    color,
    'Should display the configured pin color'
  );

  colorSelector.props().colorSets[0].setColor(nextColor);
  t.ok(
    onChange.calledOnce && onChange.calledWith({limitSearch: false, color: nextColor}),
    'Should update the geocoder pin color'
  );

  t.end();
});

test('Geocoder -> testForCoordinates', t => {
  t.deepEqual(
    testForCoordinates('21.22,-138.0'),
    [true, -138.0, 21.22],
    'should recognize valid coordinates'
  );

  t.deepEqual(
    testForCoordinates(' -21.122, -123.4321 '),
    [true, -123.4321, -21.122],
    'should recognize valid coordinates and trim spaces, and a whitespace after the comma'
  );

  t.deepEqual(
    testForCoordinates('san francisco'),
    [false, 'san francisco'],
    'should recognize invalid coordinates'
  );

  t.deepEqual(
    testForCoordinates('91,123'),
    [false, '91,123'],
    'should recognize invalid integer coordinates'
  );

  t.deepEqual(
    testForCoordinates('91.0,-138.0'),
    [false, '91.0,-138.0'],
    'should recognize out of bounds latitude'
  );

  t.deepEqual(
    testForCoordinates('50.0,200.0'),
    [false, '50.0,200.0'],
    'should recognize out of bounds longitude'
  );

  t.end();
});