1
1
import { applyLayout } from '@/utilities/apply-layout' ;
2
- import { EdgeProps , NodeProps } from '@/types' ;
2
+ import { BaseNode , EdgeProps , NodeProps } from '@/types' ;
3
3
4
4
describe ( 'apply-layout' , ( ) => {
5
5
const nodes : NodeProps [ ] = [
@@ -50,37 +50,121 @@ describe('apply-layout', () => {
50
50
edges : [ ] ,
51
51
} ) ;
52
52
} ) ;
53
- it ( 'With nodes' , async ( ) => {
53
+ it ( 'With nodes (not measured, 0 fields) ' , async ( ) => {
54
54
const result = await applyLayout < NodeProps , EdgeProps > ( nodes , [ ] , 'TOP_BOTTOM' ) ;
55
55
expect ( result . nodes ) . toEqual ( [
56
56
expect . objectContaining ( {
57
- title : 'orders' ,
58
- fields : [ ] ,
59
- type : 'collection' ,
60
- id : '1' ,
57
+ ...nodes [ 0 ] ,
61
58
position : {
62
59
x : 12 ,
63
60
y : 12 ,
64
61
} ,
65
62
} ) ,
66
63
expect . objectContaining ( {
67
- title : 'customers' ,
68
- fields : [ ] ,
69
- type : 'collection' ,
70
- id : '2' ,
64
+ ...nodes [ 1 ] ,
71
65
position : {
72
66
x : 12 ,
73
- y : 32 ,
67
+ y : 76 , // 12 + 44 (0 fields height) + 2*10 (padding)
74
68
} ,
75
69
} ) ,
76
70
expect . objectContaining ( {
77
- title : 'products' ,
78
- fields : [ ] ,
79
- type : 'collection' ,
80
- id : '3' ,
71
+ ...nodes [ 2 ] ,
72
+ position : {
73
+ x : 12 ,
74
+ y : 140 , // 76 + 44 (0 fields height) + 2*10 (padding)
75
+ } ,
76
+ } ) ,
77
+ ] ) ;
78
+ } ) ;
79
+ it ( 'With nodes (not measured, 1 field)' , async ( ) => {
80
+ const nodesWithOneField = nodes . map ( node => ( {
81
+ ...node ,
82
+ fields : [ { name : 'field1' , type : 'string' } ] ,
83
+ } ) ) ;
84
+ const result = await applyLayout < NodeProps , EdgeProps > ( nodesWithOneField , [ ] , 'TOP_BOTTOM' ) ;
85
+ expect ( result . nodes ) . toEqual ( [
86
+ expect . objectContaining ( {
87
+ ...nodesWithOneField [ 0 ] ,
88
+ position : {
89
+ x : 12 ,
90
+ y : 12 ,
91
+ } ,
92
+ } ) ,
93
+ expect . objectContaining ( {
94
+ ...nodesWithOneField [ 1 ] ,
95
+ position : {
96
+ x : 12 ,
97
+ y : 94 , // 12 + 62 (1 field height) + 2*10 (padding)
98
+ } ,
99
+ } ) ,
100
+ expect . objectContaining ( {
101
+ ...nodesWithOneField [ 2 ] ,
102
+ position : {
103
+ x : 12 ,
104
+ y : 176 , // 94 + 62 (1 field height) + 2*10 (padding)
105
+ } ,
106
+ } ) ,
107
+ ] ) ;
108
+ } ) ;
109
+ it ( 'With nodes (not measured, undefined fields)' , async ( ) => {
110
+ const baseNodes = nodes . map ( node => ( {
111
+ ...node ,
112
+ fields : undefined ,
113
+ } ) ) ;
114
+ const result = await applyLayout < BaseNode , EdgeProps > ( baseNodes , [ ] , 'TOP_BOTTOM' ) ;
115
+ expect ( result . nodes ) . toEqual ( [
116
+ expect . objectContaining ( {
117
+ ...baseNodes [ 0 ] ,
118
+ position : {
119
+ x : 12 ,
120
+ y : 12 ,
121
+ } ,
122
+ } ) ,
123
+ expect . objectContaining ( {
124
+ ...baseNodes [ 1 ] ,
125
+ position : {
126
+ x : 12 ,
127
+ y : 94 , // 12 + 62 (default height) + 2*10 (padding)
128
+ } ,
129
+ } ) ,
130
+ expect . objectContaining ( {
131
+ ...baseNodes [ 2 ] ,
132
+ position : {
133
+ x : 12 ,
134
+ y : 176 , // 94 + 62 (default height) + 2*10 (padding)
135
+ } ,
136
+ } ) ,
137
+ ] ) ;
138
+ } ) ;
139
+ it ( 'With nodes (measured)' , async ( ) => {
140
+ const measuredNodes = nodes . map ( node => ( {
141
+ ...node ,
142
+ measured : {
143
+ width : 100 ,
144
+ height : 50 ,
145
+ } ,
146
+ } ) ) ;
147
+ const result = await applyLayout < NodeProps , EdgeProps > ( measuredNodes , [ ] , 'TOP_BOTTOM' ) ;
148
+ expect ( result . nodes ) . toEqual ( [
149
+ expect . objectContaining ( {
150
+ ...measuredNodes [ 0 ] ,
151
+ position : {
152
+ x : 12 ,
153
+ y : 12 ,
154
+ } ,
155
+ } ) ,
156
+ expect . objectContaining ( {
157
+ ...measuredNodes [ 1 ] ,
81
158
position : {
82
159
x : 12 ,
83
- y : 52 ,
160
+ y : 82 , // 12 + 50 (measured node height) + 2*10 (padding)
161
+ } ,
162
+ } ) ,
163
+ expect . objectContaining ( {
164
+ ...measuredNodes [ 2 ] ,
165
+ position : {
166
+ x : 12 ,
167
+ y : 152 , // 82 + 50 (measured node height) + 2*10 (padding)
84
168
} ,
85
169
} ) ,
86
170
] ) ;
@@ -104,7 +188,7 @@ describe('apply-layout', () => {
104
188
id : '1' ,
105
189
position : {
106
190
x : 12 ,
107
- y : 12 ,
191
+ y : 76 ,
108
192
} ,
109
193
} ) ,
110
194
expect . objectContaining ( {
@@ -114,7 +198,7 @@ describe('apply-layout', () => {
114
198
id : '2' ,
115
199
position : {
116
200
x : 12 ,
117
- y : 132 ,
201
+ y : 12 ,
118
202
} ,
119
203
} ) ,
120
204
expect . objectContaining ( {
@@ -124,7 +208,7 @@ describe('apply-layout', () => {
124
208
id : '3' ,
125
209
position : {
126
210
x : 12 ,
127
- y : 112 ,
211
+ y : 220 ,
128
212
} ,
129
213
} ) ,
130
214
] ) ;
0 commit comments