| 1 | // File generated by Continue |
| 2 | |
| 3 | import mergeJson from "./merge"; |
| 4 | |
| 5 | describe("mergeJson", () => { |
| 6 | it("should merge two simple JSON objects", () => { |
| 7 | const first = { a: 1, b: 2 }; |
| 8 | const second = { b: 3, c: 4 }; |
| 9 | const result = mergeJson(first, second); |
| 10 | expect(result).toEqual({ a: 1, b: 3, c: 4 }); |
| 11 | }); |
| 12 | |
| 13 | it('should overwrite values when mergeBehavior is "overwrite"', () => { |
| 14 | const first = { a: 1, b: 2 }; |
| 15 | const second = { b: 3, c: 4 }; |
| 16 | const result = mergeJson(first, second, "overwrite"); |
| 17 | expect(result).toEqual({ a: 1, b: 3, c: 4 }); |
| 18 | }); |
| 19 | |
| 20 | it("should merge nested objects", () => { |
| 21 | const first = { a: { b: 1 } }; |
| 22 | const second = { a: { c: 2 } }; |
| 23 | const result = mergeJson(first, second); |
| 24 | expect(result).toEqual({ a: { b: 1, c: 2 } }); |
| 25 | }); |
| 26 | |
| 27 | it("should merge arrays without mergeKeys", () => { |
| 28 | const first = { a: [1, 2] }; |
| 29 | const second = { a: [3, 4] }; |
| 30 | const result = mergeJson(first, second); |
| 31 | expect(result).toEqual({ a: [1, 2, 3, 4] }); |
| 32 | }); |
| 33 | |
| 34 | it("should merge arrays with mergeKeys", () => { |
| 35 | const first = { |
| 36 | a: [ |
| 37 | { id: 1, value: "first" }, |
| 38 | { id: 2, value: "second" }, |
| 39 | ], |
| 40 | }; |
| 41 | const second = { |
| 42 | a: [ |
| 43 | { id: 2, value: "updated" }, |
| 44 | { id: 3, value: "third" }, |
| 45 | ], |
| 46 | }; |
| 47 | const mergeKeys = { |
| 48 | a: (item1: any, item2: any) => item1.id === item2.id, |
| 49 | }; |
| 50 | const result = mergeJson(first, second, undefined, mergeKeys); |
| 51 | expect(result).toEqual({ |
| 52 | a: [ |
| 53 | { id: 1, value: "first" }, |
| 54 | { id: 2, value: "updated" }, |
| 55 | { id: 3, value: "third" }, |
| 56 | ], |
| 57 | }); |
| 58 | }); |
| 59 | |
| 60 | it("should handle non-object values correctly", () => { |
| 61 | const first = { a: 1, b: "string", c: true }; |
| 62 | const second = { a: 2, b: "new string", c: false }; |
| 63 | const result = mergeJson(first, second); |
| 64 | expect(result).toEqual({ a: 2, b: "new string", c: false }); |
| 65 | }); |
| 66 | |
| 67 | it("should handle null and undefined values correctly", () => { |
| 68 | const first = { a: 1, b: null }; |
| 69 | const second = { b: 2, c: undefined }; |
| 70 | const result = mergeJson(first, second); |
| 71 | expect(result).toEqual({ a: 1, b: 2, c: undefined }); |
| 72 | }); |
| 73 | |
| 74 | it("should handle empty objects", () => { |
| 75 | const first = {}; |
| 76 | const second = { a: 1 }; |
| 77 | const result = mergeJson(first, second); |
| 78 | expect(result).toEqual({ a: 1 }); |
| 79 | }); |
| 80 | |
| 81 | it("should handle empty second object", () => { |
| 82 | const first = { a: 1 }; |
| 83 | const second = {}; |
| 84 | const result = mergeJson(first, second); |
| 85 | expect(result).toEqual({ a: 1 }); |
| 86 | }); |
| 87 | |
| 88 | it("should handle errors gracefully", () => { |
| 89 | const first = { a: 1 }; |
| 90 | const second = { b: () => {} }; // Functions are not serializable in JSON |
| 91 | const result = mergeJson(first, second); |
| 92 | expect(result).toEqual({ a: 1, b: expect.any(Function) }); |
| 93 | }); |
| 94 | |
| 95 | it("should merge deeply nested objects", () => { |
| 96 | const first = { a: { b: { c: 1 } } }; |
| 97 | const second = { a: { b: { d: 2 } } }; |
| 98 | const result = mergeJson(first, second); |
| 99 | expect(result).toEqual({ a: { b: { c: 1, d: 2 } } }); |
| 100 | }); |
| 101 | }); |