|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { setByPath } from './setByPath'; |
| 4 | + |
| 5 | +describe('setByPath', () => { |
| 6 | + it('should set a simple nested value', () => { |
| 7 | + const obj = {}; |
| 8 | + setByPath(obj, ['a', 'b', 'c'], 'value'); |
| 9 | + expect(obj).toEqual({ a: { b: { c: 'value' } } }); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should preserve numeric string keys as object keys, not array indices', () => { |
| 13 | + const obj = {}; |
| 14 | + setByPath(obj, ['nodeDefs', '0', 'name'], 'First Node'); |
| 15 | + setByPath(obj, ['nodeDefs', '1', 'name'], 'Second Node'); |
| 16 | + setByPath(obj, ['nodeDefs', '2', 'name'], 'Third Node'); |
| 17 | + |
| 18 | + expect(obj).toEqual({ |
| 19 | + nodeDefs: { |
| 20 | + '0': { name: 'First Node' }, |
| 21 | + '1': { name: 'Second Node' }, |
| 22 | + '2': { name: 'Third Node' }, |
| 23 | + }, |
| 24 | + }); |
| 25 | + |
| 26 | + // Verify it's an object, not an array |
| 27 | + expect(Array.isArray((obj as any).nodeDefs)).toBe(false); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should handle numeric keys at the root level', () => { |
| 31 | + const obj = {}; |
| 32 | + setByPath(obj, ['0'], 'value0'); |
| 33 | + setByPath(obj, ['1'], 'value1'); |
| 34 | + |
| 35 | + expect(obj).toEqual({ |
| 36 | + '0': 'value0', |
| 37 | + '1': 'value1', |
| 38 | + }); |
| 39 | + |
| 40 | + expect(Array.isArray(obj)).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle mixed numeric and string keys', () => { |
| 44 | + const obj = {}; |
| 45 | + setByPath(obj, ['items', '0', 'id'], 'first'); |
| 46 | + setByPath(obj, ['items', 'abc', 'id'], 'second'); |
| 47 | + setByPath(obj, ['items', '1', 'id'], 'third'); |
| 48 | + |
| 49 | + expect(obj).toEqual({ |
| 50 | + items: { |
| 51 | + '0': { id: 'first' }, |
| 52 | + '1': { id: 'third' }, |
| 53 | + 'abc': { id: 'second' }, |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + expect(Array.isArray((obj as any).items)).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should overwrite existing values', () => { |
| 61 | + const obj = { a: { b: 'old' } }; |
| 62 | + setByPath(obj, ['a', 'b'], 'new'); |
| 63 | + expect(obj).toEqual({ a: { b: 'new' } }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should handle single-key paths', () => { |
| 67 | + const obj = {}; |
| 68 | + setByPath(obj, ['key'], 'value'); |
| 69 | + expect(obj).toEqual({ key: 'value' }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should handle empty path gracefully', () => { |
| 73 | + const obj = { existing: 'data' }; |
| 74 | + setByPath(obj, [], 'value'); |
| 75 | + // Should not modify the object |
| 76 | + expect(obj).toEqual({ existing: 'data' }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should create intermediate objects when path does not exist', () => { |
| 80 | + const obj = {}; |
| 81 | + setByPath(obj, ['a', 'b', 'c', 'd'], 'value'); |
| 82 | + expect(obj).toEqual({ |
| 83 | + a: { |
| 84 | + b: { |
| 85 | + c: { |
| 86 | + d: 'value', |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should replace non-object intermediate values with objects', () => { |
| 94 | + const obj: any = { a: 'string' }; |
| 95 | + setByPath(obj, ['a', 'b'], 'value'); |
| 96 | + expect(obj).toEqual({ |
| 97 | + a: { |
| 98 | + b: 'value', |
| 99 | + }, |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should handle complex nested structures with numeric keys', () => { |
| 104 | + const obj = {}; |
| 105 | + setByPath(obj, ['nodeDefs', '0', 'inputs', '0', 'name'], 'input1'); |
| 106 | + setByPath(obj, ['nodeDefs', '0', 'inputs', '1', 'name'], 'input2'); |
| 107 | + setByPath(obj, ['nodeDefs', '1', 'outputs', '0', 'name'], 'output1'); |
| 108 | + |
| 109 | + expect(obj).toEqual({ |
| 110 | + nodeDefs: { |
| 111 | + '0': { |
| 112 | + inputs: { |
| 113 | + '0': { name: 'input1' }, |
| 114 | + '1': { name: 'input2' }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + '1': { |
| 118 | + outputs: { |
| 119 | + '0': { name: 'output1' }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }); |
| 124 | + |
| 125 | + // Verify all levels are objects, not arrays |
| 126 | + const typed = obj as any; |
| 127 | + expect(Array.isArray(typed.nodeDefs)).toBe(false); |
| 128 | + expect(Array.isArray(typed.nodeDefs['0'].inputs)).toBe(false); |
| 129 | + expect(Array.isArray(typed.nodeDefs['1'].outputs)).toBe(false); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should handle number type in path (not just numeric strings)', () => { |
| 133 | + const obj = {}; |
| 134 | + setByPath(obj, ['items', 0, 'name'], 'First'); |
| 135 | + setByPath(obj, ['items', 1, 'name'], 'Second'); |
| 136 | + |
| 137 | + expect(obj).toEqual({ |
| 138 | + items: { |
| 139 | + '0': { name: 'First' }, |
| 140 | + '1': { name: 'Second' }, |
| 141 | + }, |
| 142 | + }); |
| 143 | + |
| 144 | + expect(Array.isArray((obj as any).items)).toBe(false); |
| 145 | + }); |
| 146 | +}); |
0 commit comments