mirror of
https://github.com/apache/superset.git
synced 2026-04-14 13:44:46 +00:00
Prettify the frontend code (#8648)
* Add Prettier global configs * Format js/jsx/ts/tsx/less files
This commit is contained in:
@@ -24,7 +24,9 @@ import { shallow } from 'enzyme';
|
||||
import MetricsControl from '../../../../src/explore/components/controls/MetricsControl';
|
||||
import { AGGREGATES } from '../../../../src/explore/constants';
|
||||
import OnPasteSelect from '../../../../src/components/OnPasteSelect';
|
||||
import AdhocMetric, { EXPRESSION_TYPES } from '../../../../src/explore/AdhocMetric';
|
||||
import AdhocMetric, {
|
||||
EXPRESSION_TYPES,
|
||||
} from '../../../../src/explore/AdhocMetric';
|
||||
|
||||
const defaultProps = {
|
||||
name: 'metrics',
|
||||
@@ -63,33 +65,56 @@ const sumValueAdhocMetric = new AdhocMetric({
|
||||
});
|
||||
|
||||
describe('MetricsControl', () => {
|
||||
|
||||
it('renders an OnPasteSelect', () => {
|
||||
const { wrapper } = setup();
|
||||
expect(wrapper.find(OnPasteSelect)).toHaveLength(1);
|
||||
});
|
||||
|
||||
describe('constructor', () => {
|
||||
|
||||
it('unifies options for the dropdown select with aggregates', () => {
|
||||
const { wrapper } = setup();
|
||||
expect(wrapper.state('options')).toEqual([
|
||||
{ optionName: '_col_source', type: 'VARCHAR(255)', column_name: 'source' },
|
||||
{ optionName: '_col_target', type: 'VARCHAR(255)', column_name: 'target' },
|
||||
{
|
||||
optionName: '_col_source',
|
||||
type: 'VARCHAR(255)',
|
||||
column_name: 'source',
|
||||
},
|
||||
{
|
||||
optionName: '_col_target',
|
||||
type: 'VARCHAR(255)',
|
||||
column_name: 'target',
|
||||
},
|
||||
{ optionName: '_col_value', type: 'DOUBLE', column_name: 'value' },
|
||||
...Object.keys(AGGREGATES).map(
|
||||
aggregate => ({ aggregate_name: aggregate, optionName: '_aggregate_' + aggregate }),
|
||||
),
|
||||
{ optionName: 'sum__value', metric_name: 'sum__value', expression: 'SUM(energy_usage.value)' },
|
||||
{ optionName: 'avg__value', metric_name: 'avg__value', expression: 'AVG(energy_usage.value)' },
|
||||
...Object.keys(AGGREGATES).map(aggregate => ({
|
||||
aggregate_name: aggregate,
|
||||
optionName: '_aggregate_' + aggregate,
|
||||
})),
|
||||
{
|
||||
optionName: 'sum__value',
|
||||
metric_name: 'sum__value',
|
||||
expression: 'SUM(energy_usage.value)',
|
||||
},
|
||||
{
|
||||
optionName: 'avg__value',
|
||||
metric_name: 'avg__value',
|
||||
expression: 'AVG(energy_usage.value)',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('does not show aggregates in options if no columns', () => {
|
||||
const { wrapper } = setup({ columns: [] });
|
||||
expect(wrapper.state('options')).toEqual([
|
||||
{ optionName: 'sum__value', metric_name: 'sum__value', expression: 'SUM(energy_usage.value)' },
|
||||
{ optionName: 'avg__value', metric_name: 'avg__value', expression: 'AVG(energy_usage.value)' },
|
||||
{
|
||||
optionName: 'sum__value',
|
||||
metric_name: 'sum__value',
|
||||
expression: 'SUM(energy_usage.value)',
|
||||
},
|
||||
{
|
||||
optionName: 'avg__value',
|
||||
metric_name: 'avg__value',
|
||||
expression: 'AVG(energy_usage.value)',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -124,11 +149,9 @@ describe('MetricsControl', () => {
|
||||
'avg__value',
|
||||
]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('onChange', () => {
|
||||
|
||||
it('handles saved metrics being selected', () => {
|
||||
const { wrapper, onChange } = setup();
|
||||
const select = wrapper.find(OnPasteSelect);
|
||||
@@ -143,16 +166,20 @@ describe('MetricsControl', () => {
|
||||
|
||||
const adhocMetric = onChange.lastCall.args[0][0];
|
||||
expect(adhocMetric instanceof AdhocMetric).toBe(true);
|
||||
expect(onChange.lastCall.args).toEqual([[{
|
||||
expressionType: EXPRESSION_TYPES.SIMPLE,
|
||||
column: valueColumn,
|
||||
aggregate: AGGREGATES.SUM,
|
||||
label: 'SUM(value)',
|
||||
fromFormData: false,
|
||||
hasCustomLabel: false,
|
||||
optionName: adhocMetric.optionName,
|
||||
sqlExpression: null,
|
||||
}]]);
|
||||
expect(onChange.lastCall.args).toEqual([
|
||||
[
|
||||
{
|
||||
expressionType: EXPRESSION_TYPES.SIMPLE,
|
||||
column: valueColumn,
|
||||
aggregate: AGGREGATES.SUM,
|
||||
label: 'SUM(value)',
|
||||
fromFormData: false,
|
||||
hasCustomLabel: false,
|
||||
optionName: adhocMetric.optionName,
|
||||
sqlExpression: null,
|
||||
},
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('handles aggregates being selected', () => {
|
||||
@@ -171,15 +198,22 @@ describe('MetricsControl', () => {
|
||||
select.simulate('change', [{ aggregate_name: 'SUM', optionName: 'SUM' }]);
|
||||
|
||||
expect(setInputSpy.calledWith('SUM()')).toBe(true);
|
||||
expect(handleInputSpy.calledWith({ target: { value: 'SUM()' } })).toBe(true);
|
||||
expect(handleInputSpy.calledWith({ target: { value: 'SUM()' } })).toBe(
|
||||
true,
|
||||
);
|
||||
expect(onChange.lastCall.args).toEqual([[]]);
|
||||
});
|
||||
|
||||
it('preserves existing selected AdhocMetrics', () => {
|
||||
const { wrapper, onChange } = setup();
|
||||
const select = wrapper.find(OnPasteSelect);
|
||||
select.simulate('change', [{ metric_name: 'sum__value' }, sumValueAdhocMetric]);
|
||||
expect(onChange.lastCall.args).toEqual([['sum__value', sumValueAdhocMetric]]);
|
||||
select.simulate('change', [
|
||||
{ metric_name: 'sum__value' },
|
||||
sumValueAdhocMetric,
|
||||
]);
|
||||
expect(onChange.lastCall.args).toEqual([
|
||||
['sum__value', sumValueAdhocMetric],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -189,12 +223,12 @@ describe('MetricsControl', () => {
|
||||
value: [sumValueAdhocMetric],
|
||||
});
|
||||
|
||||
const editedMetric = sumValueAdhocMetric.duplicateWith({ aggregate: AGGREGATES.AVG });
|
||||
const editedMetric = sumValueAdhocMetric.duplicateWith({
|
||||
aggregate: AGGREGATES.AVG,
|
||||
});
|
||||
wrapper.instance().onMetricEdit(editedMetric);
|
||||
|
||||
expect(onChange.lastCall.args).toEqual([[
|
||||
editedMetric,
|
||||
]]);
|
||||
expect(onChange.lastCall.args).toEqual([[editedMetric]]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -220,118 +254,151 @@ describe('MetricsControl', () => {
|
||||
it('includes user defined metrics', () => {
|
||||
const { wrapper } = setup({ datasourceType: 'druid' });
|
||||
|
||||
expect(!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'a_metric',
|
||||
optionName: 'a_metric',
|
||||
expression: 'SUM(FANCY(metric))',
|
||||
},
|
||||
'a',
|
||||
)).toBe(true);
|
||||
expect(
|
||||
!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'a_metric',
|
||||
optionName: 'a_metric',
|
||||
expression: 'SUM(FANCY(metric))',
|
||||
},
|
||||
'a',
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('includes auto generated avg metrics for druid', () => {
|
||||
const { wrapper } = setup({ datasourceType: 'druid' });
|
||||
|
||||
expect(!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'avg__metric',
|
||||
optionName: 'avg__metric',
|
||||
expression: 'AVG(metric)',
|
||||
},
|
||||
'a',
|
||||
)).toBe(true);
|
||||
expect(
|
||||
!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'avg__metric',
|
||||
optionName: 'avg__metric',
|
||||
expression: 'AVG(metric)',
|
||||
},
|
||||
'a',
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('includes columns and aggregates', () => {
|
||||
const { wrapper } = setup();
|
||||
|
||||
expect(!!wrapper.instance().selectFilterOption(
|
||||
{ type: 'VARCHAR(255)', column_name: 'source', optionName: '_col_source' },
|
||||
'sou',
|
||||
)).toBe(true);
|
||||
expect(
|
||||
!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
type: 'VARCHAR(255)',
|
||||
column_name: 'source',
|
||||
optionName: '_col_source',
|
||||
},
|
||||
'sou',
|
||||
),
|
||||
).toBe(true);
|
||||
|
||||
expect(!!wrapper.instance().selectFilterOption(
|
||||
{ aggregate_name: 'AVG', optionName: '_aggregate_AVG' },
|
||||
'av',
|
||||
)).toBe(true);
|
||||
expect(
|
||||
!!wrapper
|
||||
.instance()
|
||||
.selectFilterOption(
|
||||
{ aggregate_name: 'AVG', optionName: '_aggregate_AVG' },
|
||||
'av',
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('includes columns based on verbose_name', () => {
|
||||
const { wrapper } = setup();
|
||||
|
||||
expect(!!wrapper.instance().selectFilterOption(
|
||||
{ metric_name: 'sum__num', verbose_name: 'babies', optionName: '_col_sum_num' },
|
||||
'bab',
|
||||
)).toBe(true);
|
||||
expect(
|
||||
!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'sum__num',
|
||||
verbose_name: 'babies',
|
||||
optionName: '_col_sum_num',
|
||||
},
|
||||
'bab',
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('excludes auto generated avg metrics for sqla', () => {
|
||||
const { wrapper } = setup();
|
||||
|
||||
expect(!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'avg__metric',
|
||||
optionName: 'avg__metric',
|
||||
expression: 'AVG(metric)',
|
||||
},
|
||||
'a',
|
||||
)).toBe(false);
|
||||
expect(
|
||||
!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'avg__metric',
|
||||
optionName: 'avg__metric',
|
||||
expression: 'AVG(metric)',
|
||||
},
|
||||
'a',
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('includes custom made simple saved metrics', () => {
|
||||
const { wrapper } = setup();
|
||||
|
||||
expect(!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'my_fancy_sum_metric',
|
||||
optionName: 'my_fancy_sum_metric',
|
||||
expression: 'SUM(value)',
|
||||
},
|
||||
'sum',
|
||||
)).toBe(true);
|
||||
expect(
|
||||
!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'my_fancy_sum_metric',
|
||||
optionName: 'my_fancy_sum_metric',
|
||||
expression: 'SUM(value)',
|
||||
},
|
||||
'sum',
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('excludes auto generated metrics', () => {
|
||||
const { wrapper } = setup();
|
||||
|
||||
expect(!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'sum__value',
|
||||
optionName: 'sum__value',
|
||||
expression: 'SUM(value)',
|
||||
},
|
||||
'sum',
|
||||
)).toBe(false);
|
||||
expect(
|
||||
!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'sum__value',
|
||||
optionName: 'sum__value',
|
||||
expression: 'SUM(value)',
|
||||
},
|
||||
'sum',
|
||||
),
|
||||
).toBe(false);
|
||||
|
||||
expect(!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'sum__value',
|
||||
optionName: 'sum__value',
|
||||
expression: 'SUM("table"."value")',
|
||||
},
|
||||
'sum',
|
||||
)).toBe(false);
|
||||
expect(
|
||||
!!wrapper.instance().selectFilterOption(
|
||||
{
|
||||
metric_name: 'sum__value',
|
||||
optionName: 'sum__value',
|
||||
expression: 'SUM("table"."value")',
|
||||
},
|
||||
'sum',
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('filters out metrics if the input begins with an aggregate', () => {
|
||||
const { wrapper } = setup();
|
||||
wrapper.setState({ aggregateInInput: true });
|
||||
|
||||
expect(!!wrapper.instance().selectFilterOption(
|
||||
{ metric_name: 'metric', expression: 'SUM(FANCY(metric))' },
|
||||
'SUM(',
|
||||
)).toBe(false);
|
||||
expect(
|
||||
!!wrapper
|
||||
.instance()
|
||||
.selectFilterOption(
|
||||
{ metric_name: 'metric', expression: 'SUM(FANCY(metric))' },
|
||||
'SUM(',
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('includes columns if the input begins with an aggregate', () => {
|
||||
const { wrapper } = setup();
|
||||
wrapper.setState({ aggregateInInput: true });
|
||||
|
||||
expect(!!wrapper.instance().selectFilterOption(
|
||||
{ type: 'DOUBLE', column_name: 'value' },
|
||||
'SUM(',
|
||||
)).toBe(true);
|
||||
expect(
|
||||
!!wrapper
|
||||
.instance()
|
||||
.selectFilterOption({ type: 'DOUBLE', column_name: 'value' }, 'SUM('),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('Removes metrics if savedMetrics changes', () => {
|
||||
|
||||
Reference in New Issue
Block a user