11import { Rect } from '../../@types' ;
22import { annotation } from '../__mocks__/data' ;
3- import { centerRegion , centerShape , isRegion , styleShape } from '../regionUtil' ;
3+ import {
4+ centerRegion ,
5+ centerShape ,
6+ getPoints ,
7+ getTransformedShape ,
8+ isRegion ,
9+ styleShape ,
10+ selectTransformationPoint ,
11+ } from '../regionUtil' ;
412
513describe ( 'regionUtil' , ( ) => {
614 const getRect = ( ) : Rect => ( {
@@ -10,6 +18,12 @@ describe('regionUtil', () => {
1018 x : 10 ,
1119 y : 10 ,
1220 } ) ;
21+ const parseValue = ( value : number ) : number => parseFloat ( value . toFixed ( 3 ) ) ;
22+
23+ const regionShape = { height : 2 , width : 3 , x : 1 , y : 1 } ;
24+ const regionShapeRotated90 = { height : 3 , width : 2 , x : 1 , y : 96 } ;
25+ const regionShapeRotated180 = { height : 2 , width : 3 , x : 96 , y : 97 } ;
26+ const regionShapeRotated270 = { height : 3 , width : 2 , x : 97 , y : 1 } ;
1327
1428 describe ( 'centerShape()' , ( ) => {
1529 test ( 'should return the position of the center of the shape' , ( ) => {
@@ -27,6 +41,61 @@ describe('regionUtil', () => {
2741 } ) ;
2842 } ) ;
2943
44+ describe ( 'getPoints()' , ( ) => {
45+ test ( 'should return the points based on a region shape' , ( ) => {
46+ const [ p1 , p2 , p3 , p4 ] = getPoints ( regionShape ) ;
47+ expect ( p1 ) . toEqual ( { x : 1 , y : 1 } ) ;
48+ expect ( p2 ) . toEqual ( { x : 4 , y : 1 } ) ;
49+ expect ( p3 ) . toEqual ( { x : 4 , y : 3 } ) ;
50+ expect ( p4 ) . toEqual ( { x : 1 , y : 3 } ) ;
51+ } ) ;
52+ } ) ;
53+
54+ describe ( 'selectTransformationPoint()' , ( ) => {
55+ test . each `
56+ rotation | expectedPoint
57+ ${ 0 } | ${ { x : 1 , y : 1 } }
58+ ${ - 90 } | ${ { x : 4 , y : 1 } }
59+ ${ - 180 } | ${ { x : 4 , y : 3 } }
60+ ${ - 270 } | ${ { x : 1 , y : 3 } }
61+ ${ - 360 } | ${ { x : 1 , y : 1 } }
62+ ` (
63+ 'should return the appropriate point if shape=$shape and rotation=$rotation' ,
64+ ( { rotation, expectedPoint } ) => {
65+ expect ( selectTransformationPoint ( regionShape , rotation ) ) . toEqual ( expectedPoint ) ;
66+ } ,
67+ ) ;
68+ } ) ;
69+
70+ describe ( 'getTransformedShape()' , ( ) => {
71+ test . each `
72+ rotation | expectedShape
73+ ${ 0 } | ${ regionShape }
74+ ${ - 90 } | ${ regionShapeRotated90 }
75+ ${ - 180 } | ${ regionShapeRotated180 }
76+ ${ - 270 } | ${ regionShapeRotated270 }
77+ ${ - 360 } | ${ regionShape }
78+ ` (
79+ 'should return the transformed shape based on rotation=$rotation and reference element' ,
80+ ( { rotation, expectedShape } ) => {
81+ const { height : expHeight , width : expWidth , x : expX , y : expY } = expectedShape ;
82+ const { height, width, x = NaN , y = NaN } = getTransformedShape ( regionShape , rotation ) || { } ;
83+
84+ expect ( { height, width } ) . toEqual ( { height : expHeight , width : expWidth } ) ;
85+ expect ( parseValue ( x ) ) . toEqual ( parseValue ( expX ) ) ;
86+ expect ( parseValue ( y ) ) . toEqual ( parseValue ( expY ) ) ;
87+ } ,
88+ ) ;
89+
90+ test ( 'should transform -90deg shape back to an unrotated state' , ( ) => {
91+ const { height, width, x, y } = getTransformedShape ( regionShapeRotated90 , - 270 ) ;
92+ const { height : expHeight , width : expWidth , x : expX , y : expY } = regionShape ;
93+ expect ( { height, width } ) . toEqual ( { height : expHeight , width : expWidth } ) ;
94+ expect ( parseValue ( x ) ) . toEqual ( parseValue ( expX ) ) ;
95+ expect ( parseValue ( y ) ) . toEqual ( parseValue ( expY ) ) ;
96+ } ) ;
97+ } ) ;
98+
3099 describe ( 'isRegion()' , ( ) => {
31100 test . each `
32101 type | result
0 commit comments