-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtypy.test.js
549 lines (483 loc) · 18.5 KB
/
typy.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import t, { Schema, addCustomTypes } from '../src/index'; // eslint-disable-line import/no-named-as-default
describe('Typy', () => {
describe('Defined/Undefined', () => {
const obj = {
goodKey: 'hooray'
};
test('should test if object/key is defined', () => {
expect(t(obj.goodKey).isDefined === true).toBeTruthy();
expect(t(obj.goodKey).isUndefined === false).toBeTruthy();
});
test('should test if nested object/key is defined', () => {
const deepObj = {
nestedKey: {
goodKey: 'hello',
numberKey: 10,
zeroKey: 0,
objKey: {}
}
};
expect(t(deepObj, 'nestedKey.goodKey').isDefined === true).toBeTruthy();
expect(t(deepObj, 'nestedKey.goodKey').isUndefined === false).toBeTruthy();
expect(t(deepObj, 'nestedKey.goodKey').isString === true).toBeTruthy();
expect(t(deepObj, 'nestedKey.numberKey').isNumber === true).toBeTruthy();
expect(t(deepObj, 'nestedKey.numberKey').isTruthy === true).toBeTruthy();
expect(t(deepObj, 'nestedKey.numberKey').isFalsy === false).toBeTruthy();
expect(t(deepObj, 'nestedKey.zeroKey').isNumber === true).toBeTruthy();
expect(t(deepObj, 'nestedKey.zeroKey').isTruthy === false).toBeTruthy();
expect(t(deepObj, 'nestedKey.zeroKey').isFalsy === true).toBeTruthy();
expect(t(deepObj.nestedKey).isDefined === true).toBeTruthy();
expect(t(deepObj, 'nestedKey').isDefined === true).toBeTruthy();
expect(t(deepObj, 'nestedKey.objKey').isDefined === true).toBeTruthy();
expect(t(deepObj, 'nestedKey.objKey').isUndefined === false).toBeTruthy();
expect(t(deepObj, 'nestedKey.objKey').isObject === true).toBeTruthy();
expect(t(deepObj, 'nestedKey.objKey').isFunction === false).toBeTruthy();
expect(t(deepObj, 'nestedKey.objKey').isArray === false).toBeTruthy();
});
test('should test if object/key is undefined', () => {
expect(t(obj.badKey).isUndefined === true).toBeTruthy();
expect(t(obj.badKey).isDefined === false).toBeTruthy();
});
test('should test if nested object/key is undefined', () => {
expect(t(obj, 'goodKey.badKey').isUndefined === true).toBeTruthy();
expect(t(obj, 'badKey.superBadKey').isUndefined === true).toBeTruthy();
expect(t(obj, 'badKey.superBadKey').isDefined === false).toBeTruthy();
});
});
describe('Null', () => {
test('should test if object is null', () => {
const obj = null;
expect(t(obj).isNull === true).toBeTruthy();
});
});
describe('Null or Undefined', () => {
test('should test if object is null', () => {
const nullObj = null;
expect(t(nullObj).isNullOrUndefined === true).toBeTruthy();
const obj = {
goodKey: 'hooray'
};
expect(t(obj.badKey).isNullOrUndefined === true).toBeTruthy();
expect(t('hello').isNullOrUndefined === false).toBeTruthy();
expect(t(false).isNullOrUndefined === false).toBeTruthy();
expect(t(22).isNullOrUndefined === false).toBeTruthy();
expect(t([]).isNullOrUndefined === false).toBeTruthy();
expect(t({}).isNullOrUndefined === false).toBeTruthy();
});
});
describe('Truthy/Falsy', () => {
test('should test if object is truthy', () => {
const truthyValues = ['hey', 11, {}, { yo: 'yoyo' }, true, [], [1, 2], new Boolean(23)]; // eslint-disable-line no-new-wrappers
truthyValues.map((value) => {
expect(t(value).isTruthy === true).toBeTruthy();
expect(t(value).isFalsy === false).toBeTruthy();
return value;
});
});
test('should test if object is falsy', () => {
const mockObj = { goodKey: 'hello' };
const falsyValues = ['', 0, null, mockObj.badKey, false, NaN];
falsyValues.map((value) => {
expect(t(value).isFalsy === true).toBeTruthy();
expect(t(value).isTruthy === false).toBeTruthy();
return value;
});
});
});
describe('Type', () => {
test('should test if type is object', () => {
const obj = {};
expect(t(obj).isObject === true).toBeTruthy();
});
test('should test if object is empty', () => {
const obj = {};
expect(t(obj).isEmptyObject === true).toBeTruthy();
expect(t('hello').isEmptyObject === false).toBeTruthy();
expect(t(false).isEmptyObject === false).toBeTruthy();
expect(t(22).isEmptyObject === false).toBeTruthy();
expect(t([]).isEmptyObject === false).toBeTruthy();
});
test('should test if type is string', () => {
const obj = 'hello';
const stringObject = new String('myStringObject'); // eslint-disable-line no-new-wrappers
expect(t(obj).isString === true).toBeTruthy();
expect(t(stringObject).isString === true).toBeTruthy();
});
test('should test if string is empty string', () => {
const obj = '';
expect(t(obj).isEmptyString === true).toBeTruthy();
expect(t('hello').isEmptyString === false).toBeTruthy();
expect(t(false).isEmptyString === false).toBeTruthy();
expect(t(22).isEmptyString === false).toBeTruthy();
expect(t([]).isEmptyString === false).toBeTruthy();
expect(t({}).isEmptyString === false).toBeTruthy();
});
test('should test if type is Number', () => {
let num = 22;
expect(t(num).isNumber === true).toBeTruthy();
num = -22;
expect(t(num).isNumber === true).toBeTruthy();
num = 0;
expect(t(num).isNumber === true).toBeTruthy();
num = 22.345;
expect(t(num).isNumber === true).toBeTruthy();
num = 'number';
expect(t(num).isNumber === false).toBeTruthy();
num = Infinity;
expect(t(num).isNumber === true).toBeTruthy();
num = new Number(23523452345); // eslint-disable-line no-new-wrappers
expect(t(num).isNumber === true).toBeTruthy();
});
test('should test if type is Boolean', () => {
const trueObj = true;
expect(t(trueObj).isBoolean === true).toBeTruthy();
const falseObj = false;
expect(t(falseObj).isBoolean === true).toBeTruthy();
});
test('should test if object is true', () => {
const obj = true;
expect(t(obj).isTrue === true).toBeTruthy();
expect(t('hello').isTrue === false).toBeTruthy();
expect(t(false).isNullOrUndefined === false).toBeTruthy();
expect(t(22).isTrue === false).toBeTruthy();
expect(t([]).isTrue === false).toBeTruthy();
expect(t({}).isTrue === false).toBeTruthy();
});
test('should test if object is false', () => {
const obj = false;
expect(t(obj).isFalse === true).toBeTruthy();
expect(t('hello').isFalse === false).toBeTruthy();
expect(t(true).isFalse === false).toBeTruthy();
expect(t(22).isFalse === false).toBeTruthy();
expect(t([]).isFalse === false).toBeTruthy();
expect(t({}).isFalse === false).toBeTruthy();
});
test('should test if type is Array', () => {
const obj = ['Howdy!'];
expect(t(obj).isArray === true).toBeTruthy();
expect(t(obj).isObject === false).toBeTruthy();
});
test('should test if Array is Empty Array', () => {
const obj = [];
expect(t(obj).isEmptyArray === true).toBeTruthy();
expect(t('').isEmptyArray === false).toBeTruthy();
expect(t(false).isEmptyArray === false).toBeTruthy();
expect(t(22).isEmptyArray === false).toBeTruthy();
expect(t({}).isEmptyArray === false).toBeTruthy();
});
test('should test if type is Function', () => {
const func = () => {};
expect(t(func).isFunction === true).toBeTruthy();
});
test('should test if type is Date', () => {
const date = new Date();
expect(t(date).isDate === true).toBeTruthy();
});
test('should test if type is Symbol', () => {
const mySymbol = Symbol('someSymbol');
expect(t(mySymbol).isSymbol === true).toBeTruthy();
expect(t(Object(mySymbol)).isSymbol === true).toBeTruthy();
expect(t('Hello World').isSymbol === false).toBeTruthy();
expect(t({}).isSymbol === false).toBeTruthy();
expect(t([]).isSymbol === false).toBeTruthy();
expect(t(23).isSymbol === false).toBeTruthy();
});
});
describe('Safe Object', () => {
const deepObj = {
nestedKey: {
goodKey: 'hello',
numberKey: 10,
zeroKey: 0,
objKey: {}
}
};
test('should return the object if found in path', () => {
expect(t(deepObj).safeObject).toEqual(deepObj);
expect(t(deepObj.nestedKey).safeObject).toEqual(deepObj.nestedKey);
expect(t(deepObj, 'nestedKey').safeObject).toEqual(deepObj.nestedKey);
expect(t(deepObj.nestedKey.goodKey).safeObject).toEqual(deepObj.nestedKey.goodKey);
expect(t(deepObj, 'nestedKey.goodKey').safeObject).toEqual(deepObj.nestedKey.goodKey);
});
test('should not throw if object not found in path', () => {
expect(t(deepObj, 'badkey').safeObject).toEqual(undefined);
expect(t(deepObj, 'badKey.goodKey').safeObject).toEqual(undefined);
expect(t(deepObj, 'badkey').safeObjectOrEmpty).toEqual({});
expect(t(deepObj, 'badKey.goodKey').safeObjectOrEmpty).toEqual({});
});
});
describe('Safe Object Or Empty', () => {
const deepObj = {
nestedKey: {
goodKey: 'hello',
numberKey: 10,
zeroKey: 0,
objKey: {}
}
};
test('should return the object if found in path', () => {
expect(t(deepObj).safeObjectOrEmpty).toEqual(deepObj);
expect(t(deepObj.nestedKey).safeObjectOrEmpty).toEqual(deepObj.nestedKey);
expect(t(deepObj, 'nestedKey').safeObjectOrEmpty).toEqual(deepObj.nestedKey);
expect(t(deepObj.nestedKey.goodKey).safeObjectOrEmpty).toEqual(deepObj.nestedKey.goodKey);
expect(t(deepObj, 'nestedKey.goodKey').safeObjectOrEmpty).toEqual(deepObj.nestedKey.goodKey);
});
test('should return an empty object if object not found in path', () => {
expect(t(deepObj, 'badkey').safeObjectOrEmpty).toEqual({});
expect(t(deepObj, 'badKey.goodKey').safeObjectOrEmpty).toEqual({});
expect(t(deepObj, 'goodKey.goodKey').safeObjectOrEmpty).toEqual({});
});
});
describe('Safe String', () => {
test('should return the string if type is string', () => {
const str = 'hello there';
expect(t(str).safeString === str).toBeTruthy();
});
test('should return empty string if type is not string', () => {
let obj = null;
expect(t(obj).safeString === '').toBeTruthy();
obj = 22;
expect(t(obj).safeString === '').toBeTruthy();
obj = {};
expect(t(obj).safeString === '').toBeTruthy();
obj = undefined;
expect(t(obj).safeString === '').toBeTruthy();
obj = [];
expect(t(obj).safeString === '').toBeTruthy();
expect(t(obj.badKey).safeString === '').toBeTruthy();
});
});
describe('Safe Number', () => {
test('should return the number if type is number', () => {
const num = 22;
expect(t(num).safeNumber === num).toBeTruthy();
});
test('should return 0 if type is not Number', () => {
let obj = null;
expect(t(obj).safeNumber === 0).toBeTruthy();
obj = 'str';
expect(t(obj).safeNumber === 0).toBeTruthy();
obj = {};
expect(t(obj).safeNumber === 0).toBeTruthy();
obj = undefined;
expect(t(obj).safeNumber === 0).toBeTruthy();
obj = [];
expect(t(obj).safeNumber === 0).toBeTruthy();
expect(t(obj.badKey).safeNumber === 0).toBeTruthy();
});
});
describe('Safe Boolean', () => {
test('should return the boolean if type is boolean', () => {
const bool = true;
expect(t(bool).safeBoolean === true).toBeTruthy();
});
test('should return 0 if type is not Number', () => {
let obj = null;
expect(t(obj).safeBoolean === false).toBeTruthy();
obj = 'str';
expect(t(obj).safeBoolean === false).toBeTruthy();
obj = {};
expect(t(obj).safeBoolean === false).toBeTruthy();
obj = undefined;
expect(t(obj).safeBoolean === false).toBeTruthy();
obj = [];
expect(t(obj).safeBoolean === false).toBeTruthy();
expect(t(obj.badKey).safeBoolean === false).toBeTruthy();
});
});
describe('Safe Function', () => {
test('should return the function if type is function', () => {
const func = () => {};
expect(t(func).safeFunction === func).toBeTruthy();
});
test('should return () => {} if type is not function', () => {
let obj = null;
expect(typeof t(obj).safeFunction === 'function').toBeTruthy();
obj = 'str';
expect(typeof t(obj).safeFunction === 'function').toBeTruthy();
obj = {};
expect(typeof t(obj).safeFunction === 'function').toBeTruthy();
obj = undefined;
expect(typeof t(obj).safeFunction === 'function').toBeTruthy();
obj = [];
expect(typeof t(obj).safeFunction === 'function').toBeTruthy();
expect(typeof t(obj).safeFunction === 'function').toBeTruthy();
});
});
describe('Safe Array', () => {
const deepObj = {
nestedKey: [
{
goodKey: ['hello'],
numberKey: 10,
objKey: {
data: 'irrelevant',
},
funcKey: () => {}
}
],
nullKey: null,
};
test('should return the nested array in path', () => {
expect(t(deepObj.nestedKey).safeArray).toEqual(deepObj.nestedKey);
expect(t(deepObj, 'nestedKey').safeArray).toEqual(deepObj.nestedKey);
expect(t(deepObj.nestedKey[0].goodKey).safeArray).toEqual(deepObj.nestedKey[0].goodKey);
expect(t(deepObj, 'nestedKey[0].goodKey').safeArray).toEqual(deepObj.nestedKey[0].goodKey);
});
test('should return an array with a single value if path not an array', () => {
expect(t(deepObj.nestedKey[0].numberKey).isNumber);
expect(t(deepObj.nestedKey[0].numberKey).safeArray).toEqual([deepObj.nestedKey[0].numberKey]);
expect(t(deepObj, 'nestedKey[0].numberKey').safeArray).toEqual([deepObj.nestedKey[0].numberKey]);
expect(t(deepObj.nestedKey[0].objKey).isObject);
expect(t(deepObj.nestedKey[0].objKey).safeArray).toEqual([deepObj.nestedKey[0].objKey]);
expect(t(deepObj, 'nestedKey[0].objKey').safeArray).toEqual([deepObj.nestedKey[0].objKey]);
expect(t(deepObj.nestedKey[0].funcKey).isFunction);
expect(t(deepObj.nestedKey[0].funcKey).safeArray).toEqual([deepObj.nestedKey[0].funcKey]);
expect(t(deepObj, 'nestedKey[0].funcKey').safeArray).toEqual([deepObj.nestedKey[0].funcKey]);
});
test('should not throw if path not found', () => {
expect(t(deepObj, 'nestedKey[0].undefinedKey').safeArray).toEqual([]);
expect(t(deepObj, 'nestedKey[1]').safeArray).toEqual([]);
expect(t(deepObj, 'nullKey').isNull).toEqual(true);
expect(t(deepObj, 'nullKey').safeArray).toEqual([]);
});
});
describe('New Instance', () => {
test('should return new instance for each input', () => {
const stringType = t('hello');
expect(stringType.isString === true).toBeTruthy();
const numberType = t(123);
expect(numberType.isNumber === true).toBeTruthy();
expect(stringType.isString === true).toBeTruthy();
const dateType = t(new Date());
expect(dateType.isDate === true).toBeTruthy();
});
});
describe('Monkey Test', () => {
test('should not throw error for any input', () => {
const mockObj = { goodKey: 'hello' };
const monkeyInputs = [
1, 0, -1, '', 'hey', true, false, {}, { yo: 'yoyo' },
[], [1, 2], null, undefined, mockObj.goodKey, mockObj.badKey,
NaN, () => {}, Error, new Error(), Object,
];
monkeyInputs.map((input) => {
expect(() => t(input)).not.toThrow();
return input;
});
});
});
describe('Check Object Schema', () => {
const batmanObject = {
name: 'Batman',
data: [
{
kills: 1001,
build: [
{
species: 'Human',
weight: 100
}
]
}
],
lastSeen: new Date()
};
const superheroSchema = {
name: Schema.String,
data: [
{
kills: Schema.Number,
build: [
{
species: Schema.String,
weight: Schema.Number
}
]
}
],
lastSeen: Schema.Date
};
test('should return true if object and schema are a valid match', () => {
expect(t(batmanObject, superheroSchema).input).toEqual(batmanObject);
expect(t(batmanObject, superheroSchema).schemaCheck).toEqual(true);
expect(t(batmanObject, superheroSchema).isValid).toEqual(true);
});
test('should not throw if object and schema are not a valid match', () => {
const weirdSuperheroSchema = {
name: Schema.Number,
data: [
{
kills: Schema.Number,
build: [
{
species: Schema.String,
weight: Schema.Number
}
]
}
]
};
expect(t(batmanObject, weirdSuperheroSchema).schemaCheck).toEqual(false);
expect(t(batmanObject, weirdSuperheroSchema).isValid).toEqual(false);
});
});
test('Monkey Test - Schema Validation', () => {
let expectedSchema = {
name: Schema.String,
arr: Schema.Array
};
let obj = {
name: 'Jack',
arr: [1, 2, 3]
};
expect(t(obj, expectedSchema).isValid).toEqual(true);
obj = {
name: 'Jack',
arr: ['1', 2, 3]
};
expect(t(obj, expectedSchema).isValid).toEqual(true);
obj = {
name: 'Jack',
arr: [{
key: 'val'
}]
};
expect(t(obj, expectedSchema).isValid).toEqual(true);
obj = {
name: 22,
arr: ['1', 2, 3]
};
expect(t(obj, expectedSchema).isValid).toEqual(false);
expectedSchema = {
name: Schema.String,
arr: [{
key: Schema.String
}]
};
obj = {
name: 'Jack',
arr: [{
key: 'val'
}]
};
expect(t(obj, expectedSchema).isValid).toEqual(true);
obj = {
name: 'Jack',
arr: [1, 2, 4]
};
expect(t(obj, expectedSchema).isValid).toEqual(false);
});
describe('Custom Type Check', () => {
test('should validate custom type checks', () => {
addCustomTypes({
isPhone: input => (t(input).isNumber && /^\d{10}$/g.test(String(input))),
isAddress: input => (t(input).isString && input.toUpperCase().includes('DOWNING STREET'))
});
expect(t(9892389239).isPhone === true).toBeTruthy();
expect(t(98923892390).isPhone === false).toBeTruthy();
expect(t('10 Downing Street haha').isAddress === true).toBeTruthy();
expect(t('10 Street haha').isAddress === false).toBeTruthy();
});
});
});