@@ -48,4 +48,53 @@ describe('mutateMergeDeep', () => {
48
48
expect ( target . user . details ) . toBe ( originalDetails )
49
49
expect ( target . user . details . name ) . toBe ( 'test' )
50
50
} )
51
+
52
+ it ( 'Should merge two objects by mutating' , ( ) => {
53
+ const a = { a : 1 }
54
+ const b = { b : 2 }
55
+ mutateMergeDeep ( a , b )
56
+ expect ( a ) . toStrictEqual ( { a : 1 , b : 2 } )
57
+ } )
58
+
59
+ it ( 'Should merge two objects including overwriting with undefined' , ( ) => {
60
+ const a = { a : 1 }
61
+ const b = { a : undefined }
62
+ mutateMergeDeep ( a , b )
63
+ expect ( a ) . toStrictEqual ( { a : undefined } )
64
+ } )
65
+
66
+ it ( 'Should merge two object by overriding arrays' , ( ) => {
67
+ const target = { a : [ 1 ] }
68
+ const source = { a : [ 2 ] }
69
+ mutateMergeDeep ( target , source )
70
+ expect ( target ) . toStrictEqual ( { a : [ 2 ] } )
71
+ } )
72
+
73
+ it ( 'Should merge add array element when it does not exist in target' , ( ) => {
74
+ const target = { a : [ ] }
75
+ const source = { a : [ 2 ] }
76
+ mutateMergeDeep ( target , source )
77
+ expect ( target ) . toStrictEqual ( { a : [ 2 ] } )
78
+ } )
79
+
80
+ it ( 'Should override the target array if source is undefined' , ( ) => {
81
+ const target = { a : [ 2 ] }
82
+ const source = { a : undefined }
83
+ mutateMergeDeep ( target , source )
84
+ expect ( target ) . toStrictEqual ( { a : undefined } )
85
+ } )
86
+
87
+ it ( 'Should merge update array element when it does not exist in source' , ( ) => {
88
+ const target = { a : [ 2 ] }
89
+ const source = { a : [ ] }
90
+ mutateMergeDeep ( target , source )
91
+ expect ( target ) . toStrictEqual ( { a : [ ] } )
92
+ } )
93
+
94
+ it ( 'Should merge two deeply nested objects' , ( ) => {
95
+ const a = { a : { a : 1 } }
96
+ const b = { a : { b : 2 } }
97
+ mutateMergeDeep ( a , b )
98
+ expect ( a ) . toStrictEqual ( { a : { a : 1 , b : 2 } } )
99
+ } )
51
100
} )
0 commit comments