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