11// @flow
22import React from 'react' ;
33import renderer from 'react-test-renderer' ;
4- import { shallow } from 'enzyme' ;
4+ import { shallow , mount } from 'enzyme' ;
55import { DraggableCore } from "react-draggable" ;
66
77import Resizable from '../lib/Resizable' ;
@@ -23,7 +23,6 @@ describe('render Resizable', () => {
2323 transformScale : 1 ,
2424 width : 50 ,
2525 } ;
26- const handleFn = ( axis , ref ) => < span className = { `custom-handle-${ axis } ` } ref = { ref } /> ;
2726 const userChildren = < span className = { 'children' } /> ;
2827 const resizableBoxChildren = < div style = { { width : '50px' , height : '50px' } } > { userChildren } </ div > ;
2928
@@ -46,40 +45,65 @@ describe('render Resizable', () => {
4645 expect ( cursorE ) . toHaveLength ( 1 ) ;
4746 } ) ;
4847
49- test ( 'with handle function' , ( ) => {
50- const handleFn = ( axis , ref ) => {
51- expect ( axis ) . toMatch ( / ( s e | e ) / ) ;
52- expect ( ref ) . toMatchObject ( { current : null } ) ; // ReactRef
53- return < span className = { `custom-handle-${ axis } ` } ref = { ref } /> ;
54- } ;
55- const element = shallow ( < Resizable { ...props } handle = { handleFn } > { resizableBoxChildren } </ Resizable > ) ;
56- expect ( element . find ( '.test-classname' ) . find ( '.children' ) ) ;
57- expect ( element . find ( DraggableCore ) ) . toHaveLength ( 2 ) ;
58- const cursorSe = element . find ( '.custom-handle-se' ) ;
59- const cursorE = element . find ( '.custom-handle-e' ) ;
60- expect ( cursorSe ) . toHaveLength ( 1 ) ;
61- expect ( cursorE ) . toHaveLength ( 1 ) ;
62- } ) ;
63-
64- describe ( 'and pass handle props' , ( ) => {
65- test ( 'as component' , ( ) => {
66- const customProps = {
67- ...props ,
68- resizeHandles : [ 'se' ] ,
69- handle : < span className = { 'custom-component' } />
48+ describe ( 'Handles' , ( ) => {
49+ test ( 'with handle function' , ( ) => {
50+ const handleFn = ( axis , ref ) => {
51+ expect ( axis ) . toMatch ( / ( s e | e ) / ) ;
52+ expect ( ref ) . toMatchObject ( { current : null } ) ; // ReactRef
53+ return < span className = { `custom-handle-${ axis } ` } ref = { ref } /> ;
7054 } ;
71- const element = shallow ( < Resizable { ...customProps } > { resizableBoxChildren } </ Resizable > ) ;
72- expect ( element . find ( '.react-resizable-handle-se' ) ) . toHaveLength ( 0 ) ;
73- expect ( element . find ( '.custom-component' ) ) . toHaveLength ( 1 ) ;
55+ const element = shallow ( < Resizable { ...props } handle = { handleFn } > { resizableBoxChildren } </ Resizable > ) ;
56+
57+ expect ( element . find ( '.test-classname' ) . find ( '.children' ) ) ;
58+ expect ( element . find ( DraggableCore ) ) . toHaveLength ( 2 ) ;
59+ const cursorSe = element . find ( '.custom-handle-se' ) ;
60+ const cursorE = element . find ( '.custom-handle-e' ) ;
61+ expect ( cursorSe ) . toHaveLength ( 1 ) ;
62+ expect ( cursorE ) . toHaveLength ( 1 ) ;
7463 } ) ;
75- test ( 'as function' , ( ) => {
76- const customProps = {
77- ...props ,
78- resizeHandles : [ 'se' ] ,
79- handle : ( h ) => < span className = { `custom-component-${ h } ` } />
80- } ;
81- const element = shallow ( < Resizable { ...customProps } > { resizableBoxChildren } </ Resizable > ) ;
82- expect ( element . find ( '.custom-component-se' ) ) . toHaveLength ( 1 ) ;
64+
65+ test . only ( 'with handle component' , ( ) => {
66+ const ResizeHandle = React . forwardRef ( ( props , ref ) => {
67+ // $FlowIgnore doens't know this is cloned and has handleAxis
68+ const { handleAxis, ...restProps } = props ;
69+ return (
70+ < div
71+ ref = { ref }
72+ className = { `react-resizable-handle element-handle-${ handleAxis } ` }
73+ { ...restProps }
74+ />
75+ ) ;
76+ } ) ;
77+ const element = mount ( < Resizable { ...props } handle = { < ResizeHandle /> } > { resizableBoxChildren } </ Resizable > ) ;
78+
79+ expect ( element . find ( '.test-classname' ) . find ( '.children' ) ) ;
80+ expect ( element . find ( DraggableCore ) ) . toHaveLength ( 2 ) ;
81+ const cursorSe = element . find ( '.element-handle-se' ) ;
82+ const cursorE = element . find ( '.element-handle-e' ) ;
83+ expect ( cursorSe ) . toHaveLength ( 1 ) ;
84+ expect ( cursorE ) . toHaveLength ( 1 ) ;
85+ } ) ;
86+
87+ describe ( 'and pass handle props' , ( ) => {
88+ test ( 'as component' , ( ) => {
89+ const customProps = {
90+ ...props ,
91+ resizeHandles : [ 'se' ] ,
92+ handle : < span className = { 'custom-component' } />
93+ } ;
94+ const element = shallow ( < Resizable { ...customProps } > { resizableBoxChildren } </ Resizable > ) ;
95+ expect ( element . find ( '.react-resizable-handle-se' ) ) . toHaveLength ( 0 ) ;
96+ expect ( element . find ( '.custom-component' ) ) . toHaveLength ( 1 ) ;
97+ } ) ;
98+ test ( 'as function' , ( ) => {
99+ const customProps = {
100+ ...props ,
101+ resizeHandles : [ 'se' ] ,
102+ handle : ( h ) => < span className = { `custom-component-${ h } ` } />
103+ } ;
104+ const element = shallow ( < Resizable { ...customProps } > { resizableBoxChildren } </ Resizable > ) ;
105+ expect ( element . find ( '.custom-component-se' ) ) . toHaveLength ( 1 ) ;
106+ } ) ;
83107 } ) ;
84108 } ) ;
85109
0 commit comments