11import { test , expect } from '@jest/globals' ;
2- import { stringifyIfNotString , jsonStringifyKeepMeta , wrapInObjectIfNotObject , jsonStringifyPossiblyCircular , removeCircularReferencesFromObject } from './../../helpers/helperFunctions' ;
2+ import { stringifyIfNotString , jsonStringifyKeepMeta , wrapInObjectIfNotObject , jsonReplacerWithPath , jsonRefReplacer , parseRefJSON , jsonStringifyPossiblyCircular , removeCircularReferencesFromObject } from './../../helpers/helperFunctions' ;
33import { makeRandomString } from './../../helpers/random' ;
44import { CONFIG } from '../../config' ;
55
@@ -67,6 +67,47 @@ test("wrapInObjectIfNotObject test", () => {
6767 expect ( test7 ) . toEqual ( { a : 1 , b : 2 , c : 3 } ) ;
6868} ) ;
6969
70+ test ( "jsonReplacerWithPath test" , ( ) => {
71+ const a = { a1 : 1 , a2 : 1 } ;
72+ const b = { b1 : 2 , b2 : [ 1 , a ] } ;
73+ const c = { c1 : 3 , c2 : b } ;
74+
75+ const test1 : string [ ] = [ ] ;
76+ const s = JSON . stringify ( c , jsonReplacerWithPath ( function ( this : any , field : string , value : any , path : string ) {
77+ test1 . push ( path ) ;
78+ return value ;
79+ } ) ) ;
80+
81+ expect ( test1 ) . toEqual ( [ "" , "c1" , "c2" , "c2.b1" , "c2.b2" , "c2.b2[0]" , "c2.b2[1]" , "c2.b2[1].a1" , "c2.b2[1].a2" ] )
82+ expect ( s ) . toEqual ( JSON . stringify ( c ) ) ;
83+ } ) ;
84+
85+ test ( "jsonRefReplacer test" , ( ) => {
86+ // Creating object with duplicate references
87+ const a : any = { a1 : 1 , a2 : 2 } ;
88+ const b : any = { b1 : 3 , b2 : "4" } ;
89+ const obj : any = { o1 : { o2 : a } , b, a } ; // duplicate reference
90+ a . a3 = [ 1 , 2 , b ] ; // circular reference
91+ b . b3 = a ; // circular reference
92+
93+ const test1 = JSON . stringify ( obj , jsonRefReplacer ( ) ) ;
94+ expect ( test1 ) . toEqual ( `{"o1":{"o2":{"a1":1,"a2":2,"a3":[1,2,{"b1":3,"b2":"4","b3":"#REF:$.o1.o2"}]}},"b":"#REF:$.o1.o2.a3[2]","a":"#REF:$.o1.o2"}` ) ;
95+ } ) ;
96+
97+ test ( "parseRefJSON test" , ( ) => {
98+ // Creating object with duplicate references
99+ const a : any = { a1 : 1 , a2 : 2 } ;
100+ const b : any = { b1 : 3 , b2 : "4" } ;
101+ const obj : any = { o1 : { o2 : a } , b, a } ; // duplicate reference
102+ a . a3 = [ 1 , 2 , b ] ; // circular reference
103+ b . b3 = a ; // circular reference
104+
105+ const s = JSON . stringify ( obj , jsonRefReplacer ( ) ) ;
106+
107+ const test1 = parseRefJSON ( s ) ;
108+ expect ( test1 ) . toEqual ( obj ) ;
109+ } ) ;
110+
70111test ( "jsonStringifyPossiblyCircular test" , ( ) => {
71112 const test1 = jsonStringifyPossiblyCircular ( [ ] ) ;
72113 const test2 = jsonStringifyPossiblyCircular ( { } ) ;
@@ -84,7 +125,7 @@ test("jsonStringifyPossiblyCircular test", () => {
84125 expect ( test3 ) . toEqual ( `{"a":1,"b":2,"c":3}` ) ;
85126 expect ( test4 ) . toEqual ( `{"a":{"x":1,"y":2},"b":{"x":0,"y":-1}}` ) ;
86127 expect ( test4Reversed ) . toEqual ( data4 ) ;
87- expect ( test5 ) . toEqual ( `{"data":"123"}` ) ;
128+ expect ( test5 ) . toEqual ( `{"data":"123","circ":"#REF:$" }` ) ;
88129} ) ;
89130
90131test ( "removeCircularReferencesFromObject test" , ( ) => {
@@ -110,12 +151,17 @@ test("removeCircularReferencesFromObject test", () => {
110151
111152 const test6 = removeCircularReferencesFromObject ( innerRecursiveLinksObject ) ;
112153
154+ // console.log("test6", test6);
155+
113156 expect ( test1 ) . toEqual ( a ) ;
114157 expect ( test2 ) . toEqual ( b ) ;
115158 expect ( test3 ) . toEqual ( c ) ;
116159 expect ( test4 ) . toEqual ( d ) ;
117- expect ( test5 ) . toEqual ( { data : "123" } ) ;
118- expect ( test6 ) . toEqual ( { aRefersToB : { x : 10 , linkB : { y : 100 } } } ) ;
160+ expect ( test5 ) . toEqual ( { data : "123" , circ : "#REF:$" } ) ;
161+ expect ( test6 ) . toEqual ( {
162+ aRefersToB : { x : 10 , linkB : { linkA : "#REF:$.aRefersToB" , y : 100 } } ,
163+ bRefersToA : "#REF:$.aRefersToB.linkB"
164+ } ) ;
119165} ) ;
120166
121167test ( "jsonStringifyKeepMeta test" , ( ) => {
@@ -153,5 +199,5 @@ test("jsonStringifyKeepMeta test", () => {
153199
154200 const test4 = jsonStringifyKeepMeta ( data3 , true ) ;
155201 expect ( test4 . ok ) . toBe ( true ) ;
156- expect ( test4 . result ) . toEqual ( `{"a":101,"b":"abc","c":true,"d":null,"f":[1,2,3],"g":{"x":10,"y":12},"circularObject":{"data":"123"}}` ) ;
202+ expect ( test4 . result ) . toEqual ( `{"a":101,"b":"abc","c":true,"d":null,"f":[1,2,3],"g":{"x":10,"y":12},"circularObject":{"data":"123","circ":"#REF:$.circularObject" }}` ) ;
157203} ) ;
0 commit comments