Skip to content

Commit

Permalink
[Enhancement] pass dataset to renderLayer function (#1341)
Browse files Browse the repository at this point in the history
* pass dataset to renderLayer function
* add layer configurator tests

Signed-off-by: Shan He <heshan0131@gmail.com>
  • Loading branch information
heshan0131 committed Dec 3, 2020
1 parent 524fc59 commit 06ea669
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/components/side-panel/layer-panel/layer-configurator.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -944,6 +947,7 @@ export default function LayerConfiguratorFactory(
{this[renderTemplate] &&
this[renderTemplate]({
layer,
dataset,
visConfiguratorProps,
layerChannelConfigProps,
layerConfiguratorProps
Expand Down
1 change: 1 addition & 0 deletions test/browser/components/side-panel/index.js
Expand Up @@ -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';
131 changes: 131 additions & 0 deletions 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();
});

0 comments on commit 06ea669

Please sign in to comment.