|
| 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 | +}); |
0 commit comments