test: Enhance tests and directory structure for FilterControl components (#13745)

* Enhance directory structure

* Enhance paths

* Move AdhocFilter

* Add AdhocFilterPopoverTrigger tests

* Move import

* Enhance AdhocFilterOption tests

* Linting

* Fix linting

* Update superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterOption/AdhocFilterOption.test.tsx

Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com>

* Update superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterOption/index.jsx

Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com>

* Enhance

Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com>
This commit is contained in:
Geido
2021-04-05 21:05:35 +03:00
committed by GitHub
parent 61129f749f
commit 9a4de232a7
15 changed files with 216 additions and 83 deletions

View File

@@ -1,212 +0,0 @@
/**
* 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 AdhocFilter, {
EXPRESSION_TYPES,
CLAUSES,
} from 'src/explore/components/controls/FilterControl/AdhocFilter';
describe('AdhocFilter', () => {
it('sets filterOptionName in constructor', () => {
const adhocFilter = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '>',
comparator: '10',
clause: CLAUSES.WHERE,
});
expect(adhocFilter.filterOptionName.length).toBeGreaterThan(10);
expect(adhocFilter).toEqual({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '>',
comparator: '10',
clause: CLAUSES.WHERE,
filterOptionName: adhocFilter.filterOptionName,
sqlExpression: null,
isExtra: false,
isNew: false,
});
});
it('can create altered duplicates', () => {
const adhocFilter1 = new AdhocFilter({
isNew: true,
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '>',
comparator: '10',
clause: CLAUSES.WHERE,
});
const adhocFilter2 = adhocFilter1.duplicateWith({ operator: '<' });
expect(adhocFilter1.subject).toBe(adhocFilter2.subject);
expect(adhocFilter1.comparator).toBe(adhocFilter2.comparator);
expect(adhocFilter1.clause).toBe(adhocFilter2.clause);
expect(adhocFilter1.expressionType).toBe(adhocFilter2.expressionType);
expect(adhocFilter1.operator).toBe('>');
expect(adhocFilter2.operator).toBe('<');
// duplicated clone should not be new
expect(adhocFilter1.isNew).toBe(true);
expect(adhocFilter2.isNew).toStrictEqual(false);
});
it('can verify equality', () => {
const adhocFilter1 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '>',
comparator: '10',
clause: CLAUSES.WHERE,
});
const adhocFilter2 = adhocFilter1.duplicateWith({});
// eslint-disable-next-line no-unused-expressions
expect(adhocFilter1.equals(adhocFilter2)).toBe(true);
// eslint-disable-next-line no-unused-expressions
expect(adhocFilter1 === adhocFilter2).toBe(false);
});
it('can verify inequality', () => {
const adhocFilter1 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '>',
comparator: '10',
clause: CLAUSES.WHERE,
});
const adhocFilter2 = adhocFilter1.duplicateWith({ operator: '<' });
// eslint-disable-next-line no-unused-expressions
expect(adhocFilter1.equals(adhocFilter2)).toBe(false);
const adhocFilter3 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SQL,
sqlExpression: 'value > 10',
clause: CLAUSES.WHERE,
});
const adhocFilter4 = adhocFilter3.duplicateWith({
sqlExpression: 'value = 5',
});
// eslint-disable-next-line no-unused-expressions
expect(adhocFilter3.equals(adhocFilter4)).toBe(false);
});
it('can determine if it is valid', () => {
const adhocFilter1 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '>',
comparator: '10',
clause: CLAUSES.WHERE,
});
// eslint-disable-next-line no-unused-expressions
expect(adhocFilter1.isValid()).toBe(true);
const adhocFilter2 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '>',
comparator: null,
clause: CLAUSES.WHERE,
});
// eslint-disable-next-line no-unused-expressions
expect(adhocFilter2.isValid()).toBe(false);
const adhocFilter3 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SQL,
sqlExpression: 'some expression',
clause: null,
});
// eslint-disable-next-line no-unused-expressions
expect(adhocFilter3.isValid()).toBe(false);
const adhocFilter4 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: 'IN',
comparator: [],
clause: CLAUSES.WHERE,
});
// eslint-disable-next-line no-unused-expressions
expect(adhocFilter4.isValid()).toBe(false);
const adhocFilter5 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: 'IN',
comparator: ['val1'],
clause: CLAUSES.WHERE,
});
// eslint-disable-next-line no-unused-expressions
expect(adhocFilter5.isValid()).toBe(true);
const adhocFilter6 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '==',
comparator: 1,
clause: CLAUSES.WHERE,
});
// eslint-disable-next-line no-unused-expressions
expect(adhocFilter6.isValid()).toBe(true);
const adhocFilter7 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '==',
comparator: 0,
clause: CLAUSES.WHERE,
});
// eslint-disable-next-line no-unused-expressions
expect(adhocFilter7.isValid()).toBe(true);
const adhocFilter8 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '==',
comparator: null,
clause: CLAUSES.WHERE,
});
// eslint-disable-next-line no-unused-expressions
expect(adhocFilter8.isValid()).toBe(false);
});
it('can translate from simple expressions to sql expressions', () => {
const adhocFilter1 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '==',
comparator: '10',
clause: CLAUSES.WHERE,
});
expect(adhocFilter1.translateToSql()).toBe('value = 10');
const adhocFilter2 = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'SUM(value)',
operator: '!=',
comparator: '5',
clause: CLAUSES.HAVING,
});
expect(adhocFilter2.translateToSql()).toBe('SUM(value) <> 5');
});
});

View File

@@ -1,144 +0,0 @@
/**
* 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.
*/
/* eslint-disable no-unused-expressions */
import React from 'react';
import sinon from 'sinon';
import { shallow } from 'enzyme';
import { supersetTheme } from '@superset-ui/core';
import AdhocFilter, {
EXPRESSION_TYPES,
CLAUSES,
} from 'src/explore/components/controls/FilterControl/AdhocFilter';
import { LabelsContainer } from 'src/explore/components/controls/OptionControls';
import { AGGREGATES, OPERATORS } from 'src/explore/constants';
import AdhocFilterControl from 'src/explore/components/controls/FilterControl/AdhocFilterControl';
import AdhocMetric from 'src/explore/components/controls/MetricControl/AdhocMetric';
const simpleAdhocFilter = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '>',
comparator: '10',
clause: CLAUSES.WHERE,
});
const sumValueAdhocMetric = new AdhocMetric({
expressionType: EXPRESSION_TYPES.SIMPLE,
column: { type: 'VARCHAR(255)', column_name: 'source' },
aggregate: AGGREGATES.SUM,
});
const savedMetric = { metric_name: 'sum__value', expression: 'SUM(value)' };
const columns = [
{ type: 'VARCHAR(255)', column_name: 'source' },
{ type: 'VARCHAR(255)', column_name: 'target' },
{ type: 'DOUBLE', column_name: 'value' },
];
const formData = {
metric: undefined,
metrics: [sumValueAdhocMetric, savedMetric.metric_name],
};
function setup(overrides) {
const onChange = sinon.spy();
const props = {
onChange,
value: [simpleAdhocFilter],
datasource: { type: 'table' },
columns,
savedMetrics: [savedMetric],
formData,
theme: supersetTheme,
...overrides,
};
const wrapper = shallow(<AdhocFilterControl {...props} />);
const component = wrapper.dive().shallow();
return { wrapper, component, onChange };
}
describe('AdhocFilterControl', () => {
it('renders LabelsContainer', () => {
const { component } = setup();
expect(component.find(LabelsContainer)).toExist();
});
it('handles saved metrics being selected to filter on', () => {
const { component, onChange } = setup({ value: [] });
component.instance().onNewFilter({ saved_metric_name: 'sum__value' });
const adhocFilter = onChange.lastCall.args[0][0];
expect(adhocFilter instanceof AdhocFilter).toBe(true);
expect(
adhocFilter.equals(
new AdhocFilter({
expressionType: EXPRESSION_TYPES.SQL,
subject: savedMetric.expression,
operator: OPERATORS['>'],
comparator: 0,
clause: CLAUSES.HAVING,
}),
),
).toBe(true);
});
it('handles adhoc metrics being selected to filter on', () => {
const { component, onChange } = setup({ value: [] });
component.instance().onNewFilter(sumValueAdhocMetric);
const adhocFilter = onChange.lastCall.args[0][0];
expect(adhocFilter instanceof AdhocFilter).toBe(true);
expect(
adhocFilter.equals(
new AdhocFilter({
expressionType: EXPRESSION_TYPES.SQL,
subject: sumValueAdhocMetric.label,
operator: OPERATORS['>'],
comparator: 0,
clause: CLAUSES.HAVING,
}),
),
).toBe(true);
});
it('persists existing filters even when new filters are added', () => {
const { component, onChange } = setup();
component.instance().onNewFilter(columns[0]);
const existingAdhocFilter = onChange.lastCall.args[0][0];
expect(existingAdhocFilter instanceof AdhocFilter).toBe(true);
expect(existingAdhocFilter.equals(simpleAdhocFilter)).toBe(true);
const newAdhocFilter = onChange.lastCall.args[0][1];
expect(newAdhocFilter instanceof AdhocFilter).toBe(true);
expect(
newAdhocFilter.equals(
new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: columns[0].column_name,
operator: OPERATORS['=='],
comparator: '',
clause: CLAUSES.WHERE,
}),
),
).toBe(true);
});
});

