@@ -27,5 +27,104 @@ describe('json models', () => {
2727 expect ( ( ) => formatJson ( { rawJson, sortKeys : false , repairJson : false } ) ) . to . throw ( ) ;
2828 } ) ;
2929 } ) ;
30+
31+ const testJson = '{"b": 2, "a": 1}' ;
32+ const expectedSorted = '{\n "a": 1,\n "b": 2\n}' ;
33+ const expectedUnsorted = '{\n "b": 2,\n "a": 1\n}' ;
34+
35+ it ( 'formats JSON with default options (sorted keys, 3 spaces)' , ( ) => {
36+ const result = formatJson ( { rawJson : testJson } ) ;
37+ expect ( result ) . toBe ( expectedSorted ) ;
38+ } ) ;
39+
40+ it ( 'formats JSON without sorting keys when sortKeys is false' , ( ) => {
41+ const result = formatJson ( { rawJson : testJson , sortKeys : false } ) ;
42+ expect ( result ) . toBe ( expectedUnsorted ) ;
43+ } ) ;
44+
45+ it ( 'formats JSON with custom indent size' , ( ) => {
46+ const result = formatJson ( { rawJson : testJson , indentSize : 2 } ) ;
47+ const expected = '{\n "a": 1,\n "b": 2\n}' ;
48+ expect ( result ) . toBe ( expected ) ;
49+ } ) ;
50+
51+ it ( 'works with reactive refs' , ( ) => {
52+ const rawJsonRef = ref ( testJson ) ;
53+ const sortKeysRef = ref ( true ) ;
54+ const indentSizeRef = ref ( 3 ) ;
55+
56+ const result = formatJson ( {
57+ rawJson : rawJsonRef ,
58+ sortKeys : sortKeysRef ,
59+ indentSize : indentSizeRef ,
60+ } ) ;
61+ expect ( result ) . toBe ( expectedSorted ) ;
62+ } ) ;
63+
64+ describe ( 'autoUnescape functionality' , ( ) => {
65+ it ( 'unescapes escaped JSON strings when autoUnescape is true' , ( ) => {
66+ const escapedJson = '"{\\\"id\\\":\\\"123\\\",\\\"name\\\":\\\"test\\\"}"' ;
67+ const result = formatJson ( { rawJson : escapedJson , unescapeJsonString : true , indentSize : 2 } ) ;
68+ const expected = '{\n "id": "123",\n "name": "test"\n}' ;
69+ expect ( result ) . toBe ( expected ) ;
70+ } ) ;
71+
72+ it ( 'handles escaped JSON without outer quotes' , ( ) => {
73+ const escapedJson = '{\\\"id\\\":\\\"123\\\",\\\"name\\\":\\\"test\\\"}' ;
74+ const result = formatJson ( { rawJson : escapedJson , unescapeJsonString : true , indentSize : 2 } ) ;
75+ const expected = '{\n "id": "123",\n "name": "test"\n}' ;
76+ expect ( result ) . toBe ( expected ) ;
77+ } ) ;
78+
79+ it ( 'unescapes various escape sequences' , ( ) => {
80+ const escapedJson = '{\\\"text\\\":\\\"Hello\\\\\\\\World\\\",\\\"path\\\":\\\"/api\\\\/test\\\"}' ;
81+ const result = formatJson ( { rawJson : escapedJson , unescapeJsonString : true , indentSize : 2 } ) ;
82+ const expected = '{\n "path": "/api/test",\n "text": "Hello\\\\World"\n}' ;
83+ expect ( result ) . toBe ( expected ) ;
84+ } ) ;
85+
86+ it ( 'handles single-quoted outer strings' , ( ) => {
87+ const escapedJson = '\'{\\\"id\\\":\\\"123\\\"}\'' ;
88+ const result = formatJson ( { rawJson : escapedJson , unescapeJsonString : true , indentSize : 2 } ) ;
89+ const expected = '{\n "id": "123"\n}' ;
90+ expect ( result ) . toBe ( expected ) ;
91+ } ) ;
92+
93+ it ( 'processes regular JSON normally when autoUnescape is false' , ( ) => {
94+ const normalJson = '{"id":"123","name":"test"}' ;
95+ const result = formatJson ( { rawJson : normalJson , unescapeJsonString : false , indentSize : 2 } ) ;
96+ const expected = '{\n "id": "123",\n "name": "test"\n}' ;
97+ expect ( result ) . toBe ( expected ) ;
98+ } ) ;
99+
100+ it ( 'handles malformed escaped JSON gracefully' , ( ) => {
101+ const malformedJson = '"{\\\"incomplete' ;
102+ // Should fall back to original string and fail parsing
103+ expect ( ( ) => formatJson ( { rawJson : malformedJson , unescapeJsonString : true } ) ) . toThrow ( ) ;
104+ } ) ;
105+
106+ it ( 'works with complex nested objects' , ( ) => {
107+ const complexEscaped = '"{\\\"users\\\":[{\\\"id\\\":\\\"1\\\",\\\"data\\\":{\\\"active\\\":true}}],\\\"meta\\\":{\\\"total\\\":1}}"' ;
108+ const result = formatJson ( { rawJson : complexEscaped , unescapeJsonString : true , indentSize : 2 } ) ;
109+ const expected = '{\n "meta": {\n "total": 1\n },\n "users": [\n {\n "data": {\n "active": true\n },\n "id": "1"\n }\n ]\n}' ;
110+ expect ( result ) . toBe ( expected ) ;
111+ } ) ;
112+
113+ it ( 'works with reactive autoUnescape ref' , ( ) => {
114+ const escapedJson = '"{\\\"test\\\":\\\"value\\\"}"' ;
115+ const autoUnescapeRef = ref ( true ) ;
116+ const result = formatJson ( { rawJson : escapedJson , unescapeJsonString : autoUnescapeRef , indentSize : 2 } ) ;
117+ const expected = '{\n "test": "value"\n}' ;
118+ expect ( result ) . toBe ( expected ) ;
119+ } ) ;
120+ } ) ;
121+
122+ it ( 'handles empty string input' , ( ) => {
123+ expect ( ( ) => formatJson ( { rawJson : '' } ) ) . toThrow ( ) ;
124+ } ) ;
125+
126+ it ( 'handles invalid JSON input' , ( ) => {
127+ expect ( ( ) => formatJson ( { rawJson : 'invalid json' } ) ) . toThrow ( ) ;
128+ } ) ;
30129 } ) ;
31130} ) ;
0 commit comments