Skip to content

Commit

Permalink
[Vis: Default editor] Unit test cases for controls (#41244)
Browse files Browse the repository at this point in the history
* Add unit tests

* Enable collecting test coverage

* Use brace expansion

* Fix types
  • Loading branch information
sulemanof committed Jul 19, 2019
1 parent 206266d commit a4bedf0
Show file tree
Hide file tree
Showing 16 changed files with 424 additions and 21 deletions.
10 changes: 4 additions & 6 deletions src/dev/jest/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ export default {
'packages/kbn-ui-framework/src/services/**/*.js',
'!packages/kbn-ui-framework/src/services/index.js',
'!packages/kbn-ui-framework/src/services/**/*/index.js',
'src/legacy/core_plugins/**/*.js',
'src/legacy/core_plugins/**/*.jsx',
'src/legacy/core_plugins/**/*.ts',
'src/legacy/core_plugins/**/*.tsx',
'!src/legacy/core_plugins/**/__test__/**/*',
'!src/legacy/core_plugins/**/__snapshots__/**/*',
'src/legacy/core_plugins/**/*.{js,jsx,ts,tsx}',
'!src/legacy/core_plugins/**/{__test__,__snapshots__}/**/*',
'src/legacy/ui/public/{agg_types,vis}/**/*.{ts,tsx}',
'!src/legacy/ui/public/{agg_types,vis}/**/*.d.ts',
],
moduleNameMapper: {
'^plugins/([^\/.]*)(.*)': '<rootDir>/src/legacy/core_plugins/$1/public$2',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import React from 'react';

const wrapWithInlineComp = Component => props => (
<div className={`visEditorAggParam--half visEditorAggParam--half-${props.aggParam.name}`}>
<Component {...props} wrappedWithInlineComp={true}/>
<Component {...props}/>
</div>);

export { wrapWithInlineComp };

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function areBoundsValid({ min, max }: Bounds): boolean {
}

function ExtendedBoundsParamEditor({
value,
value = {} as Bounds,
setValue,
setValidity,
showValidation,
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/ui/public/agg_types/controls/filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface FilterValue {
id: string;
}

function FiltersParamEditor({ agg, value, setValue }: AggParamEditorProps<FilterValue[]>) {
function FiltersParamEditor({ agg, value = [], setValue }: AggParamEditorProps<FilterValue[]>) {
const [filters, setFilters] = useState(() =>
value.map(filter => ({ ...filter, id: generateId() }))
);
Expand Down
1 change: 0 additions & 1 deletion src/legacy/ui/public/agg_types/controls/order.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function OrderParamEditor({
setValue,
setValidity,
setTouched,
wrappedWithInlineComp,
}: AggParamEditorProps<OptionedValueProp> & OptionedParamEditorProps) {
const label = i18n.translate('common.ui.aggTypes.orderLabel', {
defaultMessage: 'Order',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { NumberList } from '../number_list';
function PercentileRanksEditor({
agg,
showValidation,
value,
value = [],
setTouched,
setValidity,
setValue,
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/ui/public/agg_types/controls/percentiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { NumberList } from '../number_list';
function PercentilesEditor({
agg,
showValidation,
value,
value = [],
setTouched,
setValidity,
setValue,
Expand Down
4 changes: 2 additions & 2 deletions src/legacy/ui/public/agg_types/controls/raw_json.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { isValidJson } from '../utils';
function RawJsonParamEditor({
agg,
showValidation,
value,
value = '',
setValidity,
setValue,
setTouched,
Expand Down Expand Up @@ -68,7 +68,7 @@ function RawJsonParamEditor({
<EuiTextArea
id={`visEditorRawJson${agg.id}`}
isInvalid={showValidation ? !isValid : false}
value={value || ''}
value={value}
onChange={onChange}
rows={2}
fullWidth={true}
Expand Down
94 changes: 94 additions & 0 deletions src/legacy/ui/public/agg_types/controls/size.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. 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 React from 'react';
import { mountWithIntl, shallowWithIntl } from 'test_utils/enzyme_helpers';
import { EuiIconTip } from '@elastic/eui';
import { SizeParamEditor, SizeParamEditorProps } from './size';
import { aggParamCommonPropsMock } from './test_utils';

describe('SizeParamEditor', () => {
let defaultProps: SizeParamEditorProps;

beforeEach(() => {
defaultProps = {
...aggParamCommonPropsMock,
value: '',
setValue: jest.fn(),
setValidity: jest.fn(),
setTouched: jest.fn(),
};
});

it('should init with the default set of props', () => {
const comp = shallowWithIntl(<SizeParamEditor {...defaultProps} />);

expect(comp).toMatchSnapshot();
});

it('should render an iconTip in the label if it was passed', () => {
const iconTip = <EuiIconTip position="right" content={'test'} type="questionInCircle" />;
const comp = shallowWithIntl(<SizeParamEditor {...defaultProps} iconTip={iconTip} />);

expect(comp.props().label.props.children[1]).toEqual(iconTip);
});

it('should change its validity due to passed props', () => {
const comp = mountWithIntl(<SizeParamEditor {...defaultProps} />);

expect(defaultProps.setValidity).toHaveBeenCalledWith(false);
expect(comp.children().props()).toHaveProperty('isInvalid', false);

comp.setProps({ disabled: true, showValidation: true });

expect(defaultProps.setValidity).toHaveBeenCalledWith(true);
expect(comp.children().props()).toHaveProperty('isInvalid', false);

comp.setProps({ disabled: false, showValidation: true });

expect(defaultProps.setValidity).toHaveBeenCalledWith(false);
expect(comp.children().props()).toHaveProperty('isInvalid', true);

comp.setProps({ value: 2, showValidation: true });

expect(defaultProps.setValidity).toHaveBeenCalledWith(true);
expect(comp.children().props()).toHaveProperty('isInvalid', false);
expect(defaultProps.setValidity).toHaveBeenCalledTimes(4);
});

it('should set new parsed value', () => {
const comp = mountWithIntl(<SizeParamEditor {...defaultProps} />);
const input = comp.find('[type="number"]');
input.simulate('change', { target: { value: '3' } });

expect(defaultProps.setValue).toBeCalledWith(3);

input.simulate('change', { target: { value: '' } });

expect(defaultProps.setValue).toBeCalledWith('');
expect(defaultProps.setValue).toHaveBeenCalledTimes(2);
});

it('should call setTouched on blur', () => {
const comp = mountWithIntl(<SizeParamEditor {...defaultProps} />);
comp.find('[type="number"]').simulate('blur');

expect(defaultProps.setTouched).toHaveBeenCalledTimes(1);
});
});
3 changes: 1 addition & 2 deletions src/legacy/ui/public/agg_types/controls/size.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { AggParamEditorProps } from 'ui/vis/editors/default';
import { EuiFormRow, EuiFieldNumber } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';

interface SizeParamEditorProps extends AggParamEditorProps<number | ''> {
export interface SizeParamEditorProps extends AggParamEditorProps<number | ''> {
iconTip?: React.ReactNode;
disabled?: boolean;
}
Expand All @@ -36,7 +36,6 @@ function SizeParamEditor({
showValidation,
setValidity,
setTouched,
wrappedWithInlineComp,
}: SizeParamEditorProps) {
const label = (
<>
Expand Down
33 changes: 33 additions & 0 deletions src/legacy/ui/public/agg_types/controls/test_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. 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 { AggConfig, VisState } from 'ui/vis';
import { EditorConfig } from 'ui/vis/editors/config/types';
import { SubAggParamsProp } from 'ui/vis/editors/default/components/default_editor_agg_params';
import { AggParam } from '..';

export const aggParamCommonPropsMock = {
agg: {} as AggConfig,
aggParam: {} as AggParam,
editorConfig: {} as EditorConfig,
metricAggs: [] as AggConfig[],
subAggParams: {} as SubAggParamsProp,
state: {} as VisState,
showValidation: false,
};
Loading

0 comments on commit a4bedf0

Please sign in to comment.