|
| 1 | +import { describe, expect, it, vi } 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 | + |
| 45 | + for (const inheritedKind of ["setter", "non-writable"] as const) { |
| 46 | + it(`creates an own parent over an inherited ${inheritedKind} property`, () => { |
| 47 | + const setter = vi.fn(); |
| 48 | + const prototype = {}; |
| 49 | + Object.defineProperty( |
| 50 | + prototype, |
| 51 | + "branch", |
| 52 | + inheritedKind === "setter" |
| 53 | + ? { configurable: true, set: setter } |
| 54 | + : { configurable: true, value: { leaf: "prototype" }, writable: false }, |
| 55 | + ); |
| 56 | + const root = Object.create(prototype) as Record<string, unknown>; |
| 57 | + |
| 58 | + expect(() => setConfigValueAtPath(root, ["branch", "leaf"], "own")).not.toThrow(); |
| 59 | + expect(setter).not.toHaveBeenCalled(); |
| 60 | + expect(Object.getOwnPropertyDescriptor(root, "branch")).toMatchObject({ |
| 61 | + configurable: true, |
| 62 | + enumerable: true, |
| 63 | + value: { leaf: "own" }, |
| 64 | + writable: true, |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it(`creates an own leaf over an inherited ${inheritedKind} property`, () => { |
| 69 | + const setter = vi.fn(); |
| 70 | + const prototype = {}; |
| 71 | + Object.defineProperty( |
| 72 | + prototype, |
| 73 | + "leaf", |
| 74 | + inheritedKind === "setter" |
| 75 | + ? { configurable: true, set: setter } |
| 76 | + : { configurable: true, value: "prototype", writable: false }, |
| 77 | + ); |
| 78 | + const parent = Object.create(prototype) as Record<string, unknown>; |
| 79 | + const root = { parent }; |
| 80 | + |
| 81 | + expect(() => setConfigValueAtPath(root, ["parent", "leaf"], "own")).not.toThrow(); |
| 82 | + expect(setter).not.toHaveBeenCalled(); |
| 83 | + expect(Object.getOwnPropertyDescriptor(parent, "leaf")).toMatchObject({ |
| 84 | + configurable: true, |
| 85 | + enumerable: true, |
| 86 | + value: "own", |
| 87 | + writable: true, |
| 88 | + }); |
| 89 | + }); |
| 90 | + } |
| 91 | +}); |
0 commit comments