View File

@@ -1,200 +0,0 @@
/**
* 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.
*/
/* eslint-disable no-unused-expressions */
import React from 'react';
import sinon from 'sinon';
import { shallow } from 'enzyme';
import { FormGroup } from 'react-bootstrap';
import AdhocFilter, {
EXPRESSION_TYPES,
CLAUSES,
} from 'src/explore/components/controls/FilterControl/AdhocFilter';
import { AGGREGATES } from 'src/explore/constants';
import AdhocFilterEditPopoverSimpleTabContent from 'src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSimpleTabContent';
import AdhocMetric from 'src/explore/components/controls/MetricControl/AdhocMetric';
const simpleAdhocFilter = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '>',
comparator: '10',
clause: CLAUSES.WHERE,
});
const simpleMultiAdhocFilter = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: 'IN',
comparator: ['10'],
clause: CLAUSES.WHERE,
});
const sumValueAdhocMetric = new AdhocMetric({
expressionType: EXPRESSION_TYPES.SIMPLE,
column: { type: 'VARCHAR(255)', column_name: 'source' },
aggregate: AGGREGATES.SUM,
});
const simpleCustomFilter = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'ds',
operator: 'LATEST PARTITION',
});
const options = [
{ type: 'VARCHAR(255)', column_name: 'source', id: 1 },
{ type: 'VARCHAR(255)', column_name: 'target', id: 2 },
{ type: 'DOUBLE', column_name: 'value', id: 3 },
{ saved_metric_name: 'my_custom_metric', id: 4 },
sumValueAdhocMetric,
];
function setup(overrides) {
const onChange = sinon.spy();
const onHeightChange = sinon.spy();
const props = {
adhocFilter: simpleAdhocFilter,
onChange,
onHeightChange,
options,
datasource: {},
...overrides,
};
const wrapper = shallow(
<AdhocFilterEditPopoverSimpleTabContent {...props} />,
);
return { wrapper, onChange, onHeightChange };
}
describe('AdhocFilterEditPopoverSimpleTabContent', () => {
it('renders the simple tab form', () => {
const { wrapper } = setup();
expect(wrapper.find(FormGroup)).toHaveLength(3);
});
it('passes the new adhocFilter to onChange after onSubjectChange', () => {
const { wrapper, onChange } = setup();
wrapper.instance().onSubjectChange(1);
expect(onChange.calledOnce).toBe(true);
expect(onChange.lastCall.args[0]).toEqual(
simpleAdhocFilter.duplicateWith({ subject: 'source' }),
);
});
it('may alter the clause in onSubjectChange if the old clause is not appropriate', () => {
const { wrapper, onChange } = setup();
wrapper.instance().onSubjectChange(sumValueAdhocMetric.optionName);
expect(onChange.calledOnce).toBe(true);
expect(onChange.lastCall.args[0]).toEqual(
simpleAdhocFilter.duplicateWith({
subject: sumValueAdhocMetric.label,
clause: CLAUSES.HAVING,
}),
);
});
it('will convert from individual comparator to array if the operator changes to multi', () => {
const { wrapper, onChange } = setup();
wrapper.instance().onOperatorChange('IN');
expect(onChange.calledOnce).toBe(true);
expect(onChange.lastCall.args[0]).toEqual(
simpleAdhocFilter.duplicateWith({ operator: 'IN', comparator: ['10'] }),
);
});
it('will convert from array to individual comparators if the operator changes from multi', () => {
const { wrapper, onChange } = setup({
adhocFilter: simpleMultiAdhocFilter,
});
wrapper.instance().onOperatorChange('<');
expect(onChange.calledOnce).toBe(true);
expect(onChange.lastCall.args[0]).toEqual(
simpleMultiAdhocFilter.duplicateWith({ operator: '<', comparator: '10' }),
);
});
it('passes the new adhocFilter to onChange after onComparatorChange', () => {
const { wrapper, onChange } = setup();
wrapper.instance().onComparatorChange('20');
expect(onChange.calledOnce).toBe(true);
expect(onChange.lastCall.args[0]).toEqual(
simpleAdhocFilter.duplicateWith({ comparator: '20' }),
);
});
it('will filter operators for table datasources', () => {
const { wrapper } = setup({ datasource: { type: 'table' } });
expect(wrapper.instance().isOperatorRelevant('REGEX')).toBe(false);
expect(wrapper.instance().isOperatorRelevant('LIKE')).toBe(true);
});
it('will filter operators for druid datasources', () => {
const { wrapper } = setup({ datasource: { type: 'druid' } });
expect(wrapper.instance().isOperatorRelevant('REGEX')).toBe(true);
expect(wrapper.instance().isOperatorRelevant('LIKE')).toBe(false);
});
it('will show LATEST PARTITION operator', () => {
const { wrapper } = setup({
datasource: {
type: 'table',
datasource_name: 'table1',
schema: 'schema',
},
adhocFilter: simpleCustomFilter,
partitionColumn: 'ds',
});
expect(
wrapper.instance().isOperatorRelevant('LATEST PARTITION', 'ds'),
).toBe(true);
expect(
wrapper.instance().isOperatorRelevant('LATEST PARTITION', 'value'),
).toBe(false);
});
it('will generate custom sqlExpression for LATEST PARTITION operator', () => {
const testAdhocFilter = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'ds',
});
const { wrapper, onChange } = setup({
datasource: {
type: 'table',
datasource_name: 'table1',
schema: 'schema',
},
adhocFilter: testAdhocFilter,
partitionColumn: 'ds',
});
wrapper.instance().onOperatorChange('LATEST PARTITION');
expect(onChange.lastCall.args[0]).toEqual(
testAdhocFilter.duplicateWith({
subject: 'ds',
operator: 'LATEST PARTITION',
comparator: null,
clause: 'WHERE',
expressionType: 'SQL',
sqlExpression: "ds = '{{ presto.latest_partition('schema.table1') }}'",
}),
);
});
});

