|
| 1 | +import React from 'react'; |
| 2 | +import renderer from 'react-test-renderer'; |
| 3 | +import {shallow} from 'enzyme'; |
| 4 | +import {DraggableCore} from "react-draggable"; |
| 5 | + |
| 6 | +import Resizable from '../lib/Resizable'; |
| 7 | + |
| 8 | +describe('render Resizable', () => { |
| 9 | + const props = { |
| 10 | + axis: 'both', |
| 11 | + className: 'test-classname', |
| 12 | + handleSize: [20, 20], |
| 13 | + height: 50, |
| 14 | + lockAspectRatio: false, |
| 15 | + maxConstraints: [Infinity, Infinity], |
| 16 | + minConstraints: [20, 20], |
| 17 | + onResize: jest.fn(), |
| 18 | + resizeHandles: ['se', 'e'], |
| 19 | + transformScale: 1, |
| 20 | + width: 50, |
| 21 | + }; |
| 22 | + const userChildren = <span className={'children'} />; |
| 23 | + const resizableBoxChildren = <div style={{width: '50px', height: '50px'}}>{userChildren}</div>; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + jest.clearAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + test('snapshot default props', () => { |
| 30 | + const tree = renderer.create(<Resizable {...props}>{resizableBoxChildren}</Resizable>).toJSON(); |
| 31 | + expect(tree).toMatchSnapshot(); |
| 32 | + }); |
| 33 | + |
| 34 | + test('with correct props', () => { |
| 35 | + const element = shallow(<Resizable {...props}>{resizableBoxChildren}</Resizable>); |
| 36 | + expect(element.find('.test-classname').find('.children')); |
| 37 | + expect(element.find(DraggableCore)).toHaveLength(2); |
| 38 | + const cursorSe = element.find('.react-resizable-handle-se'); |
| 39 | + const cursorE = element.find('.react-resizable-handle-e'); |
| 40 | + expect(cursorSe).toHaveLength(1); |
| 41 | + expect(cursorE).toHaveLength(1); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('and pass handle props', () => { |
| 45 | + test('as component', () => { |
| 46 | + const customProps = { |
| 47 | + ...props, |
| 48 | + resizeHandles: ['se'], |
| 49 | + handle: <span className={'custom-component'} /> |
| 50 | + }; |
| 51 | + const element = shallow(<Resizable {...customProps}>{resizableBoxChildren}</Resizable>); |
| 52 | + expect(element.find('.react-resizable-handle-se')).toHaveLength(0); |
| 53 | + expect(element.find('.custom-component')).toHaveLength(1); |
| 54 | + }); |
| 55 | + test('as function', () => { |
| 56 | + const customProps = { |
| 57 | + ...props, |
| 58 | + resizeHandles: ['se'], |
| 59 | + handle: (h) => <span className={`custom-component-${h}`} /> |
| 60 | + }; |
| 61 | + const element = shallow(<Resizable {...customProps}>{resizableBoxChildren}</Resizable>); |
| 62 | + expect(element.find('.custom-component-se')).toHaveLength(1); |
| 63 | + }); |
| 64 | + }); |
| 65 | +}); |
0 commit comments