@@ -164,6 +164,74 @@ describe('getObjectSubset()', () => {
164164 } ,
165165 ) ;
166166 } ) ;
167+
168+ describe ( 'calculating subsets of objects with circular references' , ( ) => {
169+ test ( 'simple circular references' , ( ) => {
170+ const nonCircularObj = { a : 'world' , b : 'something' } ;
171+
172+ const circularObjA = { a : 'hello' } ;
173+ circularObjA . ref = circularObjA ;
174+
175+ const circularObjB = { a : 'world' } ;
176+ circularObjB . ref = circularObjB ;
177+
178+ const primitiveInsteadOfRef = { b : 'something' } ;
179+ primitiveInsteadOfRef . ref = 'not a ref' ;
180+
181+ const nonCircularRef = { b : 'something' } ;
182+ nonCircularRef . ref = { } ;
183+
184+ expect ( getObjectSubset ( circularObjA , nonCircularObj ) ) . toEqual ( {
185+ a : 'hello' ,
186+ } ) ;
187+ expect ( getObjectSubset ( nonCircularObj , circularObjA ) ) . toEqual ( {
188+ a : 'world' ,
189+ } ) ;
190+
191+ expect ( getObjectSubset ( circularObjB , circularObjA ) ) . toEqual ( circularObjB ) ;
192+
193+ expect ( getObjectSubset ( primitiveInsteadOfRef , circularObjA ) ) . toEqual ( {
194+ ref : 'not a ref' ,
195+ } ) ;
196+ expect ( getObjectSubset ( nonCircularRef , circularObjA ) ) . toEqual ( {
197+ ref : { } ,
198+ } ) ;
199+ } ) ;
200+
201+ test ( 'transitive circular references' , ( ) => {
202+ const nonCircularObj = { a : 'world' , b : 'something' } ;
203+
204+ const transitiveCircularObjA = { a : 'hello' } ;
205+ transitiveCircularObjA . nestedObj = { parentObj : transitiveCircularObjA } ;
206+
207+ const transitiveCircularObjB = { a : 'world' } ;
208+ transitiveCircularObjB . nestedObj = { parentObj : transitiveCircularObjB } ;
209+
210+ const primitiveInsteadOfRef = { } ;
211+ primitiveInsteadOfRef . nestedObj = { otherProp : 'not the parent ref' } ;
212+
213+ const nonCircularRef = { } ;
214+ nonCircularRef . nestedObj = { otherProp : { } } ;
215+
216+ expect ( getObjectSubset ( transitiveCircularObjA , nonCircularObj ) ) . toEqual ( {
217+ a : 'hello' ,
218+ } ) ;
219+ expect ( getObjectSubset ( nonCircularObj , transitiveCircularObjA ) ) . toEqual ( {
220+ a : 'world' ,
221+ } ) ;
222+
223+ expect (
224+ getObjectSubset ( transitiveCircularObjB , transitiveCircularObjA ) ,
225+ ) . toEqual ( transitiveCircularObjB ) ;
226+
227+ expect (
228+ getObjectSubset ( primitiveInsteadOfRef , transitiveCircularObjA ) ,
229+ ) . toEqual ( { nestedObj : { otherProp : 'not the parent ref' } } ) ;
230+ expect ( getObjectSubset ( nonCircularRef , transitiveCircularObjA ) ) . toEqual ( {
231+ nestedObj : { otherProp : { } } ,
232+ } ) ;
233+ } ) ;
234+ } ) ;
167235} ) ;
168236
169237describe ( 'emptyObject()' , ( ) => {
0 commit comments