[Explore] Adding custom expressions to adhoc metrics (#4736)

* adding custom expressions to adhoc metrics

* adjusted transitions and made the box expandable
This commit is contained in:
Gabe Lyons
2018-04-13 11:20:53 -07:00
committed by Chris Williams
parent 4c268ec678
commit 8669874ec6
15 changed files with 430 additions and 55 deletions

View File

@@ -6,7 +6,7 @@ import { describe, it } from 'mocha';
import { shallow } from 'enzyme';
import { Button, FormGroup, Popover } from 'react-bootstrap';
import AdhocMetric from '../../../../javascripts/explore/AdhocMetric';
import AdhocMetric, { EXPRESSION_TYPES } from '../../../../javascripts/explore/AdhocMetric';
import AdhocMetricEditPopover from '../../../../javascripts/explore/components/AdhocMetricEditPopover';
import { AGGREGATES } from '../../../../javascripts/explore/constants';
@@ -17,10 +17,16 @@ const columns = [
];
const sumValueAdhocMetric = new AdhocMetric({
expressionType: EXPRESSION_TYPES.SIMPLE,
column: columns[2],
aggregate: AGGREGATES.SUM,
});
const sqlExpressionAdhocMetric = new AdhocMetric({
expressionType: EXPRESSION_TYPES.SQL,
sqlExpression: 'COUNT(*)',
});
function setup(overrides) {
const onChange = sinon.spy();
const onClose = sinon.spy();
@@ -39,7 +45,7 @@ describe('AdhocMetricEditPopover', () => {
it('renders a popover with edit metric form contents', () => {
const { wrapper } = setup();
expect(wrapper.find(Popover)).to.have.lengthOf(1);
expect(wrapper.find(FormGroup)).to.have.lengthOf(2);
expect(wrapper.find(FormGroup)).to.have.lengthOf(3);
expect(wrapper.find(Button)).to.have.lengthOf(2);
});
@@ -55,6 +61,12 @@ describe('AdhocMetricEditPopover', () => {
expect(wrapper.state('adhocMetric')).to.deep.equal(sumValueAdhocMetric.duplicateWith({ aggregate: AGGREGATES.AVG }));
});
it('overwrites the adhocMetric in state with onSqlExpressionChange', () => {
const { wrapper } = setup({ adhocMetric: sqlExpressionAdhocMetric });
wrapper.instance().onSqlExpressionChange('COUNT(1)');
expect(wrapper.state('adhocMetric')).to.deep.equal(sqlExpressionAdhocMetric.duplicateWith({ sqlExpression: 'COUNT(1)' }));
});
it('overwrites the adhocMetric in state with onLabelChange', () => {
const { wrapper } = setup();
wrapper.instance().onLabelChange({ target: { value: 'new label' } });
@@ -87,4 +99,15 @@ describe('AdhocMetricEditPopover', () => {
wrapper.instance().onColumnChange({ column: columns[1] });
expect(wrapper.find(Button).find({ bsStyle: 'primary' })).to.have.lengthOf(1);
});
it('will initiate a drag when clicked', () => {
const { wrapper } = setup();
wrapper.instance().onDragDown = sinon.spy();
wrapper.instance().forceUpdate();
expect(wrapper.find('i.glyphicon-resize-full')).to.have.lengthOf(1);
expect(wrapper.instance().onDragDown.calledOnce).to.be.false;
wrapper.find('i.glyphicon-resize-full').simulate('mouseDown');
expect(wrapper.instance().onDragDown.calledOnce).to.be.true;
});
});

View File

@@ -8,7 +8,7 @@ import { shallow } from 'enzyme';
import MetricsControl from '../../../../javascripts/explore/components/controls/MetricsControl';
import { AGGREGATES } from '../../../../javascripts/explore/constants';
import OnPasteSelect from '../../../../javascripts/components/OnPasteSelect';
import AdhocMetric from '../../../../javascripts/explore/AdhocMetric';
import AdhocMetric, { EXPRESSION_TYPES } from '../../../../javascripts/explore/AdhocMetric';
const defaultProps = {
name: 'metrics',
@@ -73,6 +73,7 @@ describe('MetricsControl', () => {
const { wrapper } = setup({
value: [
{
expressionType: EXPRESSION_TYPES.SIMPLE,
column: { type: 'double', column_name: 'value' },
aggregate: AGGREGATES.SUM,
label: 'SUM(value)',
@@ -87,12 +88,14 @@ describe('MetricsControl', () => {
expect(adhocMetric.optionName.length).to.be.above(10);
expect(wrapper.state('value')).to.deep.equal([
{
expressionType: EXPRESSION_TYPES.SIMPLE,
column: { type: 'double', column_name: 'value' },
aggregate: AGGREGATES.SUM,
fromFormData: true,
label: 'SUM(value)',
hasCustomLabel: false,
optionName: 'blahblahblah',
sqlExpression: null,
},
'avg__value',
]);
@@ -117,12 +120,14 @@ describe('MetricsControl', () => {
const adhocMetric = onChange.lastCall.args[0][0];
expect(adhocMetric instanceof AdhocMetric).to.be.true;
expect(onChange.lastCall.args).to.deep.equal([[{
expressionType: EXPRESSION_TYPES.SIMPLE,
column: valueColumn,
aggregate: AGGREGATES.SUM,
label: 'SUM(value)',
fromFormData: false,
hasCustomLabel: false,
optionName: adhocMetric.optionName,
sqlExpression: null,
}]]);
});