-
-
Notifications
You must be signed in to change notification settings - Fork 475
/
IsPlainObjectTest.js
80 lines (66 loc) · 2.58 KB
/
IsPlainObjectTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const test = require("ava");
const isPlainObject = require("../src/Util/IsPlainObject");
test("isPlainObject", (t) => {
t.is(isPlainObject(null), false);
t.is(isPlainObject(undefined), false);
t.is(isPlainObject(1), false);
t.is(isPlainObject(true), false);
t.is(isPlainObject(false), false);
t.is(isPlainObject("string"), false);
t.is(isPlainObject([]), false);
t.is(isPlainObject(Symbol("a")), false);
t.is(
isPlainObject(function () {}),
false
);
});
// https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/test/test.js#L11447
// Notably, did not include the test for DOM Elements.
test("Test from lodash.isPlainObject", (t) => {
t.is(isPlainObject({}), true);
t.is(isPlainObject({ a: 1 }), true);
function Foo(a) {
this.a = 1;
}
t.is(isPlainObject({ constructor: Foo }), true);
t.is(isPlainObject([1, 2, 3]), false);
t.is(isPlainObject(new Foo(1)), false);
});
test("Test from lodash.isPlainObject: should return `true` for objects with a `[[Prototype]]` of `null`", (t) => {
let obj = Object.create(null);
t.is(isPlainObject(obj), true);
obj.constructor = Object.prototype.constructor;
t.is(isPlainObject(obj), true);
});
test("Test from lodash.isPlainObject: should return `true` for objects with a `valueOf` property", (t) => {
t.is(isPlainObject({ valueOf: 0 }), true);
});
test("Test from lodash.isPlainObject: should return `true` for objects with a writable `Symbol.toStringTag` property", (t) => {
let obj = {};
obj[Symbol.toStringTag] = "X";
t.is(isPlainObject(obj), true);
});
test("Test from lodash.isPlainObject: should return `false` for objects with a custom `[[Prototype]]`", (t) => {
let obj = Object.create({ a: 1 });
t.is(isPlainObject(obj), false);
});
test("Test from lodash.isPlainObject (modified): should return `false` for non-Object objects", (t) => {
t.is(isPlainObject(arguments), true); // WARNING: lodash was false
t.is(isPlainObject(Error), false);
t.is(isPlainObject(Math), true); // WARNING: lodash was false
});
test("Test from lodash.isPlainObject: should return `false` for non-objects", (t) => {
t.is(isPlainObject(true), false);
t.is(isPlainObject("a"), false);
t.is(isPlainObject(Symbol("a")), false);
});
test("Test from lodash.isPlainObject (modified): should return `true` for objects with a read-only `Symbol.toStringTag` property", (t) => {
var object = {};
Object.defineProperty(object, Symbol.toStringTag, {
configurable: true,
enumerable: false,
writable: false,
value: "X",
});
t.is(isPlainObject(object), true); // WARNING: lodash was false
});