|
| 1 | +import { describe, expectTypeOf, it } from "vitest"; |
| 2 | +import type { EmptyObject } from "../src/index.js"; |
| 3 | +import jsonMergePatch from "../src/index.js"; |
| 4 | + |
| 5 | +describe("jsonMergePatch type", () => { |
| 6 | + it("should replace an attribute", () => { |
| 7 | + const patched = jsonMergePatch({ a: "b" }, { a: "c" }); |
| 8 | + expectTypeOf(patched).toEqualTypeOf<{ a: string }>(); |
| 9 | + }); |
| 10 | + |
| 11 | + it("should add an attribute", () => { |
| 12 | + const patched = jsonMergePatch({ a: "b" }, { b: "c" }); |
| 13 | + expectTypeOf(patched).toEqualTypeOf<{ a: string; b: string }>(); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should delete attribute", () => { |
| 17 | + let patched = jsonMergePatch({ a: "b" }, { a: null }); |
| 18 | + expectTypeOf(patched).toEqualTypeOf<EmptyObject>(); |
| 19 | + // @ts-expect-error |
| 20 | + patched = 5; |
| 21 | + }); |
| 22 | + |
| 23 | + it("should delete attribute without affecting others", () => { |
| 24 | + const patched = jsonMergePatch({ a: "b", b: "c" }, { a: null }); |
| 25 | + expectTypeOf(patched).toEqualTypeOf<{ b: string }>(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should replace array with a string", () => { |
| 29 | + const patched = jsonMergePatch({ a: ["b"] }, { a: "c" }); |
| 30 | + expectTypeOf(patched).toEqualTypeOf<{ a: string }>(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should replace a string with an array", () => { |
| 34 | + const patched = jsonMergePatch({ a: "c" }, { a: ["b"] }); |
| 35 | + expectTypeOf(patched).toEqualTypeOf<{ a: string[] }>(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should apply recursively", () => { |
| 39 | + const patched = jsonMergePatch({ a: { b: "c" } }, { a: { b: "d", c: null } }); |
| 40 | + expectTypeOf(patched).toEqualTypeOf<{ a: { b: string } }>(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should replace an object array with a number array", () => { |
| 44 | + const patched = jsonMergePatch({ a: [{ b: "c" }] }, { a: [1] as const }); |
| 45 | + expectTypeOf(patched).toEqualTypeOf<{ a: readonly [1] }>(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should replace an array", () => { |
| 49 | + const patched = jsonMergePatch(["a", "b"] as const, ["c", "d"] as const); |
| 50 | + expectTypeOf(patched).toEqualTypeOf<readonly ["c", "d"]>(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should replace an object with an array", () => { |
| 54 | + const patched = jsonMergePatch({ a: "b" }, ["c"] as const); |
| 55 | + expectTypeOf(patched).toEqualTypeOf<readonly ["c"]>(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should replace an object with null", () => { |
| 59 | + const patched = jsonMergePatch({ a: "foo" }, null); |
| 60 | + expectTypeOf(patched).toEqualTypeOf<null>(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should replace an object with a string", () => { |
| 64 | + const patched = jsonMergePatch({ a: "foo" }, "bar"); |
| 65 | + expectTypeOf(patched).toEqualTypeOf<string>(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should not change null attributes", () => { |
| 69 | + const patched = jsonMergePatch({ e: null }, { a: 1 }); |
| 70 | + expectTypeOf(patched).toEqualTypeOf<{ e: null; a: number }>(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should not set an attribute to null", () => { |
| 74 | + const patched = jsonMergePatch([1, 2], { a: "b", c: null }); |
| 75 | + expectTypeOf(patched).toEqualTypeOf<{ a: string }>(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should not set an attribute to null in a sub object", () => { |
| 79 | + const patched = jsonMergePatch({}, { a: { bb: { ccc: null } } }); |
| 80 | + expectTypeOf(patched).toEqualTypeOf<{ a: { bb: EmptyObject } }>(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments