Skip to content

Commit

Permalink
test: improve tests for isPlainObject
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 5, 2024
1 parent e922a16 commit b24a213
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
6 changes: 3 additions & 3 deletions test/defu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("defu", () => {
}>();
});

it.skip("should avoid merging objects with custom constructor", () => {
it("should avoid merging objects with custom constructor", () => {
class Test {
// eslint-disable-next-line no-useless-constructor
constructor(public value: string) {}
Expand All @@ -59,11 +59,11 @@ describe("defu", () => {
expect(result).toEqual({ test: new Test("a") });
});

it.skip("should assign date properly", () => {
it("should assign date properly", () => {
const date1 = new Date("2020-01-01");
const date2 = new Date("2020-01-02");
const result = defu({ date: date1 }, { date: date2 });
expect(result).toEqual({ date: date2 });
expect(result).toEqual({ date: date1 });
});

it("should correctly merge different object types", () => {
Expand Down
37 changes: 37 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { it, expect } from "vitest";
import { isPlainObject } from "../src/_utils";

it("isPlainObject", () => {
expect(isPlainObject(undefined)).toBe(false);
expect(isPlainObject(0)).toBe(false);
expect(isPlainObject(0n)).toBe(false);
expect(isPlainObject("")).toBe(false);
expect(isPlainObject(true)).toBe(false);
expect(isPlainObject(Symbol(""))).toBe(false);
expect(isPlainObject(() => {})).toBe(false);
expect(isPlainObject(function namedFunc() {})).toBe(false);
expect(isPlainObject(null)).toBe(false);
expect(isPlainObject({})).toBe(true);
expect(isPlainObject(Math)).toBe(false);
expect(isPlainObject(new Set([]))).toBe(false);
expect(isPlainObject(new ArrayBuffer(0))).toBe(false);
expect(isPlainObject(Promise.resolve())).toBe(false);
expect(isPlainObject(Object.create(null))).toBe(true);
expect(isPlainObject(new Intl.Locale("en"))).toBe(false);
// eslint-disable-next-line no-new-object
expect(isPlainObject(new Object({ prop: true }))).toBe(true);
expect(isPlainObject(new (class Class {})())).toBe(false);
expect(isPlainObject([])).toBe(false);
expect(isPlainObject(/regexp/)).toBe(false);
expect(isPlainObject(new Error("test"))).toBe(false);
expect(isPlainObject(new Date())).toBe(false);
expect(
isPlainObject(
(function () {
// eslint-disable-next-line prefer-rest-params
return arguments;
})(),
),
).toBe(false);
// expect(isPlainObject(new Proxy({}, {}))).toBe(false); // TODO
});

0 comments on commit b24a213

Please sign in to comment.