|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { immutableDelete } from "../../src/common/utils/dotProp.js"; |
| 3 | + |
| 4 | +describe("immutableDelete", () => { |
| 5 | + it("returns a new object reference", () => { |
| 6 | + const obj = { a: 1 }; |
| 7 | + const result = immutableDelete(obj, "a"); |
| 8 | + expect(result).not.toBe(obj); |
| 9 | + }); |
| 10 | + |
| 11 | + it("removes a top-level property", () => { |
| 12 | + expect(immutableDelete({ a: 1, b: 2 }, "a")).toEqual({ b: 2 }); |
| 13 | + }); |
| 14 | + |
| 15 | + it("removes a nested property by dot path", () => { |
| 16 | + expect(immutableDelete({ a: { b: 1, c: 2 } }, "a.b")).toEqual({ a: { c: 2 } }); |
| 17 | + }); |
| 18 | + |
| 19 | + it("does not mutate the original object", () => { |
| 20 | + const obj = { a: 1, b: 2 }; |
| 21 | + immutableDelete(obj, "a"); |
| 22 | + expect(obj).toEqual({ a: 1, b: 2 }); |
| 23 | + }); |
| 24 | + |
| 25 | + it("is a no-op when the path does not exist", () => { |
| 26 | + const obj = { a: 1 }; |
| 27 | + expect(immutableDelete(obj, "b")).toEqual({ a: 1 }); |
| 28 | + }); |
| 29 | + |
| 30 | + it("splices an element from a cloned array by numeric index", () => { |
| 31 | + const arr = ["a", "b", "c"]; |
| 32 | + const result = immutableDelete(arr, 1); |
| 33 | + expect(result).toEqual(["a", "c"]); |
| 34 | + }); |
| 35 | + |
| 36 | + it("splices the first element from a cloned array", () => { |
| 37 | + const arr = [10, 20, 30]; |
| 38 | + expect(immutableDelete(arr, 0)).toEqual([20, 30]); |
| 39 | + }); |
| 40 | + |
| 41 | + it("splices the last element from a cloned array", () => { |
| 42 | + const arr = [10, 20, 30]; |
| 43 | + expect(immutableDelete(arr, 2)).toEqual([10, 20]); |
| 44 | + }); |
| 45 | + |
| 46 | + it("does not mutate the original array", () => { |
| 47 | + const arr = ["a", "b", "c"]; |
| 48 | + immutableDelete(arr, 1); |
| 49 | + expect(arr).toEqual(["a", "b", "c"]); |
| 50 | + }); |
| 51 | + |
| 52 | + it("returns a new array reference", () => { |
| 53 | + const arr = [1, 2, 3]; |
| 54 | + const result = immutableDelete(arr, 0); |
| 55 | + expect(result).not.toBe(arr); |
| 56 | + }); |
| 57 | + |
| 58 | + it("returns a clone when the index is out of bounds", () => { |
| 59 | + const arr = ["x"]; |
| 60 | + const result = immutableDelete(arr, 5); |
| 61 | + expect(result).toEqual(["x"]); |
| 62 | + expect(result).not.toBe(arr); |
| 63 | + }); |
| 64 | + |
| 65 | + it("returns a clone for a negative index", () => { |
| 66 | + const arr = [1, 2, 3]; |
| 67 | + const result = immutableDelete(arr, -1); |
| 68 | + expect(result).toEqual([1, 2, 3]); |
| 69 | + expect(result).not.toBe(arr); |
| 70 | + }); |
| 71 | +}); |
0 commit comments