|
| 1 | +import React from 'react'; |
| 2 | +import { cleanup, fireEvent, render } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom/extend-expect'; |
| 4 | + |
| 5 | +import Float, { IFloatProps } from '../index'; |
| 6 | + |
| 7 | +function dragFromTo( |
| 8 | + ele: HTMLElement, |
| 9 | + from: NonNullable<IFloatProps['position']>, |
| 10 | + to: NonNullable<IFloatProps['position']> |
| 11 | +) { |
| 12 | + fireEvent.mouseDown(ele, { clientX: from.x, clientY: from.y }); |
| 13 | + fireEvent.mouseMove(document, { clientX: to.x, clientY: to.y }); |
| 14 | + return { |
| 15 | + mouseUp: () => fireEvent.mouseUp(ele, { clientX: to.x, clientY: to.y }), |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +describe('Float Component', () => { |
| 20 | + const defaultProps: IFloatProps = { |
| 21 | + className: 'test-class', |
| 22 | + style: { color: 'red' }, |
| 23 | + draggable: true, |
| 24 | + position: { x: 0, y: 0 }, |
| 25 | + }; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + cleanup(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should match snapshot', () => { |
| 32 | + const { asFragment } = render(<Float {...defaultProps}>Test</Float>); |
| 33 | + expect(asFragment()).toMatchSnapshot(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should handle drag events', () => { |
| 37 | + const fn = jest.fn(); |
| 38 | + const { container } = render( |
| 39 | + <Float {...defaultProps} onChange={fn}> |
| 40 | + Test |
| 41 | + </Float> |
| 42 | + ); |
| 43 | + const floatContainer = container.firstChild as HTMLElement; |
| 44 | + const { mouseUp } = dragFromTo(floatContainer, { x: 0, y: 0 }, { x: 100, y: 100 }); |
| 45 | + expect(floatContainer).toHaveClass('dtc-float-container__dragging'); |
| 46 | + |
| 47 | + mouseUp(); |
| 48 | + |
| 49 | + expect(fn.mock.calls[0][1]).toEqual(expect.objectContaining({ x: 100, y: 100 })); |
| 50 | + expect(floatContainer).not.toHaveClass('dtc-float-container__dragging'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should disable dragging when draggable is set to false', () => { |
| 54 | + const fn = jest.fn(); |
| 55 | + const { container } = render( |
| 56 | + <Float {...defaultProps} draggable={false} onChange={fn}> |
| 57 | + Test |
| 58 | + </Float> |
| 59 | + ); |
| 60 | + const floatContainer = container.firstChild as HTMLElement; |
| 61 | + dragFromTo(floatContainer, { x: 0, y: 0 }, { x: 100, y: 100 }).mouseUp(); |
| 62 | + |
| 63 | + expect(fn).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should support pass through draggable options', () => { |
| 67 | + const fn = jest.fn(); |
| 68 | + const { container } = render( |
| 69 | + <Float |
| 70 | + {...defaultProps} |
| 71 | + draggable={{ |
| 72 | + onDrag: fn, |
| 73 | + }} |
| 74 | + > |
| 75 | + Test |
| 76 | + </Float> |
| 77 | + ); |
| 78 | + |
| 79 | + const floatContainer = container.firstChild as HTMLElement; |
| 80 | + dragFromTo(floatContainer, { x: 0, y: 0 }, { x: 100, y: 100 }).mouseUp(); |
| 81 | + |
| 82 | + expect(fn).toBeCalled(); |
| 83 | + }); |
| 84 | +}); |
0 commit comments