source: trip-planner-front/node_modules/is-what/test/ava.ts@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 10.9 KB
RevLine 
[6a3a178]1import test from 'ava'
2import {
3 isError,
4 isEmptyArray,
5 isObject,
6 isPlainObject,
7 isAnyObject,
8 isUndefined,
9 isNull,
10 isNullOrUndefined,
11 isFunction,
12 isArray,
13 isString,
14 isEmptyString,
15 isFullString,
16 isBoolean,
17 isRegExp,
18 isNumber,
19 isDate,
20 isSymbol,
21 isPrimitive,
22 isType,
23 isMap,
24 isWeakMap,
25 isSet,
26 isWeakSet,
27 isFullArray,
28 // isBlob,
29 // isFile,
30 isPromise,
31 isNaNValue,
32 isEmptyObject,
33 isOneOf,
34 isFullObject,
35} from '../src/index'
36
37// const blob = Buffer.from([])
38
39test('Basic true tests', t => {
40 t.is(isError(new Error('')), true)
41 t.is(isUndefined(undefined), true)
42 t.is(isNull(null), true)
43 t.is(isNullOrUndefined(null), true)
44 t.is(isNullOrUndefined(undefined), true)
45 t.is(isObject({}), true)
46 t.is(isEmptyObject({}), true)
47 t.is(isFullObject({0: ''}), true)
48 t.is(isFullObject({'': ''}), true)
49 t.is(isObject(new Object()), true)
50 t.is(isArray([]), true)
51 t.is(isEmptyArray([]), true)
52 t.is(isFullArray(['']), true)
53 t.is(isArray(new Array()), true)
54 t.is(isString(''), true)
55 t.is(isString('_'), true)
56 t.is(isEmptyString(''), true)
57 t.is(isFullString(' '), true)
58 t.is(isBoolean(true), true)
59 t.is(isBoolean(false), true)
60 t.is(isRegExp(/./), true)
61 t.is(isRegExp(/./gi), true)
62 t.is(isNumber(0), true)
63 t.is(isNumber(1), true)
64 t.is(isDate(new Date()), true)
65 t.is(isSymbol(Symbol()), true)
66 t.is(isMap(new Map()), true)
67 t.is(isWeakMap(new WeakMap()), true)
68 t.is(isSet(new Set()), true)
69 t.is(isWeakSet(new WeakSet()), true)
70 // t.is(isBlob(blob), true)
71 // t.is(isFile(new File([''], '', { type: 'text/html' })), true)
72 t.is(isPromise(new Promise((resolve, reject) => {})), true)
73})
74
75test('Basic false tests', t => {
76 t.is(isError({}), false)
77 t.is(isNumber(NaN), false)
78 t.is(isDate(new Date('_')), false)
79 t.is(isDate(NaN), false)
80 t.is(isUndefined(NaN), false)
81 t.is(isNull(NaN), false)
82 t.is(isObject(NaN), false)
83 t.is(isArray(NaN), false)
84 t.is(isString(NaN), false)
85 t.is(isEmptyString(' '), false)
86 t.is(isFullString(''), false)
87 t.is(isBoolean(NaN), false)
88 t.is(isRegExp(NaN), false)
89 t.is(isSymbol(NaN), false)
90 t.is(isMap(new WeakMap()), false)
91 t.is(isWeakMap(new Map()), false)
92 t.is(isSet(new WeakSet()), false)
93 t.is(isWeakSet(new Set()), false)
94 t.is(isNullOrUndefined(NaN), false)
95})
96
97test('isFunction', t => {
98 t.is(isFunction(NaN), false)
99 t.is(isFunction(() => {}), true)
100 t.is(isFunction(function () {}), true)
101 t.is(isFunction(async () => {}), true)
102 t.is(isFunction(async function () {}), true)
103 t.is(isFunction(function * () {}), true)
104 t.is(isFunction(async function * () {}), true)
105 const _ = { fn: () => {}, method () {} }
106 t.is(isFunction(_.fn), true)
107 t.is(isFunction(_.method), true)
108})
109
110test('isEmptyObject', t => {
111 t.is(isEmptyObject({}), true)
112 t.is(isEmptyObject(new Object()), true)
113
114 t.is(isEmptyObject('{}'), false)
115 t.is(isEmptyObject('{}'), false)
116 t.is(isEmptyObject(null), false)
117 t.is(isEmptyObject(new Date()), false)
118 t.is(isEmptyObject(new Error('')), false)
119 t.is(isEmptyObject(new Date()), false)
120 t.is(isEmptyObject(Symbol()), false)
121 t.is(isEmptyObject(new Map()), false)
122 t.is(isEmptyObject(new WeakMap()), false)
123 t.is(isEmptyObject(new Set()), false)
124 t.is(isEmptyObject(new WeakSet()), false)
125})
126
127test('isEmptyArray', t => {
128 t.is(isEmptyArray([]), true)
129 t.is(isEmptyArray(new Array()), true)
130 t.is(isEmptyArray(new Array(0)), true)
131
132 t.is(isEmptyArray(new Array(1)), false)
133 t.is(isEmptyArray([undefined]), false)
134 t.is(isEmptyArray(null), false)
135 t.is(isEmptyArray(new Date()), false)
136 t.is(isEmptyArray(new Error('')), false)
137 t.is(isEmptyArray(new Date()), false)
138 t.is(isEmptyArray(Symbol()), false)
139 t.is(isEmptyArray(new Map()), false)
140 t.is(isEmptyArray(new WeakMap()), false)
141 t.is(isEmptyArray(new Set()), false)
142 t.is(isEmptyArray(new WeakSet()), false)
143})
144
145test('isFullArray', t => {
146 t.is(isFullArray(new Array(1)), true)
147 t.is(isFullArray([undefined]), true)
148 t.is(isFullArray([null]), true)
149 t.is(isFullArray(['']), true)
150
151 t.is(isFullArray([]), false)
152 t.is(isFullArray(new Array()), false)
153 t.is(isFullArray(new Array(0)), false)
154
155 t.is(isFullArray(null), false)
156 t.is(isFullArray(new Date()), false)
157 t.is(isFullArray(new Error('')), false)
158 t.is(isFullArray(new Date()), false)
159 t.is(isFullArray(Symbol()), false)
160 t.is(isFullArray(new Map()), false)
161 t.is(isFullArray(new WeakMap()), false)
162 t.is(isFullArray(new Set()), false)
163 t.is(isFullArray(new WeakSet()), false)
164})
165
166test('NaN tests', t => {
167 t.is(isNaNValue(NaN), true)
168 t.is(isNaNValue(new Error('')), false)
169 t.is(isNaNValue(undefined), false)
170 t.is(isNaNValue(null), false)
171 t.is(isNaNValue(undefined), false)
172 t.is(isNaNValue({}), false)
173 t.is(isNaNValue(new Object()), false)
174 t.is(
175 isNaNValue(() => {}),
176 false
177 )
178 t.is(isNaNValue([]), false)
179 t.is(isNaNValue(new Array()), false)
180 t.is(isNaNValue(''), false)
181 t.is(isNaNValue('_'), false)
182 t.is(isNaNValue(''), false)
183 t.is(isNaNValue(' '), false)
184 t.is(isNaNValue(true), false)
185 t.is(isNaNValue(false), false)
186 t.is(isNaNValue(/./), false)
187 t.is(isNaNValue(/./gi), false)
188 t.is(isNaNValue(0), false)
189 t.is(isNaNValue(1), false)
190 t.is(isNaNValue(new Date()), false)
191 t.is(isNaNValue(Symbol()), false)
192 t.is(isNaNValue(new Map()), false)
193 t.is(isNaNValue(new WeakMap()), false)
194 t.is(isNaNValue(new Set()), false)
195 t.is(isNaNValue(new WeakSet()), false)
196 t.is(isNaNValue(new Promise((resolve, reject) => {})), false)
197})
198
199test('Primitive tests', t => {
200 // true
201 t.is(isPrimitive(0), true)
202 t.is(isPrimitive(''), true)
203 t.is(isPrimitive('str'), true)
204 t.is(isPrimitive(Symbol()), true)
205 t.is(isPrimitive(true), true)
206 t.is(isPrimitive(false), true)
207 t.is(isPrimitive(null), true)
208 t.is(isPrimitive(undefined), true)
209 // false
210 t.is(isPrimitive(NaN), false)
211 t.is(isPrimitive([]), false)
212 t.is(isPrimitive(new Array()), false)
213 t.is(isPrimitive({}), false)
214 t.is(isPrimitive(new Object()), false)
215 t.is(isPrimitive(new Date()), false)
216 t.is(
217 isPrimitive(() => {}),
218 false
219 )
220})
221
222test('Date exception', t => {
223 t.is(isDate(new Date('_')), false)
224})
225
226test('Generic isType', t => {
227 // -----------------------------
228 // This is correct old fashion syntax for classes, if this is missing
229 function MyClass () {}
230 MyClass.prototype.constructor = MyClass
231 // @ts-ignore
232 const myClass = new MyClass()
233 // -----------------------------
234 class MyOtherClass { constructor() {} }
235 // this is expected behaviour
236 t.is(isType('', String), true)
237 t.is(isType('_', String), true)
238 t.is(isType('Hello World', String), true)
239 t.is(isType(NaN, Number), true)
240 t.is(isType(0, Number), true)
241 t.is(isType(1, Number), true)
242 t.is(isType({}, Object), true)
243 t.is(isType(new Object(), Object), true)
244 t.is(isType([], Array), true)
245 t.is(isType(new Array(), Array), true)
246 t.is(
247 isType(() => {}, Function),
248 true
249 )
250 t.is(isType(true, Boolean), true)
251 t.is(isType(false, Boolean), true)
252 t.is(isType(new Date('_'), Date), true)
253 t.is(isType(new Date(), Date), true)
254 t.is(isType(/./, RegExp), true)
255 t.is(isType(/./gi, RegExp), true)
256 t.is(isType(myClass, MyClass), true)
257 t.is(isType(new MyOtherClass(), MyOtherClass), true)
258 t.is(isType(myClass, MyOtherClass), false)
259 t.is(isType(Symbol(), Symbol), true)
260 // t.is(isType(null, Null), true)
261 // t.is(isType(undefined, Undefined), true)
262 // It SHOULD fail
263 t.is(isType(5, String), false)
264 t.is(isType(null, Object), false)
265 // Not sure if this would be the expected behaviour but everything is an object
266 // so I would say so
267 t.is(isType(myClass, Object), true)
268})
269
270test('isObject vs isAnyObject', t => {
271 // -----------------------------
272 // This is correct old fashion syntax for classes, if this is missing
273 function MyClass () {}
274 MyClass.prototype.constructor = MyClass
275 // @ts-ignore
276 const myClass = new MyClass()
277 // -----------------------------
278 class MyClass2 { constructor() {} }
279 const myClass2 = new MyClass2()
280 const mySpecialObject = {}
281 Object.setPrototypeOf(mySpecialObject, {
282 toDate: function () {
283 return new Date()
284 },
285 })
286 // IS OBJECT
287 // plain object
288 t.is(isObject({}), true)
289 t.is(isObject(new Object()), true)
290 t.is(isPlainObject({}), true)
291 t.is(isPlainObject(new Object()), true)
292 // classes & prototypes
293 t.is(isObject(myClass), false)
294 t.is(isObject(myClass2), false)
295 t.is(isObject(mySpecialObject), false)
296 t.is(isPlainObject(myClass), false)
297 t.is(isPlainObject(myClass2), false)
298 t.is(isPlainObject(mySpecialObject), false)
299 // arrays and dates
300 t.is(isObject([]), false)
301 t.is(isObject(new Array()), false)
302 t.is(isObject(new Date('_')), false)
303 t.is(isObject(new Date()), false)
304 t.is(isPlainObject([]), false)
305 t.is(isPlainObject(new Array()), false)
306 t.is(isPlainObject(new Date('_')), false)
307 t.is(isPlainObject(new Date()), false)
308 // IS ANY OBJECT
309 // plain object
310 t.is(isAnyObject({}), true)
311 t.is(isAnyObject(new Object()), true)
312 // classes & prototypes
313 t.is(isAnyObject(myClass), true)
314 t.is(isAnyObject(myClass2), true)
315 t.is(isAnyObject(mySpecialObject), true)
316 // arrays and dates
317 t.is(isAnyObject([]), false)
318 t.is(isAnyObject(new Array()), false)
319 t.is(isAnyObject(new Date('_')), false)
320 t.is(isAnyObject(new Date()), false)
321})
322
323test('isOneOf', t => {
324 t.is(isOneOf(isString, isNumber)('_'), true)
325 t.is(isOneOf(isString, isNumber)(1), true)
326 t.is(isOneOf(isString, isNumber)(undefined), false)
327
328 t.is(isOneOf(isString, isNumber, isBoolean)('_'), true)
329 t.is(isOneOf(isString, isNumber, isBoolean)(1), true)
330 t.is(isOneOf(isString, isNumber, isBoolean)(true), true)
331 t.is(isOneOf(isString, isNumber, isBoolean)(undefined), false)
332
333 t.is(isOneOf(isString, isNumber, isBoolean, isArray)('_'), true)
334 t.is(isOneOf(isString, isNumber, isBoolean, isArray)(1), true)
335 t.is(isOneOf(isString, isNumber, isBoolean, isArray)(true), true)
336 t.is(isOneOf(isString, isNumber, isBoolean, isArray)([]), true)
337 t.is(isOneOf(isString, isNumber, isBoolean, isArray)(undefined), false)
338
339 t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)('_'), true)
340 t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)(1), true)
341 t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)(true), true)
342 t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)([]), true)
343 t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)({}), true)
344 t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)(undefined), false)
345})
346
347test('type related tests', t => {
348 t.pass()
349 // const fn: string | ((k: number) => string) = (p) => 'a'
350 // if (!isFunction(fn)) {
351 // fn
352 // }
353
354 // const a: Record<string, number> = {}
355
356 // a[fn(1)] = fn(2)
357
358 // const myArray: string | string[] = ['a', 'b']
359 // if (!isArray(myArray)) {
360 // myArray
361 // }
362
363 // const a: Record<string, number> = {}
364
365 // a[myArray[1]] = myArray[0]
366
367 // const myArray: string | any[] = [1, 2, 'a', 'b']
368 // if (!isArray(myArray)) {
369 // myArray
370 // }
371
372 // const a: Record<string, number> = {}
373
374 // a[myArray[1]] = myArray[0]
375
376})
Note: See TracBrowser for help on using the repository browser.