test: CollectionControl (#13656)

* Tests for CollectionControl

* add role to icon

* applying factory to props
This commit is contained in:
Bruno Motta
2021-04-05 14:52:24 -03:00
committed by GitHub
parent 35f33e8276
commit 555d7bbb35
3 changed files with 142 additions and 4 deletions

View File

@@ -0,0 +1,21 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
.CollectionControl .list-group-item i.fa {
padding-top: 5px;
}

View File

@@ -0,0 +1,134 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import userEvent from '@testing-library/user-event';
import React from 'react';
import { render, screen } from 'spec/helpers/testing-library';
import CollectionControl from '.';
jest.mock('@superset-ui/chart-controls', () => ({
InfoTooltipWithTrigger: (props: any) => (
<button
onClick={props.onClick}
type="button"
data-icon={props.icon}
data-tooltip={props.tooltip}
>
{props.label}
</button>
),
}));
jest.mock('..', () => ({
__esModule: true,
default: {
TestControl: (props: any) => (
<button
type="button"
onClick={() => props.onChange(0, 'update')}
data-test="TestControl"
>
TestControl
</button>
),
},
}));
const createProps = () => ({
actions: {
addDangerToast: jest.fn(),
addInfoToast: jest.fn(),
addSuccessToast: jest.fn(),
addWarningToast: jest.fn(),
createNewSlice: jest.fn(),
fetchDatasourcesStarted: jest.fn(),
fetchDatasourcesSucceeded: jest.fn(),
fetchFaveStar: jest.fn(),
saveFaveStar: jest.fn(),
setControlValue: jest.fn(),
setDatasource: jest.fn(),
setDatasourceType: jest.fn(),
setDatasources: jest.fn(),
setExploreControls: jest.fn(),
sliceUpdated: jest.fn(),
toggleFaveStar: jest.fn(),
updateChartTitle: jest.fn(),
},
addTooltip: 'Add an item',
controlName: 'TestControl',
description: null,
hovered: false,
itemGenerator: jest.fn(),
keyAccessor: jest.fn(),
label: 'Time series columns',
name: 'column_collection',
onChange: jest.fn(),
placeholder: 'Empty collection',
type: 'CollectionControl',
validationErrors: [],
validators: [jest.fn()],
value: [{ key: 'hrYAZ5iBH' }],
});
test('Should render', () => {
const props = createProps();
render(<CollectionControl {...props} />);
expect(screen.getByTestId('CollectionControl')).toBeInTheDocument();
});
test('Should show the button with the label', () => {
const props = createProps();
render(<CollectionControl {...props} />);
expect(screen.getByRole('button', { name: props.label })).toBeInTheDocument();
expect(screen.getByRole('button', { name: props.label })).toHaveTextContent(
props.label,
);
});
test('Should have add button', () => {
const props = createProps();
render(<CollectionControl {...props} />);
expect(props.onChange).toBeCalledTimes(0);
userEvent.click(screen.getByRole('button', { name: 'add-item' }));
expect(props.onChange).toBeCalledWith([{ key: 'hrYAZ5iBH' }, undefined]);
});
test('Should have remove button', () => {
const props = createProps();
render(<CollectionControl {...props} />);
expect(props.onChange).toBeCalledTimes(0);
userEvent.click(screen.getByRole('button', { name: 'remove-item' }));
expect(props.onChange).toBeCalledWith([]);
});
test('Should have SortableDragger icon', () => {
const props = createProps();
render(<CollectionControl {...props} />);
expect(screen.getByRole('img')).toBeVisible();
});
test('Should call Control component', () => {
const props = createProps();
render(<CollectionControl {...props} />);
expect(props.onChange).toBeCalledTimes(0);
userEvent.click(screen.getByTestId('TestControl'));
expect(props.onChange).toBeCalledWith([{ key: 'hrYAZ5iBH' }]);
});

View File

@@ -0,0 +1,158 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import PropTypes from 'prop-types';
import { ListGroup, ListGroupItem } from 'react-bootstrap';
import shortid from 'shortid';
import {
SortableContainer,
SortableHandle,
SortableElement,
arrayMove,
} from 'react-sortable-hoc';
import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls';
import ControlHeader from 'src/explore/components/ControlHeader';
import controlMap from '..';
import './CollectionControl.less';
const propTypes = {
name: PropTypes.string.isRequired,
label: PropTypes.string,
description: PropTypes.string,
placeholder: PropTypes.string,
addTooltip: PropTypes.string,
itemGenerator: PropTypes.func,
keyAccessor: PropTypes.func,
onChange: PropTypes.func,
value: PropTypes.oneOfType([PropTypes.array]),
isFloat: PropTypes.bool,
isInt: PropTypes.bool,
controlName: PropTypes.string.isRequired,
};
const defaultProps = {
label: null,
description: null,
onChange: () => {},
placeholder: 'Empty collection',
itemGenerator: () => ({ key: shortid.generate() }),
keyAccessor: o => o.key,
value: [],
addTooltip: 'Add an item',
};
const SortableListGroupItem = SortableElement(ListGroupItem);
const SortableListGroup = SortableContainer(ListGroup);
const SortableDragger = SortableHandle(() => (
<i
role="img"
className="fa fa-bars text-primary"
style={{ cursor: 'ns-resize' }}
/>
));
export default class CollectionControl extends React.Component {
constructor(props) {
super(props);
this.onAdd = this.onAdd.bind(this);
}
onChange(i, value) {
Object.assign(this.props.value[i], value);
this.props.onChange(this.props.value);
}
onAdd() {
this.props.onChange(this.props.value.concat([this.props.itemGenerator()]));
}
onSortEnd({ oldIndex, newIndex }) {
this.props.onChange(arrayMove(this.props.value, oldIndex, newIndex));
}
removeItem(i) {
this.props.onChange(this.props.value.filter((o, ix) => i !== ix));
}
renderList() {
if (this.props.value.length === 0) {
return <div className="text-muted">{this.props.placeholder}</div>;
}
const Control = controlMap[this.props.controlName];
return (
<SortableListGroup
useDragHandle
lockAxis="y"
onSortEnd={this.onSortEnd.bind(this)}
>
{this.props.value.map((o, i) => {
// label relevant only for header, not here
const { label, ...commonProps } = this.props;
return (
<SortableListGroupItem
className="clearfix"
key={this.props.keyAccessor(o)}
index={i}
>
<div className="pull-left m-r-5">
<SortableDragger />
</div>
<div className="pull-left">
<Control
{...commonProps}
{...o}
onChange={this.onChange.bind(this, i)}
/>
</div>
<div className="pull-right">
<InfoTooltipWithTrigger
icon="times"
label="remove-item"
tooltip="remove item"
bsStyle="primary"
onClick={this.removeItem.bind(this, i)}
/>
</div>
</SortableListGroupItem>
);
})}
</SortableListGroup>
);
}
render() {
return (
<div data-test="CollectionControl" className="CollectionControl">
<ControlHeader {...this.props} />
{this.renderList()}
<InfoTooltipWithTrigger
icon="plus-circle"
label="add-item"
tooltip={this.props.addTooltip}
bsStyle="primary"
className="fa-lg"
onClick={this.onAdd}
/>
</div>
);
}
}
CollectionControl.propTypes = propTypes;
CollectionControl.defaultProps = defaultProps;