import { get, has, isPlainObject } from "../objects";

describe("objects", () => {
  it("can get stuff or return default", () => {
    const obj = {
      a: {
        b: {
          c: 42,
        },
      },
    };

    expect(get(obj, "a.b.c", "fortytwo")).toBe(42);
    expect(get(obj, "a.b.d", "fortytwo")).toBe("fortytwo");
    expect(get(obj, "non-existing")).toBeUndefined();
    expect(get(obj, "a.b")).toStrictEqual({ c: 42 });
    expect(get("string", "a.b")).toBeUndefined();
  });

  it("can check path on object", () => {
    const obj = {
      a: {
        b: {
          c: 42,
        },
      },
    };

    expect(has(obj, "a.b.c")).toBe(true);
    expect(has(obj, "a.b.d")).toBe(false);
    expect(has(obj, "non-existing")).toBe(false);
    expect(has(obj, "a.b")).toBe(true);
    expect(has("string", "a.b")).toBe(false);
    expect(has(null, "a")).toBe(false);
    expect(has(undefined, "a")).toBe(false);
  });

  it("can check for plain objects", () => {
    expect(isPlainObject({})).toBe(true);
    expect(isPlainObject({ param: "value" })).toBe(true);
    expect(isPlainObject("string")).toBe(false);
    expect(isPlainObject(["a", "b", "c"])).toBe(false);
    expect(isPlainObject(new Set([1, 2, 3]))).toBe(false);
    expect(isPlainObject(new Date())).toBe(false);
    expect(isPlainObject(new File([""], "filename"))).toBe(false);
    expect(isPlainObject(undefined)).toBe(false);
    expect(isPlainObject(null)).toBe(false);
    expect(isPlainObject(1)).toBe(false);
  });
});
