@@ -4,6 +4,7 @@ import "@testing-library/jest-dom/extend-expect";
44import { ViewPort } from "../ViewPort" ;
55import { drag } from "./TestUtils" ;
66import { ResizeType } from "../../core-types" ;
7+ import { Positioned } from "../Positioned" ;
78
89export const mutateComponent = ( component : React . ReactNode , newProps : Object ) => {
910 return React . cloneElement ( component as React . DetailedReactHTMLElement < any , HTMLElement > , newProps ) ;
@@ -291,70 +292,78 @@ export const commonResizableTests = (
291292
292293export const commonPositionedTests = (
293294 name : string ,
294- component : React . ReactNode ,
295295 size : ( style : CSSStyleDeclaration ) => string | null ,
296296 edge : ( style : CSSStyleDeclaration ) => string | null ,
297297 oppositeEdge : ( style : CSSStyleDeclaration ) => string | null ,
298298 handle : string ,
299299 horizontal : boolean ,
300300 negate : boolean ,
301301) => {
302- test ( `${ name } after resize has correct styles` , async ( ) => {
303- // arrange
304- const { container } = render (
305- < ViewPort >
306- { mutateComponent ( component , { id : "test" , resizable : [ ResizeType . Left , ResizeType . Top , ResizeType . Bottom , ResizeType . Right ] } ) }
307- </ ViewPort > ,
308- ) ;
309-
310- const resizeHandle = container . querySelector ( `#test-${ handle } ` ) ! ;
311- const sut = container . querySelector ( "#test" ) ! ;
312-
313- // act
314- drag (
315- resizeHandle ,
316- sut ,
317- { width : horizontal ? 50 : 0 , height : horizontal ? 0 : 50 } ,
318- { width : horizontal ? 150 : 0 , height : horizontal ? 0 : 150 } ,
319- horizontal ? ( negate ? - 100 : 100 ) : 0 ,
320- horizontal ? 0 : negate ? - 100 : 100 ,
321- ) ;
322-
323- // assert
324- const style = window . getComputedStyle ( sut ) ;
325- expect ( size ( style ) ) . toBe ( "calc(100px + -100px)" ) ;
326- } ) ;
327-
328- test ( `${ name } subsequent resize has correct styles` , async ( ) => {
329- // arrange
330- const { container } = render (
331- < ViewPort >
332- { mutateComponent ( component , { id : "test" , resizable : [ ResizeType . Left , ResizeType . Top , ResizeType . Bottom , ResizeType . Right ] } ) }
333- </ ViewPort > ,
334- ) ;
335- const resizeHandle = container . querySelector ( `#test-${ handle } ` ) ! ;
336- const sut = container . querySelector ( "#test" ) ! ;
337-
338- // act
339- drag (
340- resizeHandle ,
341- sut ,
342- { width : horizontal ? 50 : 0 , height : horizontal ? 0 : 50 } ,
343- { width : horizontal ? 150 : 0 , height : horizontal ? 0 : 150 } ,
344- horizontal ? ( negate ? - 100 : 100 ) : 0 ,
345- horizontal ? 0 : negate ? - 100 : 100 ,
346- ) ;
347- drag (
348- resizeHandle ,
349- sut ,
350- { width : horizontal ? 150 : 0 , height : horizontal ? 0 : 150 } ,
351- { width : horizontal ? 50 : 0 , height : horizontal ? 0 : 50 } ,
352- horizontal ? ( negate ? 100 : - 100 ) : 0 ,
353- horizontal ? 0 : negate ? 100 : - 100 ,
354- ) ;
302+ const testProps = { id : "test" , resizable : [ ResizeType . Left , ResizeType . Top , ResizeType . Bottom , ResizeType . Right ] } ;
303+
304+ [
305+ { name : "left/top/width/height" , props : { left : 100 , top : 100 , width : 100 , height : 100 } , widthHeightSpecified : true } ,
306+ { name : "left/top/right/bottom" , props : { left : 100 , top : 100 , right : 100 , bottom : 100 } , widthHeightSpecified : false } ,
307+ ] . forEach ( ( testCase ) => {
308+ test ( `${ name } (${ testCase . name } ) after resize has correct styles` , async ( ) => {
309+ // arrange
310+ const { container } = render ( < ViewPort > { mutateComponent ( < Positioned /> , { ...testProps , ...testCase . props } ) } </ ViewPort > ) ;
311+
312+ const resizeHandle = container . querySelector ( `#test-${ handle } ` ) ! ;
313+ const sut = container . querySelector ( "#test" ) ! ;
314+
315+ // act
316+ drag (
317+ resizeHandle ,
318+ sut ,
319+ { width : horizontal ? 50 : 0 , height : horizontal ? 0 : 50 } ,
320+ { width : horizontal ? 150 : 0 , height : horizontal ? 0 : 150 } ,
321+ horizontal ? ( negate ? - 100 : 100 ) : 0 ,
322+ horizontal ? 0 : negate ? - 100 : 100 ,
323+ ) ;
324+
325+ // assert
326+ const style = window . getComputedStyle ( sut ) ;
327+
328+ if ( testCase . widthHeightSpecified ) {
329+ expect ( size ( style ) ) . toBe ( "calc(100px + -100px)" ) ;
330+ } else {
331+ expect ( edge ( style ) ) . toBe ( "calc(100px + 100px)" ) ;
332+ }
333+ } ) ;
355334
356- // assert
357- const style = window . getComputedStyle ( sut ) ;
358- expect ( size ( style ) ) . toBe ( "100px" ) ;
335+ test ( `${ name } (${ testCase . name } ) subsequent resize has correct styles` , async ( ) => {
336+ // arrange
337+ const { container } = render ( < ViewPort > { mutateComponent ( < Positioned /> , { ...testProps , ...testCase . props } ) } </ ViewPort > ) ;
338+ const resizeHandle = container . querySelector ( `#test-${ handle } ` ) ! ;
339+ const sut = container . querySelector ( "#test" ) ! ;
340+
341+ // act
342+ drag (
343+ resizeHandle ,
344+ sut ,
345+ { width : horizontal ? 50 : 0 , height : horizontal ? 0 : 50 } ,
346+ { width : horizontal ? 150 : 0 , height : horizontal ? 0 : 150 } ,
347+ horizontal ? ( negate ? - 100 : 100 ) : 0 ,
348+ horizontal ? 0 : negate ? - 100 : 100 ,
349+ ) ;
350+ drag (
351+ resizeHandle ,
352+ sut ,
353+ { width : horizontal ? 150 : 0 , height : horizontal ? 0 : 150 } ,
354+ { width : horizontal ? 50 : 0 , height : horizontal ? 0 : 50 } ,
355+ horizontal ? ( negate ? 100 : - 100 ) : 0 ,
356+ horizontal ? 0 : negate ? 100 : - 100 ,
357+ ) ;
358+
359+ // assert
360+ const style = window . getComputedStyle ( sut ) ;
361+
362+ if ( testCase . widthHeightSpecified ) {
363+ expect ( size ( style ) ) . toBe ( "100px" ) ;
364+ } else {
365+ expect ( edge ( style ) ) . toBe ( "100px" ) ;
366+ }
367+ } ) ;
359368 } ) ;
360369} ;
0 commit comments