@@ -129,4 +129,116 @@ describe('gridstack utils', function() {
129
129
} ) ;
130
130
} ) ;
131
131
132
+ describe ( 'clone' , ( ) => {
133
+ const a : any = { first : 1 , second : 'text' } ;
134
+ const b : any = { first : 1 , second : { third : 3 } } ;
135
+ const c : any = { first : 1 , second : [ 1 , 2 , 3 ] , third : { fourth : { fifth : 5 } } } ;
136
+ it ( 'Should have the same values' , ( ) => {
137
+ const z = Utils . clone ( a ) ;
138
+ expect ( z ) . toEqual ( { first : 1 , second : 'text' } ) ;
139
+ } ) ;
140
+ it ( 'Should have 2 in first key, and original unchanged' , ( ) => {
141
+ const z = Utils . clone ( a ) ;
142
+ z . first = 2 ;
143
+ expect ( a ) . toEqual ( { first : 1 , second : 'text' } ) ;
144
+ expect ( z ) . toEqual ( { first : 2 , second : 'text' } ) ;
145
+ } ) ;
146
+ it ( 'Should have new string in second key, and original unchanged' , ( ) => {
147
+ const z = Utils . clone ( a ) ;
148
+ z . second = 2 ;
149
+ expect ( a ) . toEqual ( { first : 1 , second : 'text' } ) ;
150
+ expect ( z ) . toEqual ( { first : 1 , second : 2 } ) ;
151
+ } ) ;
152
+ it ( 'new string in both cases - use cloneDeep instead' , ( ) => {
153
+ const z = Utils . clone ( b ) ;
154
+ z . second . third = 'share' ;
155
+ expect ( b ) . toEqual ( { first : 1 , second : { third : 'share' } } ) ;
156
+ expect ( z ) . toEqual ( { first : 1 , second : { third : 'share' } } ) ;
157
+ } ) ;
158
+ it ( 'Array Should match' , ( ) => {
159
+ const z = Utils . clone ( c ) ;
160
+ expect ( c ) . toEqual ( { first : 1 , second : [ 1 , 2 , 3 ] , third : { fourth : { fifth : 5 } } } ) ;
161
+ expect ( z ) . toEqual ( { first : 1 , second : [ 1 , 2 , 3 ] , third : { fourth : { fifth : 5 } } } ) ;
162
+ } ) ;
163
+ it ( 'Array[0] changed in both cases - use cloneDeep instead' , ( ) => {
164
+ const z = Utils . clone ( c ) ;
165
+ z . second [ 0 ] = 0 ;
166
+ expect ( c ) . toEqual ( { first : 1 , second : [ 0 , 2 , 3 ] , third : { fourth : { fifth : 5 } } } ) ;
167
+ expect ( z ) . toEqual ( { first : 1 , second : [ 0 , 2 , 3 ] , third : { fourth : { fifth : 5 } } } ) ;
168
+ } ) ;
169
+ it ( 'fifth changed in both cases - use cloneDeep instead' , ( ) => {
170
+ const z = Utils . clone ( c ) ;
171
+ z . third . fourth . fifth = 'share' ;
172
+ expect ( c ) . toEqual ( { first : 1 , second : [ 0 , 2 , 3 ] , third : { fourth : { fifth : 'share' } } } ) ;
173
+ expect ( z ) . toEqual ( { first : 1 , second : [ 0 , 2 , 3 ] , third : { fourth : { fifth : 'share' } } } ) ;
174
+ } ) ;
175
+ } ) ;
176
+ describe ( 'cloneDeep' , ( ) => {
177
+ // reset our test cases
178
+ const a : any = { first : 1 , second : 'text' } ;
179
+ const b : any = { first : 1 , second : { third : 3 } } ;
180
+ const c : any = { first : 1 , second : [ 1 , 2 , 3 ] , third : { fourth : { fifth : 5 } } } ;
181
+ const d : any = { first : [ 1 , [ 2 , 3 ] , [ 'four' , 'five' , 'six' ] ] } ;
182
+ const e : any = { first : 1 , __skip : { second : 2 } } ;
183
+ const f : any = { first : 1 , _dontskip : { second : 2 } } ;
184
+
185
+ it ( 'Should have the same values' , ( ) => {
186
+ const z = Utils . cloneDeep ( a ) ;
187
+ expect ( z ) . toEqual ( { first : 1 , second : 'text' } ) ;
188
+ } ) ;
189
+ it ( 'Should have 2 in first key, and original unchanged' , ( ) => {
190
+ const z = Utils . cloneDeep ( a ) ;
191
+ z . first = 2 ;
192
+ expect ( a ) . toEqual ( { first : 1 , second : 'text' } ) ;
193
+ expect ( z ) . toEqual ( { first : 2 , second : 'text' } ) ;
194
+ } ) ;
195
+ it ( 'Should have new string in second key, and original unchanged' , ( ) => {
196
+ const z = Utils . cloneDeep ( a ) ;
197
+ z . second = 2 ;
198
+ expect ( a ) . toEqual ( { first : 1 , second : 'text' } ) ;
199
+ expect ( z ) . toEqual ( { first : 1 , second : 2 } ) ;
200
+ } ) ;
201
+ it ( 'Should have new string nested object, and original unchanged' , ( ) => {
202
+ const z = Utils . cloneDeep ( b ) ;
203
+ z . second . third = 'diff' ;
204
+ expect ( b ) . toEqual ( { first : 1 , second : { third : 3 } } ) ;
205
+ expect ( z ) . toEqual ( { first : 1 , second : { third : 'diff' } } ) ;
206
+ } ) ;
207
+ it ( 'Array Should match' , ( ) => {
208
+ const z = Utils . cloneDeep ( c ) ;
209
+ expect ( c ) . toEqual ( { first : 1 , second : [ 1 , 2 , 3 ] , third : { fourth : { fifth : 5 } } } ) ;
210
+ expect ( z ) . toEqual ( { first : 1 , second : [ 1 , 2 , 3 ] , third : { fourth : { fifth : 5 } } } ) ;
211
+ } ) ;
212
+ it ( 'Array[0] changed in z only' , ( ) => {
213
+ const z = Utils . cloneDeep ( c ) ;
214
+ z . second [ 0 ] = 0 ;
215
+ expect ( c ) . toEqual ( { first : 1 , second : [ 1 , 2 , 3 ] , third : { fourth : { fifth : 5 } } } ) ;
216
+ expect ( z ) . toEqual ( { first : 1 , second : [ 0 , 2 , 3 ] , third : { fourth : { fifth : 5 } } } ) ;
217
+ } ) ;
218
+ it ( 'nested firth element changed only in z' , ( ) => {
219
+ const z = Utils . cloneDeep ( c ) ;
220
+ z . third . fourth . fifth = 'diff' ;
221
+ expect ( c ) . toEqual ( { first : 1 , second : [ 1 , 2 , 3 ] , third : { fourth : { fifth : 5 } } } ) ;
222
+ expect ( z ) . toEqual ( { first : 1 , second : [ 1 , 2 , 3 ] , third : { fourth : { fifth : 'diff' } } } ) ;
223
+ } ) ;
224
+ it ( 'nested array only has one item changed' , ( ) => {
225
+ const z = Utils . cloneDeep ( d ) ;
226
+ z . first [ 1 ] = 'two' ;
227
+ z . first [ 2 ] [ 2 ] = 6 ;
228
+ expect ( d ) . toEqual ( { first : [ 1 , [ 2 , 3 ] , [ 'four' , 'five' , 'six' ] ] } ) ;
229
+ expect ( z ) . toEqual ( { first : [ 1 , 'two' , [ 'four' , 'five' , 6 ] ] } ) ;
230
+ } ) ;
231
+ it ( 'skip __ items so it mods both instance' , ( ) => {
232
+ const z = Utils . cloneDeep ( e ) ;
233
+ z . __skip . second = 'two' ;
234
+ expect ( e ) . toEqual ( { first : 1 , __skip : { second : 'two' } } ) ; // TODO support clone deep of function workaround
235
+ expect ( z ) . toEqual ( { first : 1 , __skip : { second : 'two' } } ) ;
236
+ } ) ;
237
+ it ( 'correctly copy _ item' , ( ) => {
238
+ const z = Utils . cloneDeep ( f ) ;
239
+ z . _dontskip . second = 'two' ;
240
+ expect ( f ) . toEqual ( { first : 1 , _dontskip : { second : 2 } } ) ;
241
+ expect ( z ) . toEqual ( { first : 1 , _dontskip : { second : 'two' } } ) ;
242
+ } ) ;
243
+ } ) ;
132
244
} ) ;
0 commit comments