1- import { addNodesWithinBounds } from '@/utilities/add-nodes-within-bounds' ;
1+ import { addNodesWithinBounds , getCoordinatesForNewNode } from '@/utilities/add-nodes-within-bounds' ;
22import { NodeProps } from '@/types' ;
33
44describe ( 'add-nodes-within-bounds' , ( ) => {
@@ -18,91 +18,132 @@ describe('add-nodes-within-bounds', () => {
1818 } ,
1919 } ,
2020 ] ;
21+
22+ const newNodes : NodeProps [ ] = [
23+ {
24+ title : 'customers' ,
25+ fields : [ ] ,
26+ type : 'collection' ,
27+ id : '2' ,
28+ measured : {
29+ height : 100 ,
30+ width : 244 ,
31+ } ,
32+ position : {
33+ x : 10 ,
34+ y : 10 , // This will be recalculated
35+ } ,
36+ } ,
37+ {
38+ title : 'products' ,
39+ fields : [ ] ,
40+ type : 'collection' ,
41+ id : '3' ,
42+ measured : {
43+ height : 100 ,
44+ width : 244 ,
45+ } ,
46+ position : {
47+ x : 10 ,
48+ y : 10 , // This will be recalculated
49+ } ,
50+ } ,
51+ ] ;
52+
2153 it ( 'With no new nodes' , ( ) => {
2254 const result = addNodesWithinBounds ( nodes , [ ] ) ;
2355 expect ( result ) . toEqual ( nodes ) ;
2456 } ) ;
25- it ( 'With no existing nodes' , ( ) => {
26- const result = addNodesWithinBounds ( [ ] , nodes ) ;
27- expect ( result ) . toEqual ( [
28- {
29- title : 'orders' ,
30- fields : [ ] ,
31- measured : {
32- height : 100 ,
33- width : 244 ,
57+
58+ describe ( 'With existing nodes and measures' , ( ) => {
59+ const expectedPosition1 = {
60+ x : 344 ,
61+ y : 312 ,
62+ } ;
63+ const expectedPosition2 = {
64+ x : 344 ,
65+ y : 512 ,
66+ } ;
67+ it ( 'addNodesWithinBounds' , ( ) => {
68+ const result = addNodesWithinBounds ( nodes , newNodes ) ;
69+ expect ( result ) . toEqual ( [
70+ ...nodes ,
71+ {
72+ ...newNodes [ 0 ] ,
73+ position : expectedPosition1 ,
3474 } ,
35- type : 'collection' ,
36- id : '1' ,
37- position : {
38- x : 344 ,
39- y : 200 ,
75+ {
76+ ...newNodes [ 1 ] ,
77+ position : expectedPosition2 ,
4078 } ,
41- } ,
42- ] ) ;
79+ ] ) ;
80+ } ) ;
81+ it ( 'getCoordinatesForNewNode' , ( ) => {
82+ const result = getCoordinatesForNewNode ( nodes , newNodes [ 0 ] ) ;
83+ expect ( result ) . toEqual ( expectedPosition1 ) ;
84+ } ) ;
4385 } ) ;
44- it ( 'With existing nodes' , ( ) => {
45- const newNodes : NodeProps [ ] = [
46- {
47- title : 'customers' ,
48- fields : [ ] ,
49- type : 'collection' ,
50- id : '2' ,
51- measured : {
52- height : 100 ,
53- width : 244 ,
54- } ,
55- position : {
56- x : 200 ,
57- y : 200 ,
58- } ,
59- } ,
60- {
61- title : 'products' ,
62- fields : [ ] ,
63- type : 'collection' ,
64- id : '3' ,
65- measured : {
66- height : 100 ,
67- width : 244 ,
68- } ,
69- position : {
70- x : 300 ,
71- y : 300 ,
72- } ,
73- } ,
74- ] ;
75- const result = addNodesWithinBounds ( nodes , newNodes ) ;
76- expect ( result ) . toEqual ( [
77- ...nodes ,
78- {
79- title : 'customers' ,
80- fields : [ ] ,
81- type : 'collection' ,
82- id : '2' ,
83- measured : {
84- height : 100 ,
85- width : 244 ,
86+
87+ describe ( 'With no existing nodes' , ( ) => {
88+ const expectedPosition = {
89+ x : 344 ,
90+ y : 200 ,
91+ } ;
92+ const expectedPosition2 = {
93+ x : 344 ,
94+ y : 400 ,
95+ } ;
96+
97+ it ( 'addNodesWithinBounds' , ( ) => {
98+ const result = addNodesWithinBounds ( [ ] as NodeProps [ ] , newNodes ) ;
99+ expect ( result ) . toEqual ( [
100+ {
101+ ...newNodes [ 0 ] ,
102+ position : expectedPosition ,
86103 } ,
87- position : {
88- x : 344 ,
89- y : 312 ,
104+ {
105+ ... newNodes [ 1 ] ,
106+ position : expectedPosition2 ,
90107 } ,
91- } ,
92- {
93- title : 'products' ,
94- fields : [ ] ,
95- type : 'collection' ,
96- id : '3' ,
97- measured : {
98- height : 100 ,
99- width : 244 ,
108+ ] ) ;
109+ } ) ;
110+
111+ it ( 'getCoordinatesForNewNode' , ( ) => {
112+ const result = getCoordinatesForNewNode ( [ ] as NodeProps [ ] , newNodes [ 0 ] ) ;
113+ expect ( result ) . toEqual ( expectedPosition ) ;
114+ } ) ;
115+ } ) ;
116+
117+ describe ( 'With no existing nodes and no measured dimensions' , ( ) => {
118+ const newNotMeasuredNodes : Omit < NodeProps , 'position' > [ ] = newNodes . map ( node => ( {
119+ ...node ,
120+ measured : undefined ,
121+ } ) ) ;
122+ const expectedPosition = {
123+ x : 344 ,
124+ y : 200 ,
125+ } ;
126+ const expectedPosition2 = {
127+ x : 344 ,
128+ y : 346 ,
129+ } ;
130+ it ( 'addNodesWithinBounds' , ( ) => {
131+ const result = addNodesWithinBounds ( [ ] as NodeProps [ ] , newNotMeasuredNodes ) ;
132+ expect ( result ) . toEqual ( [
133+ {
134+ ...newNotMeasuredNodes [ 0 ] ,
135+ position : expectedPosition ,
100136 } ,
101- position : {
102- x : 344 ,
103- y : 512 ,
137+ {
138+ ... newNotMeasuredNodes [ 1 ] ,
139+ position : expectedPosition2 ,
104140 } ,
105- } ,
106- ] ) ;
141+ ] ) ;
142+ } ) ;
143+
144+ it ( 'getCoordinatesForNewNode' , ( ) => {
145+ const result = getCoordinatesForNewNode ( [ ] , newNotMeasuredNodes [ 0 ] ) ;
146+ expect ( result ) . toEqual ( expectedPosition ) ;
147+ } ) ;
107148 } ) ;
108149} ) ;
0 commit comments