Skip to content

Commit b24a213

Browse files
committedJan 5, 2024
test: improve tests for isPlainObject
1 parent e922a16 commit b24a213

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed
 

‎test/defu.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("defu", () => {
5050
}>();
5151
});
5252

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

62-
it.skip("should assign date properly", () => {
62+
it("should assign date properly", () => {
6363
const date1 = new Date("2020-01-01");
6464
const date2 = new Date("2020-01-02");
6565
const result = defu({ date: date1 }, { date: date2 });
66-
expect(result).toEqual({ date: date2 });
66+
expect(result).toEqual({ date: date1 });
6767
});
6868

6969
it("should correctly merge different object types", () => {

‎test/utils.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { it, expect } from "vitest";
2+
import { isPlainObject } from "../src/_utils";
3+
4+
it("isPlainObject", () => {
5+
expect(isPlainObject(undefined)).toBe(false);
6+
expect(isPlainObject(0)).toBe(false);
7+
expect(isPlainObject(0n)).toBe(false);
8+
expect(isPlainObject("")).toBe(false);
9+
expect(isPlainObject(true)).toBe(false);
10+
expect(isPlainObject(Symbol(""))).toBe(false);
11+
expect(isPlainObject(() => {})).toBe(false);
12+
expect(isPlainObject(function namedFunc() {})).toBe(false);
13+
expect(isPlainObject(null)).toBe(false);
14+
expect(isPlainObject({})).toBe(true);
15+
expect(isPlainObject(Math)).toBe(false);
16+
expect(isPlainObject(new Set([]))).toBe(false);
17+
expect(isPlainObject(new ArrayBuffer(0))).toBe(false);
18+
expect(isPlainObject(Promise.resolve())).toBe(false);
19+
expect(isPlainObject(Object.create(null))).toBe(true);
20+
expect(isPlainObject(new Intl.Locale("en"))).toBe(false);
21+
// eslint-disable-next-line no-new-object
22+
expect(isPlainObject(new Object({ prop: true }))).toBe(true);
23+
expect(isPlainObject(new (class Class {})())).toBe(false);
24+
expect(isPlainObject([])).toBe(false);
25+
expect(isPlainObject(/regexp/)).toBe(false);
26+
expect(isPlainObject(new Error("test"))).toBe(false);
27+
expect(isPlainObject(new Date())).toBe(false);
28+
expect(
29+
isPlainObject(
30+
(function () {
31+
// eslint-disable-next-line prefer-rest-params
32+
return arguments;
33+
})(),
34+
),
35+
).toBe(false);
36+
// expect(isPlainObject(new Proxy({}, {}))).toBe(false); // TODO
37+
});

0 commit comments

Comments
 (0)
Please sign in to comment.