View File

@@ -1,77 +0,0 @@
/**
* 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.
*/
/* eslint-disable no-unused-expressions */
import React from 'react';
import sinon from 'sinon';
import { shallow } from 'enzyme';
import { FormGroup } from 'react-bootstrap';
import AdhocFilter, {
EXPRESSION_TYPES,
CLAUSES,
} from 'src/explore/components/controls/FilterControl/AdhocFilter';
import AdhocFilterEditPopoverSqlTabContent from 'src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSqlTabContent';
const sqlAdhocFilter = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SQL,
sqlExpression: 'value > 10',
clause: CLAUSES.WHERE,
});
function setup(overrides) {
const onChange = sinon.spy();
const props = {
adhocFilter: sqlAdhocFilter,
onChange,
options: [],
height: 100,
...overrides,
};
const wrapper = shallow(<AdhocFilterEditPopoverSqlTabContent {...props} />);
return { wrapper, onChange };
}
describe('AdhocFilterEditPopoverSqlTabContent', () => {
it('renders the sql tab form', () => {
const { wrapper } = setup();
expect(wrapper.find(FormGroup)).toHaveLength(2);
});
it('passes the new clause to onChange after onSqlExpressionClauseChange', () => {
const { wrapper, onChange } = setup();
wrapper.instance().onSqlExpressionClauseChange(CLAUSES.HAVING);
expect(onChange.calledOnce).toBe(true);
expect(
onChange.lastCall.args[0].equals(
sqlAdhocFilter.duplicateWith({ clause: CLAUSES.HAVING }),
),
).toBe(true);
});
it('passes the new query to onChange after onSqlExpressionChange', () => {
const { wrapper, onChange } = setup();
wrapper.instance().onSqlExpressionChange('value < 5');
expect(onChange.calledOnce).toBe(true);
expect(
onChange.lastCall.args[0].equals(
sqlAdhocFilter.duplicateWith({ sqlExpression: 'value < 5' }),
),
).toBe(true);
});
});

