|
| 1 | +import { decycle, previewString } from '../util'; |
| 2 | + |
| 3 | +describe('Util', () => { |
| 4 | + describe('previewString', () => { |
| 5 | + it('should handle null values', () => { |
| 6 | + expect(previewString(null)).toEqual('null'); |
| 7 | + }); |
| 8 | + |
| 9 | + it('should handle undefined values', () => { |
| 10 | + expect(previewString(undefined)).toEqual('undefined'); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should handle string values', () => { |
| 14 | + expect(previewString('hello')).toEqual('"hello"'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should handle boolean values', () => { |
| 18 | + expect(previewString(true)).toEqual('true'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should handle number (integer) values', () => { |
| 22 | + expect(previewString(42)).toEqual('42'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should handle number (decimal) values', () => { |
| 26 | + expect(previewString(5.6)).toEqual('5.6'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should handle date objects', () => { |
| 30 | + const date = new Date(); |
| 31 | + expect(previewString(date)).toEqual(`"${date.toISOString()}"`); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should handle arrays', () => { |
| 35 | + expect(previewString([1, 2, 3])).toEqual('Array[3] [1,2,3]'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should handle regular objects', () => { |
| 39 | + const obj = { a: 1, b: 'hello' }; |
| 40 | + expect(previewString(obj)).toEqual('Object {"a":1,"b":"hello"}'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle function values', () => { |
| 44 | + expect(previewString(() => {})).toEqual('Function'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should truncate when limit is exceeded', () => { |
| 48 | + const obj = { a: 1, b: 'hello'.repeat(50) }; |
| 49 | + expect(previewString(obj, 20)).toEqual('Object {"a":1,"b":"h…'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should truncate string values in objects when stringsLimit is exceeded', () => { |
| 53 | + const obj = { a: 1, b: 'hello'.repeat(50) }; |
| 54 | + expect(previewString(obj, 200, 10)).toEqual( |
| 55 | + 'Object {"a":1,"b":"hellohello…"}' |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should truncate string values in arrays when stringsLimit is exceeded', () => { |
| 60 | + expect(previewString(['longstring'.repeat(10)], 200, 10)).toEqual( |
| 61 | + 'Array[1] ["longstring…"]' |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('parity with JSON.stringify()', () => { |
| 66 | + it('should handle null values', () => { |
| 67 | + expect(previewString(null)).toEqual(JSON.stringify(null)); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle undefined values', () => { |
| 71 | + expect(previewString(undefined)).toEqual( |
| 72 | + JSON.stringify(undefined) + '' |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should handle string values', () => { |
| 77 | + expect(previewString('hello')).toEqual(JSON.stringify('hello')); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should handle number (integer) values', () => { |
| 81 | + expect(previewString(42)).toEqual(JSON.stringify(42)); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should handle number (decimal) values', () => { |
| 85 | + expect(previewString(5.6)).toEqual(JSON.stringify(5.6)); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should handle boolean values', () => { |
| 89 | + expect(previewString(true)).toEqual(JSON.stringify(true)); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should handle date objects', () => { |
| 93 | + const date = new Date(); |
| 94 | + expect(previewString(date)).toEqual(JSON.stringify(date)); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should handle regular objects', () => { |
| 98 | + const obj = { a: 1, b: 'hello' }; |
| 99 | + expect(previewString(obj)).toEqual('Object ' + JSON.stringify(obj)); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should handle arrays', () => { |
| 103 | + const arr = [1, 2, 'hello']; |
| 104 | + expect(previewString(arr)).toEqual('Array[3] ' + JSON.stringify(arr)); |
| 105 | + }); |
| 106 | + |
| 107 | + // functions have intentional differences |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('decycle', () => { |
| 112 | + it('should replace circular references with $ref properties', () => { |
| 113 | + const obj = { a: 1 } as any; |
| 114 | + obj.b = obj; |
| 115 | + expect(decycle(obj)).toEqual({ a: 1, b: { $ref: '$' } }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should handle arrays with circular references', () => { |
| 119 | + const arr: any[] = [1]; |
| 120 | + arr[1] = arr; |
| 121 | + expect(decycle(arr)).toEqual([1, { $ref: '$' }]); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should handle nested objects with circular references', () => { |
| 125 | + const obj1 = { a: 1 } as any; |
| 126 | + const obj2 = { b: 2, c: obj1 } as any; |
| 127 | + obj1.d = obj2; |
| 128 | + expect(decycle(obj1)).toEqual({ a: 1, d: { b: 2, c: { $ref: '$' } } }); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments