|
| 1 | +import { sortObjectKeys } from './pattern-key'; |
| 2 | + |
| 3 | +describe('sortObjectKeys', () => { |
| 4 | + it('should sort top-level keys alphabetically', () => { |
| 5 | + const result = sortObjectKeys({ b: 2, a: 1, c: 3 }); |
| 6 | + expect(Object.keys(result)).toEqual(['a', 'b', 'c']); |
| 7 | + expect(result).toEqual({ a: 1, b: 2, c: 3 }); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should return empty object for empty input', () => { |
| 11 | + expect(sortObjectKeys({})).toEqual({}); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should sort nested object keys recursively', () => { |
| 15 | + const result = sortObjectKeys({ z: { y: 2, x: 1 }, a: 0 }); |
| 16 | + expect(Object.keys(result)).toEqual(['a', 'z']); |
| 17 | + expect(Object.keys(result.z as object)).toEqual(['x', 'y']); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should handle arrays without modifying order', () => { |
| 21 | + const result = sortObjectKeys({ items: [3, 1, 2] }); |
| 22 | + expect(result.items).toEqual([3, 1, 2]); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should sort keys of objects nested inside arrays', () => { |
| 26 | + const result = sortObjectKeys({ |
| 27 | + items: [ |
| 28 | + { b: 1, a: 2 }, |
| 29 | + { d: 3, c: 4 }, |
| 30 | + ], |
| 31 | + }); |
| 32 | + const items = result.items as Record<string, unknown>[]; |
| 33 | + expect(Object.keys(items[0])).toEqual(['a', 'b']); |
| 34 | + expect(Object.keys(items[1])).toEqual(['c', 'd']); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should handle nested arrays', () => { |
| 38 | + const result = sortObjectKeys({ matrix: [[{ z: 1, a: 2 }]] }); |
| 39 | + const inner = (result.matrix as unknown[][])[0][0] as Record<string, unknown>; |
| 40 | + expect(Object.keys(inner)).toEqual(['a', 'z']); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should pass through primitives as-is', () => { |
| 44 | + const result = sortObjectKeys({ str: 'hello', num: 42, bool: true, nil: null }); |
| 45 | + expect(result).toEqual({ bool: true, nil: null, num: 42, str: 'hello' }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should throw on circular references', () => { |
| 49 | + const obj: Record<string, unknown> = { a: 1 }; |
| 50 | + obj.self = obj; |
| 51 | + expect(() => sortObjectKeys(obj)).toThrow('Circular reference detected'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should throw on deeply nested circular references', () => { |
| 55 | + const inner: Record<string, unknown> = { value: 1 }; |
| 56 | + const outer: Record<string, unknown> = { nested: inner }; |
| 57 | + inner.parent = outer; |
| 58 | + expect(() => sortObjectKeys(outer)).toThrow('Circular reference detected'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should allow the same object in multiple non-circular branches', () => { |
| 62 | + const shared = { x: 1, y: 2 }; |
| 63 | + const result = sortObjectKeys({ a: shared, b: shared }); |
| 64 | + expect(Object.keys(result.a as object)).toEqual(['x', 'y']); |
| 65 | + expect(Object.keys(result.b as object)).toEqual(['x', 'y']); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should handle undefined and function values like JSON.stringify', () => { |
| 69 | + const result = sortObjectKeys({ a: 1, b: undefined, c: () => {} }); |
| 70 | + expect(result).toEqual({ a: 1, b: undefined, c: expect.any(Function) }); |
| 71 | + }); |
| 72 | +}); |
0 commit comments