@@ -65,7 +65,7 @@ describe('upsert', () => {
65
65
upsert (
66
66
originalCreatures ,
67
67
creaturesForUpdate [ 0 ] ,
68
- ( o , n ) => o . id === n . id
68
+ ( o , n ) => o . id === n . id ,
69
69
) ;
70
70
71
71
expect ( originalCreatures ) . toEqual ( creatures ) ;
@@ -76,7 +76,7 @@ describe('upsert', () => {
76
76
const result = upsert (
77
77
originalCreatures ,
78
78
creaturesForUpdate [ 0 ] ,
79
- ( o , n ) => o . id === n . id
79
+ ( o , n ) => o . id === n . id ,
80
80
) ;
81
81
const result2 = upsert ( null as any , originalCreatures ) ;
82
82
@@ -98,25 +98,25 @@ describe('upsert', () => {
98
98
describe ( 'functionality' , ( ) => {
99
99
it ( 'should update value if matching by compareFn' , ( ) => {
100
100
expect (
101
- upsert ( creatures , creaturesForUpdate , ( a , b ) => a . id === b . id )
101
+ upsert ( creatures , creaturesForUpdate , ( a , b ) => a . id === b . id ) ,
102
102
) . toEqual ( creaturesAfterMultipleItemsUpdate ) ;
103
103
} ) ;
104
104
105
105
it ( 'should update value if matching by key' , ( ) => {
106
106
expect ( upsert ( creatures , creaturesForUpdate , 'id' ) ) . toEqual (
107
- creaturesAfterMultipleItemsUpdate
107
+ creaturesAfterMultipleItemsUpdate ,
108
108
) ;
109
109
} ) ;
110
110
111
111
it ( 'should update value if matching by array of keys' , ( ) => {
112
112
expect ( upsert ( creatures , creaturesForUpdate , [ 'id' ] ) ) . toEqual (
113
- creaturesAfterMultipleItemsUpdate
113
+ creaturesAfterMultipleItemsUpdate ,
114
114
) ;
115
115
} ) ;
116
116
117
117
it ( 'should update partials' , ( ) => {
118
118
expect ( upsert ( creatures , { id : 1 , type : 'lion' } , 'id' ) ) . toEqual (
119
- creaturesAfterSingleItemUpdate
119
+ creaturesAfterSingleItemUpdate ,
120
120
) ;
121
121
} ) ;
122
122
} ) ;
@@ -318,4 +318,56 @@ describe('upsert', () => {
318
318
} ) ;
319
319
} ) ;
320
320
} ) ;
321
+
322
+ describe ( 'prototype preservation' , ( ) => {
323
+ class TestClass {
324
+ constructor (
325
+ public id : number ,
326
+ public value : string ,
327
+ ) { }
328
+
329
+ getDescription ( ) : string {
330
+ return `${ this . id } : ${ this . value } ` ;
331
+ }
332
+ }
333
+
334
+ it ( 'should preserve prototype chain when updating class instances' , ( ) => {
335
+ const instances = [ new TestClass ( 1 , 'first' ) , new TestClass ( 2 , 'second' ) ] ;
336
+
337
+ const result = upsert ( instances , { id : 1 , value : 'updated' } , 'id' ) ;
338
+
339
+ expect ( result [ 0 ] ) . toBeInstanceOf ( TestClass ) ;
340
+ expect ( result [ 0 ] . getDescription ( ) ) . toBe ( '1: updated' ) ;
341
+
342
+ expect ( result [ 1 ] ) . toBeInstanceOf ( TestClass ) ;
343
+ expect ( result [ 1 ] . getDescription ( ) ) . toBe ( '2: second' ) ;
344
+ } ) ;
345
+
346
+ it ( 'should preserve prototype chain when inserting class instances' , ( ) => {
347
+ const instances = [ new TestClass ( 1 , 'first' ) , new TestClass ( 2 , 'second' ) ] ;
348
+
349
+ const result = upsert ( instances , new TestClass ( 3 , 'third' ) , 'id' ) ;
350
+
351
+ expect ( result [ 0 ] ) . toBeInstanceOf ( TestClass ) ;
352
+ expect ( result [ 1 ] ) . toBeInstanceOf ( TestClass ) ;
353
+
354
+ expect ( result [ 2 ] ) . toBeInstanceOf ( TestClass ) ;
355
+ expect ( result [ 2 ] . getDescription ( ) ) . toBe ( '3: third' ) ;
356
+ } ) ;
357
+
358
+ it ( 'should preserve prototype chain when upserting with comparison function' , ( ) => {
359
+ const instances = [ new TestClass ( 1 , 'first' ) , new TestClass ( 2 , 'second' ) ] ;
360
+
361
+ const result = upsert (
362
+ instances ,
363
+ new TestClass ( 1 , 'updated' ) ,
364
+ ( a , b ) => a . id === b . id ,
365
+ ) ;
366
+
367
+ expect ( result [ 0 ] ) . toBeInstanceOf ( TestClass ) ;
368
+ expect ( result [ 1 ] ) . toBeInstanceOf ( TestClass ) ;
369
+ expect ( result [ 0 ] . getDescription ( ) ) . toBe ( '1: updated' ) ;
370
+ expect ( result [ 1 ] . getDescription ( ) ) . toBe ( '2: second' ) ;
371
+ } ) ;
372
+ } ) ;
321
373
} ) ;
0 commit comments