honkit
Version:
HonKit is building beautiful books using Markdown.
98 lines (97 loc) • 4.88 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Same scenarios as `safeObjectPath` contract tests on the refactored branch, run here against
* the real `object-path` dependency (pre-#508). Expectations match object-path@0.11.8, not
* `safeObjectPath` (see PR review discussion).
*/
const object_path_1 = __importDefault(require("object-path"));
describe("object-path baseline (pre safeObjectPath)", () => {
describe("get", () => {
test("returns nested value by dot path", () => {
const obj = { structure: { readme: "README.md" } };
expect(object_path_1.default.get(obj, "structure.readme")).toBe("README.md");
});
test("accepts array path", () => {
const obj = { structure: { readme: "README.md" } };
expect(object_path_1.default.get(obj, ["structure", "readme"])).toBe("README.md");
});
test("returns default when path missing", () => {
const obj = { a: 1 };
expect(object_path_1.default.get(obj, "b", "fallback")).toBe("fallback");
expect(object_path_1.default.get(obj, "a.c", "fallback")).toBe("fallback");
});
test("returns default for null / undefined root", () => {
expect(object_path_1.default.get(null, "a", "d")).toBe("d");
expect(object_path_1.default.get(undefined, "a", "d")).toBe("d");
});
test("empty path returns the object (object-path semantics)", () => {
expect(object_path_1.default.get({ a: 1 }, "", "d")).toEqual({ a: 1 });
});
test("returns null when key exists with null value", () => {
expect(object_path_1.default.get({ x: null }, "x", "d")).toBeNull();
});
test("returns default when traversing into null intermediate", () => {
expect(object_path_1.default.get({ x: null }, "x.y", "d")).toBe("d");
});
test("returns default when traversing into non-object", () => {
expect(object_path_1.default.get({ x: "str" }, "x.y", "d")).toBe("d");
});
test("undefined leaf with default returns default", () => {
expect(object_path_1.default.get({ a: {} }, "a.missing", "d")).toBe("d");
});
test("prototype-ish keys with default", () => {
const obj = { safe: 1 };
expect(object_path_1.default.get(obj, "__proto__.polluted", "d")).toBe("d");
expect(object_path_1.default.get(obj, "constructor.prototype.polluted", "d")).toBe("d");
expect(obj.polluted).toBeUndefined();
});
test("does not trim segment whitespace (object-path)", () => {
expect(object_path_1.default.get({ a: { b: 2 } }, " a . b ", "d")).toBe("d");
});
});
describe("set", () => {
test("sets nested value by dot path", () => {
const obj = {};
object_path_1.default.set(obj, "structure.readme", "INTRO.md");
expect(obj).toEqual({ structure: { readme: "INTRO.md" } });
});
test("accepts array path", () => {
const obj = {};
object_path_1.default.set(obj, ["structure", "readme"], "INTRO.md");
expect(obj).toEqual({ structure: { readme: "INTRO.md" } });
});
test("overwrites existing nested value", () => {
const obj = { structure: { readme: "A.md" } };
object_path_1.default.set(obj, "structure.readme", "B.md");
expect(obj.structure.readme).toBe("B.md");
});
test("throws when intermediate is a non-object (object-path)", () => {
const obj = { a: "was-string" };
expect(() => object_path_1.default.set(obj, "a.b", 1)).toThrow();
});
test("throws when intermediate is null (object-path / native JS)", () => {
const obj = { a: null };
expect(() => object_path_1.default.set(obj, "a.b", 1)).toThrow();
expect(obj.a).toBeNull();
});
test("uses array index segments like object-path", () => {
const obj = {};
object_path_1.default.set(obj, "items.0.name", "first");
expect(obj.items).toEqual([{ name: "first" }]);
});
test("set on __proto__ path is a no-op for own keys (object-path)", () => {
const obj = { ok: true };
expect(object_path_1.default.set(obj, "__proto__", "ignored")).toBeUndefined();
expect(obj).toEqual({ ok: true });
});
test("set returns undefined (object-path)", () => {
const obj = {};
expect(object_path_1.default.set(obj, "x", 42)).toBeUndefined();
expect(obj.x).toBe(42);
});
});
});