2019-04-30 14:45:06 -04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-09-18 12:05:57 -04:00
|
|
|
import { getChartControlPanelRegistry, t } from '@superset-ui/core';
|
2021-03-11 20:19:15 -05:00
|
|
|
import {
|
|
|
|
ControlConfig,
|
|
|
|
ControlPanelState,
|
|
|
|
CustomControlItem,
|
|
|
|
DatasourceMeta,
|
|
|
|
} from '@superset-ui/chart-controls';
|
2019-04-30 14:45:06 -04:00
|
|
|
import {
|
|
|
|
getControlConfig,
|
|
|
|
getControlState,
|
|
|
|
applyMapStateToPropsToControl,
|
2020-09-18 12:05:57 -04:00
|
|
|
findControlItem,
|
2020-05-07 17:18:10 -04:00
|
|
|
} from 'src/explore/controlUtils';
|
2020-09-18 12:05:57 -04:00
|
|
|
import {
|
|
|
|
controlPanelSectionsChartOptions,
|
|
|
|
controlPanelSectionsChartOptionsOnlyColorScheme,
|
|
|
|
controlPanelSectionsChartOptionsTable,
|
|
|
|
} from 'spec/javascripts/explore/fixtures';
|
2019-04-30 14:45:06 -04:00
|
|
|
|
2021-03-11 20:19:15 -05:00
|
|
|
const getKnownControlConfig = (controlKey: string, vizType: string) =>
|
|
|
|
getControlConfig(controlKey, vizType) as ControlConfig;
|
|
|
|
|
|
|
|
const getKnownControlState = (...args: Parameters<typeof getControlState>) =>
|
|
|
|
getControlState(...args) as Exclude<ReturnType<typeof getControlState>, null>;
|
|
|
|
|
2019-04-30 14:45:06 -04:00
|
|
|
describe('controlUtils', () => {
|
2021-03-11 20:19:15 -05:00
|
|
|
const state: ControlPanelState = {
|
2021-11-17 02:31:36 -05:00
|
|
|
datasource: {
|
2021-03-11 20:19:15 -05:00
|
|
|
columns: [{ column_name: 'a' }],
|
2019-11-27 17:02:48 -05:00
|
|
|
metrics: [{ metric_name: 'first' }, { metric_name: 'second' }],
|
2021-11-17 02:31:36 -05:00
|
|
|
} as unknown as DatasourceMeta,
|
2020-07-14 02:44:57 -04:00
|
|
|
controls: {},
|
2021-03-11 20:19:15 -05:00
|
|
|
form_data: { datasource: '1__table', viz_type: 'table' },
|
2019-04-30 14:45:06 -04:00
|
|
|
};
|
|
|
|
|
2019-09-11 11:58:24 -04:00
|
|
|
beforeAll(() => {
|
2019-11-27 17:02:48 -05:00
|
|
|
getChartControlPanelRegistry()
|
|
|
|
.registerValue('test-chart', {
|
2020-09-18 12:05:57 -04:00
|
|
|
controlPanelSections: controlPanelSectionsChartOptions,
|
2019-11-27 17:02:48 -05:00
|
|
|
})
|
|
|
|
.registerValue('test-chart-override', {
|
2020-09-18 12:05:57 -04:00
|
|
|
controlPanelSections: controlPanelSectionsChartOptionsOnlyColorScheme,
|
2019-11-27 17:02:48 -05:00
|
|
|
controlOverrides: {
|
|
|
|
color_scheme: {
|
|
|
|
label: t('My beautiful colors'),
|
|
|
|
},
|
2019-09-11 11:58:24 -04:00
|
|
|
},
|
2020-04-24 19:12:26 -04:00
|
|
|
})
|
|
|
|
.registerValue('table', {
|
2020-09-18 12:05:57 -04:00
|
|
|
controlPanelSections: controlPanelSectionsChartOptionsTable,
|
2019-11-27 17:02:48 -05:00
|
|
|
});
|
2019-09-11 11:58:24 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
getChartControlPanelRegistry()
|
|
|
|
.remove('test-chart')
|
|
|
|
.remove('test-chart-override');
|
|
|
|
});
|
|
|
|
|
2019-04-30 14:45:06 -04:00
|
|
|
describe('getControlConfig', () => {
|
|
|
|
it('returns a valid spatial controlConfig', () => {
|
2019-09-11 11:58:24 -04:00
|
|
|
const spatialControl = getControlConfig('color_scheme', 'test-chart');
|
2021-03-11 20:19:15 -05:00
|
|
|
expect(spatialControl?.type).toEqual('ColorSchemeControl');
|
2019-04-30 14:45:06 -04:00
|
|
|
});
|
2019-08-23 16:41:35 -04:00
|
|
|
|
2019-04-30 14:45:06 -04:00
|
|
|
it('overrides according to vizType', () => {
|
2021-03-11 20:19:15 -05:00
|
|
|
let control = getKnownControlConfig('color_scheme', 'test-chart');
|
2019-09-11 11:58:24 -04:00
|
|
|
expect(control.label).toEqual('Color Scheme');
|
2019-04-30 14:45:06 -04:00
|
|
|
|
2021-03-11 20:19:15 -05:00
|
|
|
control = getKnownControlConfig('color_scheme', 'test-chart-override');
|
2019-09-11 11:58:24 -04:00
|
|
|
expect(control.label).toEqual('My beautiful colors');
|
2019-04-30 14:45:06 -04:00
|
|
|
});
|
|
|
|
|
2019-11-27 17:02:48 -05:00
|
|
|
it(
|
|
|
|
'returns correct control config when control config is defined ' +
|
|
|
|
'in the control panel definition',
|
|
|
|
() => {
|
|
|
|
const roseAreaProportionControlConfig = getControlConfig(
|
|
|
|
'rose_area_proportion',
|
|
|
|
'test-chart',
|
|
|
|
);
|
2019-08-23 16:41:35 -04:00
|
|
|
expect(roseAreaProportionControlConfig).toEqual({
|
|
|
|
type: 'CheckboxControl',
|
|
|
|
label: t('Use Area Proportions'),
|
|
|
|
description: t(
|
|
|
|
'Check if the Rose Chart should use segment area instead of ' +
|
2019-11-27 17:02:48 -05:00
|
|
|
'segment radius for proportioning',
|
2019-08-23 16:41:35 -04:00
|
|
|
),
|
|
|
|
default: false,
|
|
|
|
renderTrigger: true,
|
2019-11-27 17:02:48 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2019-04-30 14:45:06 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('applyMapStateToPropsToControl,', () => {
|
|
|
|
it('applies state to props as expected', () => {
|
2021-03-11 20:19:15 -05:00
|
|
|
let control = getKnownControlConfig('all_columns', 'table');
|
2019-04-30 14:45:06 -04:00
|
|
|
control = applyMapStateToPropsToControl(control, state);
|
2021-03-11 20:19:15 -05:00
|
|
|
expect(control.options).toEqual([{ column_name: 'a' }]);
|
2019-04-30 14:45:06 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getControlState', () => {
|
2020-07-01 21:32:27 -04:00
|
|
|
it('to still have the functions', () => {
|
2021-03-11 20:19:15 -05:00
|
|
|
const control = getKnownControlState('metrics', 'table', state, 'a');
|
2020-07-01 21:32:27 -04:00
|
|
|
expect(typeof control.mapStateToProps).toBe('function');
|
2021-03-11 20:19:15 -05:00
|
|
|
expect(typeof control.validators?.[0]).toBe('function');
|
2019-04-30 14:45:06 -04:00
|
|
|
});
|
|
|
|
|
2021-03-11 20:19:15 -05:00
|
|
|
it('to make sure value is array', () => {
|
|
|
|
const control = getKnownControlState('all_columns', 'table', state, 'a');
|
2019-04-30 14:45:06 -04:00
|
|
|
expect(control.value).toEqual(['a']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes missing/invalid choice', () => {
|
2020-04-03 15:50:28 -04:00
|
|
|
let control = getControlState(
|
|
|
|
'stacked_style',
|
|
|
|
'test-chart',
|
|
|
|
state,
|
|
|
|
'stack',
|
|
|
|
);
|
2021-03-11 20:19:15 -05:00
|
|
|
expect(control?.value).toBe('stack');
|
2019-04-30 14:45:06 -04:00
|
|
|
|
2020-04-03 15:50:28 -04:00
|
|
|
control = getControlState('stacked_style', 'test-chart', state, 'FOO');
|
2021-03-11 20:19:15 -05:00
|
|
|
expect(control?.value).toBeNull();
|
2020-06-29 00:37:04 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null for non-existent field', () => {
|
|
|
|
const control = getControlState('NON_EXISTENT', 'table', state);
|
|
|
|
expect(control).toBeNull();
|
2019-04-30 14:45:06 -04:00
|
|
|
});
|
|
|
|
|
2021-05-25 09:55:41 -04:00
|
|
|
it('metrics control should be empty by default', () => {
|
2019-04-30 14:45:06 -04:00
|
|
|
const control = getControlState('metrics', 'table', state);
|
2021-05-25 09:55:41 -04:00
|
|
|
expect(control?.default).toBeUndefined();
|
2019-04-30 14:45:06 -04:00
|
|
|
});
|
|
|
|
|
2021-05-25 09:55:41 -04:00
|
|
|
it('metric control should be empty by default', () => {
|
2019-04-30 14:45:06 -04:00
|
|
|
const control = getControlState('metric', 'table', state);
|
2021-05-25 09:55:41 -04:00
|
|
|
expect(control?.default).toBeUndefined();
|
2019-04-30 14:45:06 -04:00
|
|
|
});
|
2020-07-09 03:33:48 -04:00
|
|
|
|
|
|
|
it('should not apply mapStateToProps when initializing', () => {
|
|
|
|
const control = getControlState('metrics', 'table', {
|
|
|
|
...state,
|
2020-07-14 02:44:57 -04:00
|
|
|
controls: undefined,
|
2020-07-09 03:33:48 -04:00
|
|
|
});
|
2021-03-11 20:19:15 -05:00
|
|
|
expect(control?.value).toBe(undefined);
|
2020-07-09 03:33:48 -04:00
|
|
|
});
|
2019-04-30 14:45:06 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('validateControl', () => {
|
|
|
|
it('validates the control, returns an error if empty', () => {
|
|
|
|
const control = getControlState('metric', 'table', state, null);
|
2021-03-11 20:19:15 -05:00
|
|
|
expect(control?.validationErrors).toEqual(['cannot be empty']);
|
2019-04-30 14:45:06 -04:00
|
|
|
});
|
2020-07-14 02:44:57 -04:00
|
|
|
it('should not validate if control panel is initializing', () => {
|
|
|
|
const control = getControlState(
|
|
|
|
'metric',
|
|
|
|
'table',
|
|
|
|
{ ...state, controls: undefined },
|
|
|
|
undefined,
|
|
|
|
);
|
2021-03-11 20:19:15 -05:00
|
|
|
expect(control?.validationErrors).toBeUndefined();
|
2020-07-14 02:44:57 -04:00
|
|
|
});
|
2020-05-06 07:53:13 -04:00
|
|
|
});
|
2020-09-18 12:05:57 -04:00
|
|
|
|
|
|
|
describe('findControlItem', () => {
|
|
|
|
it('find control as a string', () => {
|
|
|
|
const controlItem = findControlItem(
|
|
|
|
controlPanelSectionsChartOptions,
|
|
|
|
'color_scheme',
|
|
|
|
);
|
|
|
|
expect(controlItem).toEqual('color_scheme');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('find control as a control object', () => {
|
|
|
|
let controlItem = findControlItem(
|
|
|
|
controlPanelSectionsChartOptions,
|
|
|
|
'rose_area_proportion',
|
2021-03-11 20:19:15 -05:00
|
|
|
) as CustomControlItem;
|
2020-09-18 12:05:57 -04:00
|
|
|
expect(controlItem.name).toEqual('rose_area_proportion');
|
|
|
|
expect(controlItem).toHaveProperty('config');
|
|
|
|
|
|
|
|
controlItem = findControlItem(
|
|
|
|
controlPanelSectionsChartOptions,
|
|
|
|
'stacked_style',
|
2021-03-11 20:19:15 -05:00
|
|
|
) as CustomControlItem;
|
2020-09-18 12:05:57 -04:00
|
|
|
expect(controlItem.name).toEqual('stacked_style');
|
|
|
|
expect(controlItem).toHaveProperty('config');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null when key is not found', () => {
|
|
|
|
const controlItem = findControlItem(
|
|
|
|
controlPanelSectionsChartOptions,
|
|
|
|
'non_existing_key',
|
|
|
|
);
|
|
|
|
expect(controlItem).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
2019-04-30 14:45:06 -04:00
|
|
|
});
|