/
index.test.js
178 lines (172 loc) · 5.79 KB
/
index.test.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {
isObject,
isPlainObject,
isAnyObject,
isUndefined,
isNull,
isNullOrUndefined,
isFunction,
isArray,
isString,
isEmptyString,
isFullString,
isBoolean,
isRegExp,
isNumber,
isDate,
isSymbol,
isPrimitive,
isType,
isBlob,
isFile,
} from '../dist/index.cjs'
test('Basic true tests', () => {
expect(isUndefined(undefined)).toBe(true)
expect(isNull(null)).toBe(true)
expect(isNullOrUndefined(null)).toBe(true)
expect(isNullOrUndefined(undefined)).toBe(true)
expect(isObject({})).toBe(true)
expect(isObject(new Object())).toBe(true)
expect(isFunction(_ => {})).toBe(true)
expect(isArray([])).toBe(true)
expect(isArray(new Array())).toBe(true)
expect(isString('')).toBe(true)
expect(isString('_')).toBe(true)
expect(isEmptyString('')).toBe(true)
expect(isFullString(' ')).toBe(true)
expect(isBoolean(true)).toBe(true)
expect(isBoolean(false)).toBe(true)
expect(isRegExp(/./)).toBe(true)
expect(isRegExp(/./gi)).toBe(true)
expect(isNumber(0)).toBe(true)
expect(isNumber(1)).toBe(true)
expect(isDate(new Date())).toBe(true)
expect(isSymbol(Symbol())).toBe(true)
expect(isBlob(new Blob())).toBe(true)
expect(isFile(new File([''], '', { type: 'text/html' }))).toBe(true)
})
test('Basic false tests', () => {
expect(isNumber(NaN)).toBe(false)
expect(isDate(new Date('_'))).toBe(false)
expect(isDate(NaN)).toBe(false)
expect(isUndefined(NaN)).toBe(false)
expect(isNull(NaN)).toBe(false)
expect(isObject(NaN)).toBe(false)
expect(isFunction(NaN)).toBe(false)
expect(isArray(NaN)).toBe(false)
expect(isString(NaN)).toBe(false)
expect(isEmptyString(' ')).toBe(false)
expect(isFullString('')).toBe(false)
expect(isBoolean(NaN)).toBe(false)
expect(isRegExp(NaN)).toBe(false)
expect(isSymbol(NaN)).toBe(false)
expect(isNullOrUndefined(NaN)).toBe(false)
})
test('Primitive tests', () => {
// true
expect(isPrimitive(0)).toBe(true)
expect(isPrimitive('')).toBe(true)
expect(isPrimitive('str')).toBe(true)
expect(isPrimitive(Symbol())).toBe(true)
expect(isPrimitive(true)).toBe(true)
expect(isPrimitive(false)).toBe(true)
expect(isPrimitive(null)).toBe(true)
expect(isPrimitive(undefined)).toBe(true)
// false
expect(isPrimitive(NaN)).toBe(false)
expect(isPrimitive([])).toBe(false)
expect(isPrimitive(new Array())).toBe(false)
expect(isPrimitive({})).toBe(false)
expect(isPrimitive(new Object())).toBe(false)
expect(isPrimitive(new Date())).toBe(false)
expect(isPrimitive(_ => {})).toBe(false)
})
test('Date exception', () => {
expect(isDate(new Date('_'))).toBe(false)
})
test('Generic isType', () => {
function MyClass () {}
// This is correct old fashion syntax for classes, if this is missing
MyClass.prototype.constructor = MyClass
class MyOtherClass {}
const myClass = new MyClass()
// this is expected behaviour
expect(isType('', String)).toBe(true)
expect(isType('_', String)).toBe(true)
expect(isType('Hello World', String)).toBe(true)
expect(isType(NaN, Number)).toBe(true)
expect(isType(0, Number)).toBe(true)
expect(isType(1, Number)).toBe(true)
expect(isType({}, Object)).toBe(true)
expect(isType(new Object(), Object)).toBe(true)
expect(isType([], Array)).toBe(true)
expect(isType(new Array(), Array)).toBe(true)
expect(isType(_ => {}, Function)).toBe(true)
expect(isType(true, Boolean)).toBe(true)
expect(isType(false, Boolean)).toBe(true)
expect(isType(new Date('_'), Date)).toBe(true)
expect(isType(new Date(), Date)).toBe(true)
expect(isType(/./, RegExp)).toBe(true)
expect(isType(/./gi, RegExp)).toBe(true)
expect(isType(myClass, MyClass)).toBe(true)
expect(isType(new MyOtherClass(), MyOtherClass)).toBe(true)
expect(isType(myClass, MyOtherClass)).toBe(false)
expect(isType(Symbol(), Symbol)).toBe(true)
// expect(isType(null, Null)).toBe(true)
// expect(isType(undefined, Undefined)).toBe(true)
// It SHOULD fail
expect(isType(5, String)).toBe(false)
expect(isType(null, Object)).toBe(false)
// Not sure if this would be the expected behaviour but everything is an object
// so I would say so
expect(isType(myClass, Object)).toBe(true)
})
test('isObject vs isAnyObject', () => {
function MyClass () {}
// This is correct old fashion syntax for classes, if this is missing
MyClass.prototype.constructor = MyClass
const myClass = new MyClass()
class MyClass2 {}
const myClass2 = new MyClass()
const mySpecialObject = {}
Object.setPrototypeOf(mySpecialObject, {
toDate: function () {
return new Date()
},
})
// IS OBJECT
// plain object
expect(isObject({})).toBe(true)
expect(isObject(new Object())).toBe(true)
expect(isPlainObject({})).toBe(true)
expect(isPlainObject(new Object())).toBe(true)
// classes & prototypes
expect(isObject(myClass)).toBe(false)
expect(isObject(myClass2)).toBe(false)
expect(isObject(mySpecialObject)).toBe(false)
expect(isPlainObject(myClass)).toBe(false)
expect(isPlainObject(myClass2)).toBe(false)
expect(isPlainObject(mySpecialObject)).toBe(false)
// arrays and dates
expect(isObject([])).toBe(false)
expect(isObject(new Array())).toBe(false)
expect(isObject(new Date('_'))).toBe(false)
expect(isObject(new Date())).toBe(false)
expect(isPlainObject([])).toBe(false)
expect(isPlainObject(new Array())).toBe(false)
expect(isPlainObject(new Date('_'))).toBe(false)
expect(isPlainObject(new Date())).toBe(false)
// IS ANY OBJECT
// plain object
expect(isAnyObject({})).toBe(true)
expect(isAnyObject(new Object())).toBe(true)
// classes & prototypes
expect(isAnyObject(myClass)).toBe(true)
expect(isAnyObject(myClass2)).toBe(true)
expect(isAnyObject(mySpecialObject)).toBe(true)
// arrays and dates
expect(isAnyObject([])).toBe(false)
expect(isAnyObject(new Array())).toBe(false)
expect(isAnyObject(new Date('_'))).toBe(false)
expect(isAnyObject(new Date())).toBe(false)
})