|
6 | 6 | humanizeDate, |
7 | 7 | relativeDate, |
8 | 8 | getContentType, |
9 | | - formatSize |
| 9 | + formatSize, |
| 10 | + prettifyJsonString |
10 | 11 | } from './index'; |
11 | 12 |
|
12 | 13 | describe('common utils', () => { |
@@ -191,4 +192,98 @@ describe('common utils', () => { |
191 | 192 | expect(formatSize(NaN)).toBe('0B'); |
192 | 193 | }); |
193 | 194 | }); |
| 195 | + |
| 196 | + describe('prettifyJsonString', () => { |
| 197 | + test('should return non-string inputs unchanged', () => { |
| 198 | + expect(prettifyJsonString(null)).toBe(null); |
| 199 | + expect(prettifyJsonString(undefined)).toBe(undefined); |
| 200 | + expect(prettifyJsonString(123)).toBe(123); |
| 201 | + expect(prettifyJsonString([])).toEqual([]); |
| 202 | + expect(prettifyJsonString({})).toEqual({}); |
| 203 | + expect(prettifyJsonString(true)).toBe(true); |
| 204 | + }); |
| 205 | + |
| 206 | + test('should format valid JSON without Bruno variables', () => { |
| 207 | + const input = '{"name":"John","age":30}'; |
| 208 | + const expected = `{\n "name": "John",\n "age": 30\n}`; |
| 209 | + console.log(prettifyJsonString(input)); |
| 210 | + expect(prettifyJsonString(input)).toBe(expected); |
| 211 | + }); |
| 212 | + |
| 213 | + test('should format valid JSON with Bruno variables', () => { |
| 214 | + const input = '{"name": {{userName}}}'; |
| 215 | + const expected = `{\n "name": {{userName}}\n}`; |
| 216 | + console.log(prettifyJsonString(input)); |
| 217 | + expect(prettifyJsonString(input)).toBe(expected); |
| 218 | + }); |
| 219 | + |
| 220 | + test('should format complex json string', () => { |
| 221 | + const input = `{"bruno-variables": {{brunovariable}},"id": 123456789123456789123456789,"name": "Test 'JSON' Data with "quotes" — Pretty Print ","active": true,"price": 199.9999999,"decimals": 1.00,"nullValue": null,"unicodeText": "こんにちは世界 ","escapedCharacters": "Line1\nLine2\tTabbed\"Quoted\" and 'single quoted' with 'code' style","nestedObject": { "level1": { "level2": { "emptyArray": [], "specialChars": "@#$%^&*()_+-=[]{}|;':,./<>?~", "booleanValues": [ true, false, true ], "numbers": [ 0, -1, 1.23e10, 3.1415926535 ] } }},"mixedArray": [ "string with 'apostrophe'", 42, false, null, { "innerObj": { "keyWithQuotes": "value containing 'single quotes'", "nestedArray": [ { "a": "O'Reilly" }{ "b": "'inline code'" }, [ "deep", "array", { "c": "contains 'quotes'" } ] ] } }],"nonStringVariable": {{nonStringVar}},"withBrunoVariable": "{{string}} '{{with}}' "{{variety}}" of '{{variables}}'","dateExample": "2025-11-07T12:34:56Z","regexExample": "^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$","urls": { "website": "https://example.com?param='value'&flag='true'", "escapedURL": "https:\/\/escaped-url.com\/path\?q='search'\&debug='on'"},"multiLineString": "This is a long text\nthat spans multiple\nlines with 'quotes' and 'code' snippets "}`; |
| 222 | + const expectedOutput = `{ |
| 223 | + "bruno-variables": {{brunovariable}}, |
| 224 | + "id": 123456789123456789123456789, |
| 225 | + "name": "Test 'JSON' Data with "quotes" — Pretty Print ", |
| 226 | + "active": true, |
| 227 | + "price": 199.9999999, |
| 228 | + "decimals": 1.00, |
| 229 | + "nullValue": null, |
| 230 | + "unicodeText": "こんにちは世界 ", |
| 231 | + "escapedCharacters": "Line1\nLine2\tTabbed\"Quoted\" and 'single quoted' with 'code' style", |
| 232 | + "nestedObject": { |
| 233 | + "level1": { |
| 234 | + "level2": { |
| 235 | + "emptyArray": [], |
| 236 | + "specialChars": "@#$%^&*()_+-=[]{}|;':,./<>?~", |
| 237 | + "booleanValues": [ |
| 238 | + true, |
| 239 | + false, |
| 240 | + true |
| 241 | + ], |
| 242 | + "numbers": [ |
| 243 | + 0, |
| 244 | + -1, |
| 245 | + 1.23e10, |
| 246 | + 3.1415926535 |
| 247 | + ] |
| 248 | + } |
| 249 | + } |
| 250 | + }, |
| 251 | + "mixedArray": [ |
| 252 | + "string with 'apostrophe'", |
| 253 | + 42, |
| 254 | + false, |
| 255 | + null, |
| 256 | + { |
| 257 | + "innerObj": { |
| 258 | + "keyWithQuotes": "value containing 'single quotes'", |
| 259 | + "nestedArray": [ |
| 260 | + { |
| 261 | + "a": "O'Reilly" |
| 262 | + }{ |
| 263 | + "b": "'inline code'" |
| 264 | + }, |
| 265 | + [ |
| 266 | + "deep", |
| 267 | + "array", |
| 268 | + { |
| 269 | + "c": "contains 'quotes'" |
| 270 | + } |
| 271 | + ] |
| 272 | + ] |
| 273 | + } |
| 274 | + } |
| 275 | + ], |
| 276 | + "nonStringVariable": {{nonStringVar}}, |
| 277 | + "withBrunoVariable": "{{string}} '{{with}}' "{{variety}}" of '{{variables}}'", |
| 278 | + "dateExample": "2025-11-07T12:34:56Z", |
| 279 | + "regexExample": "^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$", |
| 280 | + "urls": { |
| 281 | + "website": "https://example.com?param='value'&flag='true'", |
| 282 | + "escapedURL": "https:\/\/escaped-url.com\/path\?q='search'\&debug='on'" |
| 283 | + }, |
| 284 | + "multiLineString": "This is a long text\nthat spans multiple\nlines with 'quotes' and 'code' snippets " |
| 285 | +}`; |
| 286 | + expect(prettifyJsonString(input)).toBe(expectedOutput); |
| 287 | + }); |
| 288 | + }); |
194 | 289 | }); |
0 commit comments