|
| 1 | +import { describe, it } from "vitest"; |
| 2 | +import { mergeOptions } from "../src/utils"; |
| 3 | + |
| 4 | +describe.concurrent("mergeOptions", () => { |
| 5 | + it("should return defaultOptions if options is undefined", ({ expect }) => { |
| 6 | + const defaults = { a: 1, b: 2 }; |
| 7 | + const result = mergeOptions(undefined, defaults); |
| 8 | + expect(result).toEqual(defaults); |
| 9 | + }); |
| 10 | + |
| 11 | + it("should return options if defaultOptions is undefined", ({ expect }) => { |
| 12 | + const opts = { a: 3, b: 4 }; |
| 13 | + const result = mergeOptions(opts, undefined); |
| 14 | + expect(result).toEqual(opts); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should deeply merge nested objects", ({ expect }) => { |
| 18 | + const defaults = { a: 1, b: { c: 2, d: 3 } }; |
| 19 | + const opts = { b: { c: 5 } }; |
| 20 | + const result = mergeOptions(opts, defaults); |
| 21 | + expect(result).toEqual({ a: 1, b: { c: 5, d: 3 } }); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should override primitive values", ({ expect }) => { |
| 25 | + const defaults = { a: 1, b: 2 }; |
| 26 | + const opts = { b: 5 }; |
| 27 | + const result = mergeOptions(opts, defaults); |
| 28 | + expect(result).toEqual({ a: 1, b: 5 }); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should not merge arrays, but override them", ({ expect }) => { |
| 32 | + const defaults = { arr: [1, 2, 3], b: 2 }; |
| 33 | + const opts = { arr: [4, 5] }; |
| 34 | + const result = mergeOptions(opts, defaults); |
| 35 | + expect(result).toEqual({ arr: [4, 5], b: 2 }); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should handle null and undefined values in options", ({ expect }) => { |
| 39 | + const defaults = { a: 1, b: 2 }; |
| 40 | + const opts = { a: null, b: undefined }; |
| 41 | + const result = mergeOptions(opts, defaults as any); |
| 42 | + expect(result).toEqual({ a: null, b: undefined }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should handle both options and defaultOptions as undefined", ({ expect }) => { |
| 46 | + const result = mergeOptions(undefined, undefined); |
| 47 | + expect(result).toEqual({}); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should not mutate the input objects", ({ expect }) => { |
| 51 | + const defaults = { a: 1, b: { c: 2 } }; |
| 52 | + const opts = { b: { d: 3 } }; |
| 53 | + const defaultsCopy = JSON.parse(JSON.stringify(defaults)); |
| 54 | + const optsCopy = JSON.parse(JSON.stringify(opts)); |
| 55 | + mergeOptions(opts, defaults as any); |
| 56 | + expect(defaults).toEqual(defaultsCopy); |
| 57 | + expect(opts).toEqual(optsCopy); |
| 58 | + }); |
| 59 | +}); |
0 commit comments