View File

@@ -1,151 +0,0 @@
/**
* 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.
*/
/* eslint-disable no-unused-expressions */
import React from 'react';
import sinon from 'sinon';
import { shallow } from 'enzyme';
import Button from 'src/components/Button';
import ErrorBoundary from 'src/components/ErrorBoundary';
import Tabs from 'src/common/components/Tabs';
import AdhocFilter, {
EXPRESSION_TYPES,
CLAUSES,
} from 'src/explore/components/controls/FilterControl/AdhocFilter';
import { AGGREGATES } from 'src/explore/constants';
import AdhocFilterEditPopover from 'src/explore/components/controls/FilterControl/AdhocFilterEditPopover';
import AdhocFilterEditPopoverSimpleTabContent from 'src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSimpleTabContent';
import AdhocFilterEditPopoverSqlTabContent from 'src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSqlTabContent';
import AdhocMetric from 'src/explore/components/controls/MetricControl/AdhocMetric';
const simpleAdhocFilter = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '>',
comparator: '10',
clause: CLAUSES.WHERE,
});
const sqlAdhocFilter = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SQL,
sqlExpression: 'value > 10',
clause: CLAUSES.WHERE,
});
const faultyAdhocFilter = new AdhocFilter({
expressionType: null,
subject: null,
operator: '>',
comparator: '10',
clause: CLAUSES.WHERE,
});
const sumValueAdhocMetric = new AdhocMetric({
expressionType: EXPRESSION_TYPES.SIMPLE,
column: { type: 'VARCHAR(255)', column_name: 'source' },
aggregate: AGGREGATES.SUM,
});
const options = [
{ type: 'VARCHAR(255)', column_name: 'source' },
{ type: 'VARCHAR(255)', column_name: 'target' },
{ type: 'DOUBLE', column_name: 'value' },
{ saved_metric_name: 'my_custom_metric' },
sumValueAdhocMetric,
];
function setup(overrides) {
const onChange = sinon.spy();
const onClose = sinon.spy();
const onResize = sinon.spy();
const props = {
adhocFilter: simpleAdhocFilter,
onChange,
onClose,
onResize,
options,
datasource: {},
...overrides,
};
const wrapper = shallow(<AdhocFilterEditPopover {...props} />);
return { wrapper, onChange, onClose, onResize };
}
describe('AdhocFilterEditPopover', () => {
it('renders simple tab content by default', () => {
const { wrapper } = setup();
expect(wrapper.find(Tabs)).toExist();
expect(wrapper.find(Tabs.TabPane)).toHaveLength(2);
expect(wrapper.find(Button)).toHaveLength(2);
expect(wrapper.find(AdhocFilterEditPopoverSimpleTabContent)).toHaveLength(
1,
);
});
it('renders sql tab content when the adhoc filter expressionType is sql', () => {
const { wrapper } = setup({ adhocFilter: sqlAdhocFilter });
expect(wrapper.find(Tabs)).toExist();
expect(wrapper.find(Tabs.TabPane)).toHaveLength(2);
expect(wrapper.find(Button)).toHaveLength(2);
expect(wrapper.find(AdhocFilterEditPopoverSqlTabContent)).toExist();
});
it('renders simple and sql tabs with ErrorBoundary instead of content', () => {
const { wrapper } = setup({ adhocFilter: faultyAdhocFilter });
expect(wrapper.find(Tabs)).toExist();
expect(wrapper.find(Tabs.TabPane)).toHaveLength(2);
expect(wrapper.find(Button)).toHaveLength(2);
expect(wrapper.find(ErrorBoundary)).toHaveLength(2);
});
it('overwrites the adhocFilter in state with onAdhocFilterChange', () => {
const { wrapper } = setup();
wrapper.instance().onAdhocFilterChange(sqlAdhocFilter);
expect(wrapper.state('adhocFilter')).toEqual(sqlAdhocFilter);
});
it('prevents saving if the filter is invalid', () => {
const { wrapper } = setup();
expect(wrapper.find(Button).find({ disabled: true })).not.toExist();
wrapper
.instance()
.onAdhocFilterChange(simpleAdhocFilter.duplicateWith({ operator: null }));
expect(wrapper.find(Button).find({ disabled: true })).toExist();
wrapper.instance().onAdhocFilterChange(sqlAdhocFilter);
expect(wrapper.find(Button).find({ disabled: true })).not.toExist();
});
it('highlights save if changes are present', () => {
const { wrapper } = setup();
expect(wrapper.find(Button).find({ buttonStyle: 'primary' })).not.toExist();
wrapper.instance().onAdhocFilterChange(sqlAdhocFilter);
expect(wrapper.find(Button).find({ buttonStyle: 'primary' })).toExist();
});
it('will initiate a drag when clicked', () => {
const { wrapper } = setup();
wrapper.instance().onDragDown = sinon.spy();
wrapper.instance().forceUpdate();
expect(wrapper.find('.fa-expand')).toExist();
expect(wrapper.instance().onDragDown.calledOnce).toBe(false);
wrapper.find('.fa-expand').simulate('mouseDown', {});
expect(wrapper.instance().onDragDown.calledOnce).toBe(true);
});
});

