Skip to content

Commit

Permalink
Add TimeseriesUIRestrictions type and refactor add_delete_buttons.test
Browse files Browse the repository at this point in the history
  • Loading branch information
DianaDerevyankina committed Jun 19, 2020
1 parent 1b62b5b commit 7085e58
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
26 changes: 16 additions & 10 deletions src/plugins/vis_type_timeseries/common/ui_restrictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,38 @@
* under the License.
*/

export interface UIRestrictions {
'*': boolean;
[restriction: string]: boolean;
}

/**
* UI Restrictions keys
* @constant
* @public
*/
export const RESTRICTIONS_KEYS = {
export enum RESTRICTIONS_KEYS {
/**
* Key for getting the white listed group by fields from the UIRestrictions object.
*/
WHITE_LISTED_GROUP_BY_FIELDS: 'whiteListedGroupByFields',
WHITE_LISTED_GROUP_BY_FIELDS = 'whiteListedGroupByFields',

/**
* Key for getting the white listed metrics from the UIRestrictions object.
*/
WHITE_LISTED_METRICS: 'whiteListedMetrics',
WHITE_LISTED_METRICS = 'whiteListedMetrics',

/**
* Key for getting the white listed Time Range modes from the UIRestrictions object.
*/
WHITE_LISTED_TIMERANGE_MODES: 'whiteListedTimerangeModes',
};
WHITE_LISTED_TIMERANGE_MODES = 'whiteListedTimerangeModes',
}

export interface UIRestrictions {
'*': boolean;
[restriction: string]: boolean;
}

export interface TimeseriesUIRestrictions {
[RESTRICTIONS_KEYS.WHITE_LISTED_GROUP_BY_FIELDS]: Record<string, UIRestrictions>;
[RESTRICTIONS_KEYS.WHITE_LISTED_METRICS]: Record<string, UIRestrictions>;
[RESTRICTIONS_KEYS.WHITE_LISTED_TIMERANGE_MODES]: Record<string, UIRestrictions>;
}

/**
* Default value for the UIRestriction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,49 @@
*/

import React from 'react';
import expect from '@kbn/expect';
import { shallowWithIntl } from 'test_utils/enzyme_helpers';
import sinon from 'sinon';
import { AddDeleteButtons } from './add_delete_buttons';

describe('AddDeleteButtons', () => {
it('calls onAdd={handleAdd}', () => {
const handleAdd = sinon.spy();
const handleAdd = jest.fn();
const wrapper = shallowWithIntl(<AddDeleteButtons onAdd={handleAdd} />);
wrapper.find('EuiButtonIcon').at(0).simulate('click');
expect(handleAdd.calledOnce).to.equal(true);
expect(handleAdd).toHaveBeenCalled();
});

it('calls onDelete={handleDelete}', () => {
const handleDelete = sinon.spy();
const handleDelete = jest.fn();
const wrapper = shallowWithIntl(<AddDeleteButtons onDelete={handleDelete} />);
wrapper.find('EuiButtonIcon').at(1).simulate('click');
expect(handleDelete.calledOnce).to.equal(true);
expect(handleDelete).toHaveBeenCalled();
});

it('calls onClone={handleClone}', () => {
const handleClone = sinon.spy();
const handleClone = jest.fn();
const wrapper = shallowWithIntl(<AddDeleteButtons onClone={handleClone} />);
wrapper.find('EuiButtonIcon').at(0).simulate('click');
expect(handleClone.calledOnce).to.equal(true);
expect(handleClone).toHaveBeenCalled();
});

it('disableDelete={true}', () => {
const wrapper = shallowWithIntl(<AddDeleteButtons disableDelete={true} />);
expect(wrapper.find({ text: 'Delete' })).to.have.length(0);
expect(wrapper.find({ text: 'Delete' })).toHaveLength(0);
});

it('disableAdd={true}', () => {
const wrapper = shallowWithIntl(<AddDeleteButtons disableAdd={true} />);
expect(wrapper.find({ text: 'Add' })).to.have.length(0);
expect(wrapper.find({ text: 'Add' })).toHaveLength(0);
});

it('should not display clone by default', () => {
const wrapper = shallowWithIntl(<AddDeleteButtons />);
expect(wrapper.find({ text: 'Clone' })).to.have.length(0);
expect(wrapper.find({ text: 'Clone' })).toHaveLength(0);
});

it('should not display clone when disableAdd={true}', () => {
const fn = sinon.spy();
const fn = jest.fn();
const wrapper = shallowWithIntl(<AddDeleteButtons onClone={fn} disableAdd={true} />);
expect(wrapper.find({ text: 'Clone' })).to.have.length(0);
expect(wrapper.find({ text: 'Clone' })).toHaveLength(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { UnsupportedAgg } from './unsupported_agg';
import { TemporaryUnsupportedAgg } from './temporary_unsupported_agg';
import { MetricsItemsSchema, PanelSchema, SeriesItemsSchema } from '../../../../common/types';
import { DragHandleProps } from '../../../types';
import { UIRestrictions } from '../../../../common/ui_restrictions';
import { TimeseriesUIRestrictions } from '../../../../common/ui_restrictions';
import { IFieldType } from '../../../../../data/common/index_patterns/fields';

interface AggProps extends HTMLAttributes<HTMLElement> {
Expand All @@ -36,7 +36,7 @@ interface AggProps extends HTMLAttributes<HTMLElement> {
panel: PanelSchema;
series: SeriesItemsSchema;
siblings: MetricsItemsSchema[];
uiRestrictions: UIRestrictions;
uiRestrictions: TimeseriesUIRestrictions;
dragHandleProps: DragHandleProps;
onAdd: () => void;
onChange: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { i18n } from '@kbn/i18n';
// @ts-ignore
import { isMetricEnabled } from '../../lib/check_ui_restrictions';
import { MetricsItemsSchema } from '../../../../common/types';
import { UIRestrictions } from '../../../../common/ui_restrictions';
import { TimeseriesUIRestrictions } from '../../../../common/ui_restrictions';

type AggSelectOption = EuiComboBoxOptionOption;

Expand Down Expand Up @@ -239,7 +239,7 @@ interface AggSelectUiProps {
panelType: string;
siblings: MetricsItemsSchema[];
value: string;
uiRestrictions?: UIRestrictions;
uiRestrictions?: TimeseriesUIRestrictions;
onChange: (currentlySelectedOptions: AggSelectOption[]) => void;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { seriesChangeHandler } from '../lib/series_change_handler';
import { handleAdd, handleDelete } from '../lib/collection_actions';
import { newMetricAggFn } from '../lib/new_metric_agg_fn';
import { PanelSchema, SeriesItemsSchema } from '../../../../common/types';
import { UIRestrictions } from '../../../../common/ui_restrictions';
import { TimeseriesUIRestrictions } from '../../../../common/ui_restrictions';
import { IFieldType } from '../../../../../data/common/index_patterns/fields';

const DROPPABLE_ID = 'aggs_dnd';
Expand All @@ -37,7 +37,7 @@ export interface AggsProps {
panel: PanelSchema;
model: SeriesItemsSchema;
fields: IFieldType[];
uiRestrictions: UIRestrictions;
uiRestrictions: TimeseriesUIRestrictions;
}

export class Aggs extends PureComponent<AggsProps> {
Expand Down

0 comments on commit 7085e58

Please sign in to comment.