|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + getConfigValueAtPath, |
| 4 | + setConfigValueAtPath, |
| 5 | + unsetConfigValueAtPath, |
| 6 | +} from "./config-paths.js"; |
| 7 | + |
| 8 | +describe("config path own-property traversal", () => { |
| 9 | + for (const key of ["toString", "valueOf", "hasOwnProperty"]) { |
| 10 | + it(`does not treat inherited ${key} as config`, () => { |
| 11 | + const parent: Record<string, unknown> = {}; |
| 12 | + const root: Record<string, unknown> = { parent }; |
| 13 | + |
| 14 | + expect(getConfigValueAtPath(root, ["parent", key])).toBeUndefined(); |
| 15 | + expect(unsetConfigValueAtPath(root, ["parent", key])).toBe(false); |
| 16 | + expect(root).toEqual({ parent: {} }); |
| 17 | + |
| 18 | + setConfigValueAtPath(root, ["parent", key], "own"); |
| 19 | + expect(Object.hasOwn(parent, key)).toBe(true); |
| 20 | + expect(getConfigValueAtPath(root, ["parent", key])).toBe("own"); |
| 21 | + expect(unsetConfigValueAtPath(root, ["parent", key])).toBe(true); |
| 22 | + expect(root).toEqual({}); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + it("replaces an inherited parent instead of traversing it", () => { |
| 27 | + const prototypeBranch = { leaf: "prototype" }; |
| 28 | + const root = Object.create({ branch: prototypeBranch }) as Record<string, unknown>; |
| 29 | + |
| 30 | + expect(getConfigValueAtPath(root, ["branch", "leaf"])).toBeUndefined(); |
| 31 | + expect(unsetConfigValueAtPath(root, ["branch", "leaf"])).toBe(false); |
| 32 | + expect(prototypeBranch).toEqual({ leaf: "prototype" }); |
| 33 | + |
| 34 | + setConfigValueAtPath(root, ["branch", "leaf"], "own"); |
| 35 | + expect(Object.hasOwn(root, "branch")).toBe(true); |
| 36 | + expect(root.branch).toEqual({ leaf: "own" }); |
| 37 | + expect(prototypeBranch).toEqual({ leaf: "prototype" }); |
| 38 | + |
| 39 | + expect(unsetConfigValueAtPath(root, ["branch", "leaf"])).toBe(true); |
| 40 | + expect(Object.hasOwn(root, "branch")).toBe(false); |
| 41 | + expect(getConfigValueAtPath(root, ["branch", "leaf"])).toBeUndefined(); |
| 42 | + expect(prototypeBranch).toEqual({ leaf: "prototype" }); |
| 43 | + }); |
| 44 | +}); |
0 commit comments