-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathis.test.js
More file actions
113 lines (96 loc) · 3.9 KB
/
is.test.js
File metadata and controls
113 lines (96 loc) · 3.9 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { isArray, isEqual, isObject, isPlainObject, isUndefined } from './index.js';
describe('helpers', () => {
describe('is', () => {
describe('isArray', () => {
it('should return true if the value is an array', () => {
expect(isArray([1, 2, 3])).toBe(true);
});
it('should return false if the value is not an array', () => {
expect(isArray('not an array')).toBe(false);
expect(isArray({ key: 'value' })).toBe(false);
expect(isArray(null)).toBe(false);
expect(isArray(undefined)).toBe(false);
});
});
describe('isObject', () => {
it('should return true if the value is an object', () => {
expect(isObject({ key: 'value' })).toBe(true);
});
it('should return false if the value is not an object', () => {
expect(isObject([])).toBe(false);
expect(isObject(null)).toBe(false);
expect(isObject('string')).toBe(false);
expect(isObject(123)).toBe(false);
expect(isObject(undefined)).toBe(false);
});
});
describe('isEqual', () => {
it('should return true if values are strictly equal', () => {
const obj1 = { key: 'value' };
const obj2 = obj1;
expect(isEqual(1, 1)).toBe(true);
expect(isEqual('string', 'string')).toBe(true);
expect(isEqual(true, true)).toBe(true);
expect(isEqual(obj1, obj2)).toBe(true);
});
it('should return false if values are not strictly equal', () => {
expect(isEqual(1, '1')).toBe(false);
expect(isEqual(0, -0)).toBe(false);
expect(isEqual(0, false)).toBe(false);
expect(isEqual(true, false)).toBe(false);
expect(isEqual({}, {})).toBe(false);
});
});
describe('isUndefined', () => {
it('should return true if the value is undefined', () => {
expect(isUndefined(undefined)).toBe(true);
});
it('should return false if the value is not undefined', () => {
expect(isUndefined(null)).toBe(false);
expect(isUndefined(0)).toBe(false);
expect(isUndefined('')).toBe(false);
});
});
describe('isPlainObject', () => {
it('should return true for plain objects', () => {
expect(isPlainObject({})).toBe(true);
expect(isPlainObject({ foo: 'bar' })).toBe(true);
expect(isPlainObject(Object.create(null))).toBe(true);
expect(isPlainObject(Object.create(Object.prototype))).toBe(true);
expect(isPlainObject(Object.assign({}, { a: 1 }))).toBe(true);
});
it('should return false for primitives', () => {
expect(isPlainObject(true)).toBe(false);
expect(isPlainObject(undefined)).toBe(false);
expect(isPlainObject(1)).toBe(false);
expect(isPlainObject('string')).toBe(false);
expect(isPlainObject(Symbol('s'))).toBe(false);
});
it('should return false for `null`', () => {
expect(isPlainObject(null)).toBe(false);
});
it('should return false for functions and instances', () => {
function Foo() {
this.abc = {};
}
expect(isPlainObject(Foo)).toBe(false);
expect(isPlainObject(new Foo())).toBe(false);
expect(isPlainObject(() => {})).toBe(false);
expect(isPlainObject(function () {})).toBe(false);
});
it('should return false for arrays', () => {
expect(isPlainObject([])).toBe(false);
expect(isPlainObject([1, 2, 3])).toBe(false);
});
it('should return false for built-in objects', () => {
expect(isPlainObject(new Date())).toBe(false);
expect(isPlainObject(new Map())).toBe(false);
expect(isPlainObject(new Set())).toBe(false);
expect(isPlainObject(/abc/)).toBe(false);
});
it('should return false for objects created with Object.create and a custom prototype', () => {
expect(isPlainObject(Object.create({}))).toBe(false);
});
});
});
});