@@ -2,6 +2,7 @@ import * as React from "react";
22import { render } from "@testing-library/react" ;
33import "@testing-library/jest-dom/extend-expect" ;
44import { ViewPort } from "../ViewPort" ;
5+ import { drag } from "./TestUtils" ;
56
67const mutateComponent = ( component : React . ReactNode , newProps : Object ) => {
78 return React . cloneElement ( component as React . DetailedReactHTMLElement < any , HTMLElement > , newProps ) ;
@@ -51,3 +52,127 @@ export const commonPropsTests = (name: string, component: React.ReactNode, expec
5152 expect ( sut . nodeName ) . toBe ( "MAIN" ) ;
5253 } ) ;
5354} ;
55+
56+ export const commonAnchorTests = (
57+ name : string ,
58+ component : React . ReactNode ,
59+ size : ( style : CSSStyleDeclaration ) => string | null ,
60+ edge : ( style : CSSStyleDeclaration ) => string | null ,
61+ oppositeEdge : ( style : CSSStyleDeclaration ) => string | null ,
62+ ) => {
63+ test ( `${ name } stacked has correct styles` , async ( ) => {
64+ const { container } = render (
65+ < ViewPort >
66+ { mutateComponent ( component , { id : "test" , size : 50 , order : 0 } ) }
67+ { mutateComponent ( component , { id : "test1" , size : 100 , order : 1 } ) }
68+ </ ViewPort > ,
69+ ) ;
70+
71+ const sut = container . querySelector ( "#test" ) ! ;
72+ const style = window . getComputedStyle ( sut ) ;
73+ expect ( size ( style ) ) . toBe ( "50px" ) ;
74+ expect ( edge ( style ) ) . toBe ( "0px" ) ;
75+ expect ( oppositeEdge ( style ) ) . toBe ( "" ) ;
76+
77+ const sut1 = container . querySelector ( "#test1" ) ! ;
78+ const style1 = window . getComputedStyle ( sut1 ) ;
79+ expect ( size ( style1 ) ) . toBe ( "100px" ) ;
80+ expect ( edge ( style1 ) ) . toBe ( "calc(0px + 50px)" ) ;
81+ expect ( oppositeEdge ( style1 ) ) . toBe ( "" ) ;
82+ } ) ;
83+
84+ test ( `${ name } stacked DOM reversed has correct styles` , async ( ) => {
85+ const { container } = render (
86+ < ViewPort >
87+ { mutateComponent ( component , { id : "test1" , size : 100 , order : 1 } ) }
88+ { mutateComponent ( component , { id : "test" , size : 50 , order : 0 } ) }
89+ </ ViewPort > ,
90+ ) ;
91+
92+ const sut = container . querySelector ( "#test" ) ! ;
93+ const style = window . getComputedStyle ( sut ) ;
94+ expect ( size ( style ) ) . toBe ( "50px" ) ;
95+ expect ( edge ( style ) ) . toBe ( "0px" ) ;
96+ expect ( oppositeEdge ( style ) ) . toBe ( "" ) ;
97+
98+ const sut1 = container . querySelector ( "#test1" ) ! ;
99+ const style1 = window . getComputedStyle ( sut1 ) ;
100+ expect ( size ( style1 ) ) . toBe ( "100px" ) ;
101+ expect ( edge ( style1 ) ) . toBe ( "calc(0px + 50px)" ) ;
102+ expect ( oppositeEdge ( style1 ) ) . toBe ( "" ) ;
103+ } ) ;
104+ } ;
105+
106+ export const commonResizableTests = (
107+ name : string ,
108+ component : React . ReactNode ,
109+ size : ( style : CSSStyleDeclaration ) => string | null ,
110+ edge : ( style : CSSStyleDeclaration ) => string | null ,
111+ oppositeEdge : ( style : CSSStyleDeclaration ) => string | null ,
112+ horizontal : boolean ,
113+ negate : boolean ,
114+ ) => {
115+ test ( `${ name } after resize has correct styles` , async ( ) => {
116+ const { container } = render ( < ViewPort > { mutateComponent ( component , { id : "test" } ) } </ ViewPort > ) ;
117+ const sut = container . querySelector ( "#test" ) ! ;
118+ const resizeHandle = container . querySelector ( "#test .spaces-resize-handle" ) ! ;
119+
120+ drag (
121+ resizeHandle ,
122+ sut ,
123+ { width : horizontal ? 50 : 0 , height : horizontal ? 0 : 50 } ,
124+ { width : horizontal ? 150 : 0 , height : horizontal ? 0 : 150 } ,
125+ horizontal ? ( negate ? - 100 : 100 ) : 0 ,
126+ horizontal ? 0 : negate ? - 100 : 100 ,
127+ ) ;
128+
129+ const style = window . getComputedStyle ( sut ) ;
130+ expect ( size ( style ) ) . toBe ( "calc(50px + 100px)" ) ;
131+ } ) ;
132+
133+ test ( `${ name } subsequent resize has correct styles` , async ( ) => {
134+ const { container } = render ( < ViewPort > { mutateComponent ( component , { id : "test" } ) } </ ViewPort > ) ;
135+ const sut = container . querySelector ( "#test" ) ! ;
136+ const resizeHandle = container . querySelector ( "#test .spaces-resize-handle" ) ! ;
137+
138+ drag (
139+ resizeHandle ,
140+ sut ,
141+ { width : horizontal ? 50 : 0 , height : horizontal ? 0 : 50 } ,
142+ { width : horizontal ? 150 : 0 , height : horizontal ? 0 : 150 } ,
143+ horizontal ? ( negate ? - 100 : 100 ) : 0 ,
144+ horizontal ? 0 : negate ? - 100 : 100 ,
145+ ) ;
146+ drag (
147+ resizeHandle ,
148+ sut ,
149+ { width : horizontal ? 150 : 0 , height : horizontal ? 0 : 150 } ,
150+ { width : horizontal ? 50 : 0 , height : horizontal ? 0 : 50 } ,
151+ horizontal ? ( negate ? 100 : - 100 ) : 0 ,
152+ horizontal ? 0 : negate ? 100 : - 100 ,
153+ ) ;
154+
155+ const style = window . getComputedStyle ( sut ) ;
156+ expect ( size ( style ) ) . toBe ( "50px" ) ;
157+ } ) ;
158+
159+ test ( `${ name } resize then props size change has correct styles` , async ( ) => {
160+ const { container, rerender } = render ( < ViewPort > { mutateComponent ( component , { id : "test" , size : 50 } ) } </ ViewPort > ) ;
161+ const sut = container . querySelector ( "#test" ) ! ;
162+ const resizeHandle = container . querySelector ( "#test .spaces-resize-handle" ) ! ;
163+
164+ drag (
165+ resizeHandle ,
166+ sut ,
167+ { width : horizontal ? 50 : 0 , height : horizontal ? 0 : 50 } ,
168+ { width : horizontal ? 150 : 0 , height : horizontal ? 0 : 150 } ,
169+ horizontal ? ( negate ? - 100 : 100 ) : 0 ,
170+ horizontal ? 0 : negate ? - 100 : 100 ,
171+ ) ;
172+
173+ rerender ( < ViewPort > { mutateComponent ( component , { id : "test" , size : 150 } ) } </ ViewPort > ) ;
174+
175+ const style = window . getComputedStyle ( sut ) ;
176+ expect ( size ( style ) ) . toBe ( "150px" ) ;
177+ } ) ;
178+ } ;
0 commit comments