Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions test/object-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { omit, pick } from "../app/utils/object";
import { deepClone, ensure } from "../app/utils/clone";

describe("omit", () => {
test("returns a copy without the given keys", () => {
const obj = { a: 1, b: 2, c: 3 };
expect(omit(obj, "b")).toEqual({ a: 1, c: 3 });
expect(omit(obj, "a", "c")).toEqual({ b: 2 });
});

test("does not mutate the original object", () => {
const obj = { a: 1, b: 2 };
omit(obj, "a");
expect(obj).toEqual({ a: 1, b: 2 });
});
});

describe("pick", () => {
test("keeps only the given keys", () => {
const obj = { a: 1, b: 2, c: 3 };
expect(pick(obj, "a", "c")).toEqual({ a: 1, c: 3 });
});

test("does not mutate the original object", () => {
const obj = { a: 1, b: 2 };
pick(obj, "a");
expect(obj).toEqual({ a: 1, b: 2 });
});
});

describe("deepClone", () => {
test("clones nested structures by value", () => {
const original = { a: 1, nested: { b: 2 }, list: [1, 2] };
const copy = deepClone(original);

expect(copy).toEqual(original);
expect(copy).not.toBe(original);

copy.nested.b = 99;
copy.list.push(3);
expect(original.nested.b).toBe(2);
expect(original.list).toEqual([1, 2]);
});
});

describe("ensure", () => {
test("returns true only when all keys have non-empty values", () => {
expect(ensure({ a: 1, b: "x" }, ["a", "b"])).toBe(true);
});

test("returns false when any key is undefined, null or empty", () => {
expect(ensure({ a: 1, b: "" }, ["a", "b"])).toBe(false);
expect(ensure({ a: 1, b: null } as any, ["a", "b"])).toBe(false);
expect(ensure({ a: 1 } as any, ["a", "b"])).toBe(false);
});
});
Loading