@@ -70,9 +70,144 @@ it('uses the blacklist to avoid copying properties on the first level', () => {
7070 } ,
7171 } ;
7272
73- expect ( deepCyclicCopy ( obj , new Set ( [ 'blacklisted' ] ) ) ) . toEqual ( {
73+ expect ( deepCyclicCopy ( obj , { blacklist : new Set ( [ 'blacklisted' ] ) } ) ) . toEqual ( {
7474 subObj : {
7575 blacklisted : 42 ,
7676 } ,
7777 } ) ;
7878} ) ;
79+
80+ it ( 'does not keep the prototype by default when top level is object' , ( ) => {
81+ const sourceObject = new function ( ) { } ( ) ;
82+ sourceObject . nestedObject = new function ( ) { } ( ) ;
83+ sourceObject . nestedArray = new function ( ) {
84+ this . length = 0 ;
85+ } ( ) ;
86+
87+ const spy = jest . spyOn ( Array , 'isArray' ) . mockImplementation ( object => {
88+ return object === sourceObject . nestedArray ;
89+ } ) ;
90+
91+ const copy = deepCyclicCopy ( sourceObject , { keepPrototype : false } ) ;
92+
93+ expect ( Object . getPrototypeOf ( copy ) ) . not . toBe (
94+ Object . getPrototypeOf ( sourceObject ) ,
95+ ) ;
96+ expect ( Object . getPrototypeOf ( copy . nestedObject ) ) . not . toBe (
97+ Object . getPrototypeOf ( sourceObject . nestedObject ) ,
98+ ) ;
99+ expect ( Object . getPrototypeOf ( copy . nestedArray ) ) . not . toBe (
100+ Object . getPrototypeOf ( sourceObject . nestedArray ) ,
101+ ) ;
102+
103+ expect ( Object . getPrototypeOf ( copy ) ) . toBe ( Object . getPrototypeOf ( { } ) ) ;
104+ expect ( Object . getPrototypeOf ( copy . nestedObject ) ) . toBe (
105+ Object . getPrototypeOf ( { } ) ,
106+ ) ;
107+ expect ( Object . getPrototypeOf ( copy . nestedArray ) ) . toBe (
108+ Object . getPrototypeOf ( [ ] ) ,
109+ ) ;
110+
111+ spy . mockRestore ( ) ;
112+ } ) ;
113+
114+ it ( 'does not keep the prototype by default when top level is array' , ( ) => {
115+ const spy = jest . spyOn ( Array , 'isArray' ) . mockImplementation ( ( ) => true ) ;
116+
117+ const sourceArray = new function ( ) {
118+ this . length = 0 ;
119+ } ( ) ;
120+
121+ const copy = deepCyclicCopy ( sourceArray ) ;
122+ expect ( Object . getPrototypeOf ( copy ) ) . not . toBe (
123+ Object . getPrototypeOf ( sourceArray ) ,
124+ ) ;
125+
126+ expect ( Object . getPrototypeOf ( copy ) ) . toBe ( Object . getPrototypeOf ( [ ] ) ) ;
127+ spy . mockRestore ( ) ;
128+ } ) ;
129+
130+ it ( 'does not keep the prototype of arrays when keepPrototype = false' , ( ) => {
131+ const spy = jest . spyOn ( Array , 'isArray' ) . mockImplementation ( ( ) => true ) ;
132+
133+ const sourceArray = new function ( ) {
134+ this . length = 0 ;
135+ } ( ) ;
136+
137+ const copy = deepCyclicCopy ( sourceArray ) ;
138+ expect ( Object . getPrototypeOf ( copy ) ) . not . toBe (
139+ Object . getPrototypeOf ( sourceArray ) ,
140+ ) ;
141+
142+ expect ( Object . getPrototypeOf ( copy ) ) . toBe ( Object . getPrototypeOf ( [ ] ) ) ;
143+ spy . mockRestore ( ) ;
144+ } ) ;
145+
146+ it ( 'keeps the prototype of arrays when keepPrototype = true' , ( ) => {
147+ const spy = jest . spyOn ( Array , 'isArray' ) . mockImplementation ( ( ) => true ) ;
148+
149+ const sourceArray = new function ( ) {
150+ this . length = 0 ;
151+ } ( ) ;
152+
153+ const copy = deepCyclicCopy ( sourceArray , { keepPrototype : true } ) ;
154+ expect ( Object . getPrototypeOf ( copy ) ) . toBe ( Object . getPrototypeOf ( sourceArray ) ) ;
155+
156+ spy . mockRestore ( ) ;
157+ } ) ;
158+
159+ it ( 'does not keep the prototype for objects when keepPrototype = false' , ( ) => {
160+ const sourceobject = new function ( ) { } ( ) ;
161+ sourceobject . nestedObject = new function ( ) { } ( ) ;
162+ sourceobject . nestedArray = new function ( ) {
163+ this . length = 0 ;
164+ } ( ) ;
165+
166+ const spy = jest . spyOn ( Array , 'isArray' ) . mockImplementation ( object => {
167+ return object === sourceobject . nestedArray ;
168+ } ) ;
169+
170+ const copy = deepCyclicCopy ( sourceobject , { keepPrototype : false } ) ;
171+
172+ expect ( Object . getPrototypeOf ( copy ) ) . not . toBe (
173+ Object . getPrototypeOf ( sourceobject ) ,
174+ ) ;
175+ expect ( Object . getPrototypeOf ( copy . nestedObject ) ) . not . toBe (
176+ Object . getPrototypeOf ( sourceobject . nestedObject ) ,
177+ ) ;
178+ expect ( Object . getPrototypeOf ( copy . nestedArray ) ) . not . toBe (
179+ Object . getPrototypeOf ( sourceobject . nestedArray ) ,
180+ ) ;
181+ expect ( Object . getPrototypeOf ( copy ) ) . toBe ( Object . getPrototypeOf ( { } ) ) ;
182+ expect ( Object . getPrototypeOf ( copy . nestedObject ) ) . toBe (
183+ Object . getPrototypeOf ( { } ) ,
184+ ) ;
185+ expect ( Object . getPrototypeOf ( copy . nestedArray ) ) . toBe (
186+ Object . getPrototypeOf ( [ ] ) ,
187+ ) ;
188+
189+ spy . mockRestore ( ) ;
190+ } ) ;
191+
192+ it ( 'keeps the prototype for objects when keepPrototype = true' , ( ) => {
193+ const sourceObject = new function ( ) { } ( ) ;
194+ sourceObject . nestedObject = new function ( ) { } ( ) ;
195+ sourceObject . nestedArray = new function ( ) {
196+ this . length = 0 ;
197+ } ( ) ;
198+
199+ const spy = jest . spyOn ( Array , 'isArray' ) . mockImplementation ( object => {
200+ return object === sourceObject . nestedArray ;
201+ } ) ;
202+
203+ const copy = deepCyclicCopy ( sourceObject , { keepPrototype : true } ) ;
204+
205+ expect ( Object . getPrototypeOf ( copy ) ) . toBe ( Object . getPrototypeOf ( sourceObject ) ) ;
206+ expect ( Object . getPrototypeOf ( copy . nestedObject ) ) . toBe (
207+ Object . getPrototypeOf ( sourceObject . nestedObject ) ,
208+ ) ;
209+ expect ( Object . getPrototypeOf ( copy . nestedArray ) ) . toBe (
210+ Object . getPrototypeOf ( sourceObject . nestedArray ) ,
211+ ) ;
212+ spy . mockRestore ( ) ;
213+ } ) ;
0 commit comments