Skip to content

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

[Enhancement] pass dataset to renderLayer function (#1341)

Shan He06ea669dea5c
diff --git a/src/components/side-panel/layer-panel/layer-configurator.js b/src/components/side-panel/layer-panel/layer-configurator.js
index d2c014905..aafde97c8 100644
--- a/src/components/side-panel/layer-panel/layer-configurator.js
+++ b/src/components/side-panel/layer-panel/layer-configurator.js
@@ -60,7 +60,10 @@ const StyledLayerVisualConfigurator = styled.div.attrs({
`;
export const getLayerFields = (datasets, layer) =>
- datasets[layer.config.dataId] ? datasets[layer.config.dataId].fields : [];
+ layer.config && datasets[layer.config.dataId] ? datasets[layer.config.dataId].fields : [];
+
+export const getLayerDataset = (datasets, layer) =>
+ layer.config && datasets[layer.config.dataId] ? datasets[layer.config.dataId] : null;
export const getLayerConfiguratorProps = props => ({
layer: props.layer,
@@ -906,7 +909,7 @@ export default function LayerConfiguratorFactory(
const visConfiguratorProps = getVisConfiguratorProps(this.props);
const layerConfiguratorProps = getLayerConfiguratorProps(this.props);
const layerChannelConfigProps = getLayerChannelConfigProps(this.props);
-
+ const dataset = getLayerDataset(datasets, layer);
const renderTemplate = layer.type && `_render${capitalizeFirstLetter(layer.type)}LayerConfig`;
return (
@@ -944,6 +947,7 @@ export default function LayerConfiguratorFactory(
{this[renderTemplate] &&
this[renderTemplate]({
layer,
+ dataset,
visConfiguratorProps,
layerChannelConfigProps,
layerConfiguratorProps
diff --git a/test/browser/components/side-panel/index.js b/test/browser/components/side-panel/index.js
index b862cd1fc..643184827 100644
--- a/test/browser/components/side-panel/index.js
+++ b/test/browser/components/side-panel/index.js
@@ -23,3 +23,4 @@ import './save-export-dropdown-test';
import './side-panel-test';
import './layer-panel-header-test';
import './filter-manager-test';
+import './layer-configurator-test';
diff --git a/test/browser/components/side-panel/layer-configurator-test.js b/test/browser/components/side-panel/layer-configurator-test.js
new file mode 100644
index 000000000..27ed2c461
--- /dev/null
+++ b/test/browser/components/side-panel/layer-configurator-test.js
@@ -0,0 +1,131 @@
+// Copyright (c) 2020 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+/* eslint-disable max-statements */
+
+import React from 'react';
+import test from 'tape';
+import sinon from 'sinon';
+
+import {LayerConfiguratorFactory} from 'components';
+import {StateWFiles, testCsvDataId} from 'test/helpers/mock-state';
+import {appInjector} from 'components/container';
+import {IntlWrapper, mountWithTheme} from 'test/helpers/component-utils';
+
+// components
+const LayerConfigurator = appInjector.get(LayerConfiguratorFactory);
+
+// components
+const openModal = () => {};
+const updateLayerColorUI = () => {};
+const updateLayerConfig = () => {};
+const updateLayerVisualChannelConfig = () => {};
+const updateLayerType = () => {};
+const updateLayerTextLabel = () => {};
+const updateLayerVisConfig = () => {};
+
+const defaultProps = {
+ layer: StateWFiles.visState.layers[0],
+ datasets: StateWFiles.visState.datasets,
+ layerTypeOptions: [],
+ openModal,
+ updateLayerColorUI,
+ updateLayerConfig,
+ updateLayerVisualChannelConfig,
+ updateLayerType,
+ updateLayerTextLabel,
+ updateLayerVisConfig
+};
+
+test('Components -> LayerConfigurator.mount -> defaut prop', t => {
+ // mount
+ let wrapper;
+ t.doesNotThrow(() => {
+ wrapper = mountWithTheme(
+ <IntlWrapper>
+ <LayerConfigurator {...defaultProps} />
+ </IntlWrapper>
+ );
+ }, 'LayerConfigurator should not fail without props');
+
+ const component = wrapper.find(LayerConfigurator).instance();
+
+ const spy = sinon.spy(component, '_renderScatterplotLayerConfig');
+ component.forceUpdate();
+ wrapper.update();
+ t.ok(spy.calledOnce, 'should call _renderScatterplotLayerConfig');
+
+ const expectedDataset = StateWFiles.visState.datasets[testCsvDataId];
+ const expectedLayer = StateWFiles.visState.layers[0];
+
+ const expectedArgs = {
+ layer: expectedLayer,
+ dataset: expectedDataset,
+ visConfiguratorProps: {
+ layer: expectedLayer,
+ fields: expectedDataset.fields,
+ onChange: updateLayerVisConfig,
+ setColorUI: updateLayerColorUI
+ },
+ layerConfiguratorProps: {
+ layer: expectedLayer,
+ fields: expectedDataset.fields,
+ onChange: updateLayerConfig,
+ setColorUI: updateLayerColorUI
+ },
+ layerChannelConfigProps: {
+ layer: expectedLayer,
+ fields: expectedDataset.fields,
+ onChange: updateLayerVisualChannelConfig
+ }
+ };
+
+ const args = spy.args[0][0];
+
+ t.deepEqual(
+ Object.keys(args).sort(),
+ Object.keys(expectedArgs).sort(),
+ 'render layer method should receive 5 arguments'
+ );
+
+ t.equal(args.layer, expectedArgs.layer, 'render layer method should receive corrent layer arg');
+ t.equal(
+ args.dataset,
+ expectedArgs.dataset,
+ 'render layer method should receive corrent dataset arg'
+ );
+ t.deepEqual(
+ args.visConfiguratorProps,
+ expectedArgs.visConfiguratorProps,
+ 'render layer method should receive corrent visConfiguratorProps arg'
+ );
+ t.deepEqual(
+ args.layerConfiguratorProps,
+ expectedArgs.layerConfiguratorProps,
+ 'render layer method should receive corrent layerConfiguratorProps arg'
+ );
+ t.deepEqual(
+ args.layerChannelConfigProps,
+ expectedArgs.layerChannelConfigProps,
+ 'render layer method should receive corrent layerChannelConfigProps arg'
+ );
+
+ t.end();
+});