Skip to content

Commit 9181d0b

Browse files
committed
feat: added tests
1 parent 2ff046c commit 9181d0b

File tree

3 files changed

+84
-9
lines changed

3 files changed

+84
-9
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import React from 'react';
8+
import { i18n } from '@kbn/i18n';
9+
import { mount, shallow } from 'enzyme';
10+
import { act } from 'react-dom/test-utils';
11+
import { EuiIcon } from '@elastic/eui';
12+
import { NewBucketButton, DragDropBuckets, DraggableBucketContainer } from '../shared_components';
13+
14+
jest.mock('@elastic/eui', () => {
15+
const original = jest.requireActual('@elastic/eui');
16+
return {
17+
...original,
18+
EuiDragDropContext: 'eui-drag-drop-context',
19+
EuiDroppable: 'eui-droppable',
20+
EuiDraggable: (props) => props.children({ dragHandleProps: {} }),
21+
};
22+
});
23+
24+
describe('buckets shared components', () => {
25+
describe('DragDropBuckets', () => {
26+
it('should call onDragEnd when dragging ended with reordered items', () => {
27+
const items = ['first', 'second', 'third'];
28+
const defaultProps = {
29+
items,
30+
onDragStart: jest.fn(),
31+
onDragEnd: jest.fn(),
32+
droppableId: 'TEST_ID',
33+
children: items,
34+
};
35+
const instance = shallow(<DragDropBuckets {...defaultProps} />);
36+
act(() => {
37+
// simulate dragging ending
38+
instance.props().onDragEnd({ source: { index: 0 }, destination: { index: 1 } });
39+
});
40+
41+
expect(defaultProps.onDragEnd).toHaveBeenCalledWith(['second', 'first', 'third']);
42+
});
43+
});
44+
describe('DraggableBucketContainer', () => {
45+
const defaultProps = {
46+
isInvalid: false,
47+
invalidMessage: 'invalid',
48+
onRemoveClick: jest.fn(),
49+
removeTitle: 'remove',
50+
children: <div data-test-subj="popover">popover</div>,
51+
id: 0,
52+
idx: 0,
53+
};
54+
it('should render valid component', () => {
55+
const instance = mount(<DraggableBucketContainer {...defaultProps} />);
56+
const popover = instance.find('[data-test-subj="popover"]');
57+
expect(popover).toHaveLength(1);
58+
});
59+
it('should render invalid component', () => {
60+
const instance = mount(<DraggableBucketContainer {...defaultProps} isInvalid />);
61+
const iconProps = instance.find(EuiIcon).first().props();
62+
expect(iconProps.color).toEqual('danger');
63+
expect(iconProps.type).toEqual('alert');
64+
expect(iconProps.title).toEqual('invalid');
65+
});
66+
it('should call onRemoveClick when remove icon is clicked', () => {
67+
const instance = mount(<DraggableBucketContainer {...defaultProps} />);
68+
const removeIcon = instance
69+
.find('[data-test-subj="lns-customBucketContainer-remove"]')
70+
.first();
71+
removeIcon.simulate('click');
72+
expect(defaultProps.onRemoveClick).toHaveBeenCalled();
73+
});
74+
});
75+
});
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ import {
1919
EuiButtonEmpty,
2020
} from '@elastic/eui';
2121

22-
export interface BucketContainerProps {
22+
export const NewBucketButton = ({ label, onClick }: { label: string; onClick: () => void }) => (
23+
<EuiButtonEmpty size="xs" iconType="plusInCircle" onClick={onClick}>
24+
{label}
25+
</EuiButtonEmpty>
26+
);
27+
28+
interface BucketContainerProps {
2329
isInvalid?: boolean;
2430
invalidMessage: string;
2531
onRemoveClick: () => void;
@@ -28,7 +34,7 @@ export interface BucketContainerProps {
2834
dataTestSubj?: string;
2935
}
3036

31-
export const BucketContainer = ({
37+
const BucketContainer = ({
3238
isInvalid,
3339
invalidMessage,
3440
onRemoveClick,
@@ -126,9 +132,3 @@ export const DragDropBuckets = ({
126132
</EuiDragDropContext>
127133
);
128134
};
129-
130-
export const NewBucketButton = ({ label, onClick }: { label: string; onClick: () => void }) => (
131-
<EuiButtonEmpty size="xs" iconType="plusInCircle" onClick={onClick}>
132-
{label}
133-
</EuiButtonEmpty>
134-
);

x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/shared_components/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
*/
66

77
export * from './label_input';
8-
export * from './bucket_container';
8+
export * from './buckets';

0 commit comments

Comments
 (0)