1
+ import { isArray , isNonEmptyString , isNonNullObject , isNumber , isObject , isString , isURL } from "./validator"
2
+
3
+ describe ( "validator" , ( ) => {
4
+ describe ( "isURL" , ( ) => {
5
+ test . each ( [
6
+ [ "http://example.com/" , true ] ,
7
+ [ "http://example.com" , true ] ,
8
+ [ "https://example.com/" , true ] ,
9
+ [ "https://example.com" , true ] ,
10
+ [ "https://www.example.com:8080" , true ] ,
11
+ [ "http://localhost/path/name/" , true ] ,
12
+ [ "https://www.example.com:8080/path/name/index.php?a=1&b=2&c=3#abcd" , true ] ,
13
+ [ "http://www.example.com:8080/path/name/index.php?a=1&b=2&c=3#abcd" , true ] ,
14
+ [ "http://localhost/path/name/index.php?a=1&b=2&c=3#abcd" , true ] ,
15
+ [ "http://127.0.0.1/path/name/index.php?a=1&b=2&c=3#abcd" , true ] ,
16
+ [ "http://a--b.c-c.co-uk/" , true ] ,
17
+ [ null , false ] ,
18
+ [ undefined , false ] ,
19
+ [ [ "https://example.com" ] , false ] , // non-null string
20
+ [ "ftp://www.example.com:8080/path/name/file.png" , false ] ,
21
+ [ "http://-abc.com" , false ] ,
22
+ [ "http://www._abc.com" , false ] ,
23
+ [ "http://.com" , false ] ,
24
+ [ "123456789" , false ]
25
+ ] ) ( "%p" , ( param , want ) => {
26
+ expect ( isURL ( param ) ) . toBe ( want )
27
+ } )
28
+ } )
29
+
30
+ describe ( "isNumber" , ( ) => {
31
+ describe ( "non-number" , ( ) => {
32
+ const nonNumbers = [ undefined , null , true , false , '' , 'a' , [ ] , [ 'a' ] , { } , { a : 1 } ]
33
+ nonNumbers . forEach ( ( v ) => {
34
+ it ( `${ v } ` , ( ) => expect ( isNumber ( v ) ) . toBeFalsy ( ) )
35
+ } )
36
+ } )
37
+
38
+ describe ( "number" , ( ) => {
39
+ const numbers = [ NaN , 0 , - 1 , 1 , Number . MAX_SAFE_INTEGER , Number . MIN_SAFE_INTEGER , Infinity , - Infinity ]
40
+ numbers . forEach ( ( v ) => {
41
+ it ( `${ v } ` , ( ) => expect ( isNumber ( v ) ) . toBeTruthy ( ) )
42
+ } )
43
+ } )
44
+ } )
45
+
46
+ describe ( "isString" , ( ) => {
47
+ describe ( "non-string" , ( ) => {
48
+ const nonStrings = [ undefined , null , NaN , 0 , 1 , true , false , [ ] , [ 'a' ] , { } , { a : 1 } ]
49
+ nonStrings . forEach ( ( v ) => {
50
+ it ( `${ v } ` , ( ) => expect ( isString ( v ) ) . toBeFalsy ( ) )
51
+ } )
52
+ } )
53
+
54
+ describe ( "string" , ( ) => {
55
+ const strings = [
56
+ "" ,
57
+ " " ,
58
+ "foo"
59
+ ]
60
+ strings . forEach ( ( v ) => {
61
+ it ( `${ v } ` , ( ) => expect ( isString ( v ) ) . toBeTruthy ( ) )
62
+ } )
63
+ } )
64
+ } )
65
+
66
+ describe ( "isNonEmptyString" , ( ) => {
67
+ describe ( "non-non-empty-string" , ( ) => {
68
+ const nonStrings = [ undefined , null , NaN , 0 , 1 , true , false , [ ] , [ 'a' ] , { } , { a : 1 } , "" ]
69
+ nonStrings . forEach ( ( v ) => {
70
+ it ( `${ v } ` , ( ) => expect ( isNonEmptyString ( v ) ) . toBeFalsy ( ) )
71
+ } )
72
+ } )
73
+
74
+ describe ( "non-empty-string" , ( ) => {
75
+ const strings = [
76
+ " " ,
77
+ "foo"
78
+ ]
79
+ strings . forEach ( ( v ) => {
80
+ it ( `${ v } ` , ( ) => expect ( isNonEmptyString ( v ) ) . toBeTruthy ( ) )
81
+ } )
82
+ } )
83
+ } )
84
+
85
+ describe ( "isArray" , ( ) => {
86
+ describe ( "non-array" , ( ) => {
87
+ const nonArrays = [ undefined , null , NaN , 0 , 1 , '' , 'a' , true , false , { } , { a : 1 } ]
88
+ nonArrays . forEach ( ( v ) => {
89
+ it ( `${ v } ` , ( ) => expect ( isArray ( v ) ) . toBeFalsy ( ) )
90
+ } )
91
+ } )
92
+
93
+ describe ( "array" , ( ) => {
94
+ const arrays = [
95
+ [ ] ,
96
+ [ 1 , 2 , 3 ] ,
97
+ new Array ( ) ,
98
+ new Array ( 1 , 2 , 3 ) ,
99
+ ]
100
+ arrays . forEach ( ( v ) => {
101
+ it ( `${ v } ` , ( ) => expect ( isArray ( v ) ) . toBeTruthy ( ) )
102
+ } )
103
+ } )
104
+ } )
105
+
106
+ describe ( "isObject" , ( ) => {
107
+ describe ( "non-object" , ( ) => {
108
+ const nonObjects = [ undefined , NaN , 0 , 1 , true , false , '' , 'a' , [ ] , [ 'a' ] ]
109
+ nonObjects . forEach ( ( v ) => {
110
+ it ( `${ v } ` , ( ) => expect ( isObject ( v ) ) . toBeFalsy ( ) )
111
+ } )
112
+ } )
113
+
114
+ describe ( "object" , ( ) => {
115
+ const objects = [
116
+ null ,
117
+ { } ,
118
+ { a : 1 }
119
+ ]
120
+ objects . forEach ( ( v ) => {
121
+ it ( `${ v } ` , ( ) => expect ( isObject ( v ) ) . toBeTruthy ( ) )
122
+ } )
123
+ } )
124
+ } )
125
+
126
+ describe ( "isNonNullObject" , ( ) => {
127
+ describe ( "non-non-null-object" , ( ) => {
128
+ const nonNonNullObjects = [ undefined , NaN , 0 , 1 , true , false , '' , 'a' , [ ] , [ 'a' ] , null ]
129
+ nonNonNullObjects . forEach ( ( v ) => {
130
+ it ( `${ v } ` , ( ) => expect ( isNonNullObject ( v ) ) . toBeFalsy ( ) )
131
+ } )
132
+ } )
133
+
134
+ describe ( "object" , ( ) => {
135
+ const nonNullObjects = [
136
+ { } ,
137
+ { a : 1 }
138
+ ]
139
+ nonNullObjects . forEach ( ( v ) => {
140
+ it ( `${ v } ` , ( ) => expect ( isNonNullObject ( v ) ) . toBeTruthy ( ) )
141
+ } )
142
+ } )
143
+ } )
144
+ } )
0 commit comments