import { omit } from "./omit"; describe("omit", () => { it("returns empty object when source is not given", () => { const value = omit(undefined, ["name", "count"]); expect(value).toEqual({}); // @ts-expect-error invalid value on purpose, it's handled, but not exposed const value2 = omit(null, ["name", "count"]); expect(value2).toEqual({}); }); it("returns new object when source is function", () => { // eslint-disable-next-line @typescript-eslint/no-empty-function const source = () => {}; source.title = "ABCD"; source.weight = "XXX"; const value = omit(source, ["title"]); expect(value).toEqual({ weight: "XXX", }); }); it("returns new object without given properties", () => { const source = { a: 5, b: "string", c: undefined, d: 0, e: null, f: false, }; const value = omit(source, ["a", "d", "g"]); expect(value).toEqual({ b: "string", c: undefined, e: null, f: false, }); expect(value).not.toBe(source); }); it("returns inherited properties (as own)", () => { class Test { name = "xxx"; } const testInstance = new Test(); // @ts-expect-error invalid value on purpose testInstance.length = 100; const value = omit(testInstance, ["title"]); // value.must.eql({ // name: "xxx", // length: 100, // }); expect(value).toEqual({ name: "xxx", length: 100, }); // eslint-disable-next-line no-prototype-builtins expect(Object.hasOwnProperty("name")).toBe(true); // eslint-disable-next-line no-prototype-builtins expect(Object.hasOwnProperty("length")).toBe(true); }); it("skips non-enumerable properties", () => { const source = { aaa: 123, title: 666, }; Object.defineProperty(source, "name", { enumerable: false, value: "test", }); const value = omit(source, ["title"]); expect(value).toEqual({ aaa: 123, }); expect("name" in value).toBe(false); }); it("returns object when array is given as source", () => { const source = ["a", "b", "c"]; // @ts-expect-error Invalid values on purpose const value = omit(source, [0, "2", 15, "xxx"]); expect(value).toEqual({ 1: "b", }); }); });