File tree 2 files changed +25
-2
lines changed
2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -65,6 +65,29 @@ describe("cloning behavior", () => {
65
65
} ) ;
66
66
} ) ;
67
67
68
+ describe ( "cloning method \"shallow\" save prototype" , function ( ) {
69
+ it ( "works" , ( ) => {
70
+ function Item ( name , owner , maker ) {
71
+ this . name = name ;
72
+ this . owner = owner ;
73
+ this . maker = maker ;
74
+ }
75
+
76
+ const cdb = new loki ( "clonetest" ) ;
77
+ const citems = cdb . addCollection ( "items" , { clone : true , cloneMethod : "shallow" } ) ;
78
+ const oldObject = new Item ( "mjolnir" , "thor" , "dwarves" ) ;
79
+ const insObject = citems . insert ( oldObject ) ;
80
+
81
+ // cant' have either of these polluting our collection
82
+ oldObject . name = "mewmew" ;
83
+ insObject . name = "mewmew" ;
84
+
85
+ const result = citems . findOne ( { "owner" : "thor" } ) ;
86
+ expect ( result instanceof Item ) . toBe ( true ) ;
87
+ expect ( result . name ) . toBe ( "mjolnir" ) ;
88
+ } ) ;
89
+ } ) ;
90
+
68
91
describe ( "collection find() cloning works" , ( ) => {
69
92
it ( "works" , ( ) => {
70
93
const cdb = new loki ( "cloningEnabled" ) ;
Original file line number Diff line number Diff line change @@ -16,14 +16,14 @@ export function clone(data, method) {
16
16
break ;
17
17
case "shallow" :
18
18
// more compatible method for older browsers
19
- cloned = data . prototype ? Object . create ( data . prototype ) : { } ;
19
+ cloned = Object . create ( data . constructor . prototype ) ;
20
20
Object . keys ( data ) . map ( ( i ) => {
21
21
cloned [ i ] = data [ i ] ;
22
22
} ) ;
23
23
break ;
24
24
case "shallow-assign" :
25
25
// should be supported by newer environments/browsers
26
- cloned = data . prototype ? Object . create ( data . prototype ) : { } ;
26
+ cloned = Object . create ( data . constructor . prototype ) ;
27
27
Object . assign ( cloned , data ) ;
28
28
break ;
29
29
default :
You can’t perform that action at this time.
0 commit comments