View File

@@ -1,61 +0,0 @@
/**
* 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.
*/
/* eslint-disable no-unused-expressions */
import React from 'react';
import sinon from 'sinon';
import { shallow } from 'enzyme';
import Popover from 'src/components/Popover';
import AdhocFilter, {
EXPRESSION_TYPES,
CLAUSES,
} from 'src/explore/components/controls/FilterControl/AdhocFilter';
import AdhocFilterOption from 'src/explore/components/controls/FilterControl/AdhocFilterOption';
const simpleAdhocFilter = new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '>',
comparator: '10',
clause: CLAUSES.WHERE,
});
function setup(overrides) {
const onFilterEdit = sinon.spy();
const props = {
adhocFilter: simpleAdhocFilter,
onFilterEdit,
options: [],
datasource: {},
...overrides,
};
const wrapper = shallow(<AdhocFilterOption {...props} />);
return { wrapper };
}
describe('AdhocFilterOption', () => {
it('renders an overlay trigger wrapper for the label', () => {
const { wrapper } = setup();
const overlay = wrapper.find('AdhocFilterPopoverTrigger').shallow();
const popover = overlay.find(Popover);
expect(popover).toHaveLength(1);
expect(popover.props().defaultVisible).toBe(false);
expect(overlay.find('OptionControlLabel')).toExist();
});
});