1
+ import { expect } from "chai" ;
2
+
3
+ import v from "src/utils/validators" ;
4
+
5
+ function check ( validator , value , field , errorCount ) {
6
+ let res = validator ( value , field ) ;
7
+ expect ( res ) . to . be . instanceof ( Array ) ;
8
+ expect ( res ) . to . be . length ( errorCount ) ;
9
+ return res ;
10
+ }
11
+
12
+ describe ( "Validators" , ( ) => {
13
+
14
+ describe ( "test Validators.number" , ( ) => {
15
+
16
+ let field = {
17
+ min : 5 ,
18
+ max : 10 ,
19
+ required : true
20
+ }
21
+
22
+ it ( "should give error if value is null, but field is required" , ( ) => {
23
+ check ( v . number , null , field , 1 ) ;
24
+ } ) ;
25
+
26
+ it ( "should give error if value is smaller than min" , ( ) => {
27
+ check ( v . number , - 1 , field , 1 ) ;
28
+ check ( v . number , 0 , field , 1 ) ;
29
+ check ( v . number , 3 , field , 1 ) ;
30
+ } ) ;
31
+
32
+ it ( "should give error if value is greater than max" , ( ) => {
33
+ check ( v . number , 15 , field , 1 ) ;
34
+ } ) ;
35
+
36
+ it ( "should not give error" , ( ) => {
37
+ check ( v . number , 5 , field , 0 ) ;
38
+ check ( v . number , 8 , field , 0 ) ;
39
+ check ( v . number , 10 , field , 0 ) ;
40
+ check ( v . number , 7.56 , field , 0 ) ;
41
+ } ) ;
42
+
43
+ it ( "should give error if value is string" , ( ) => {
44
+ check ( v . number , "Abc" , field , 1 ) ;
45
+ check ( v . number , "12 Abc" , field , 1 ) ;
46
+ check ( v . number , "" , field , 1 ) ;
47
+ check ( v . number , " " , field , 1 ) ;
48
+ } ) ;
49
+
50
+ it ( "should not give error if value is null and field is not required" , ( ) => {
51
+ field . required = false ;
52
+ check ( v . number , null , field , 0 ) ;
53
+ } ) ;
54
+
55
+ } ) ;
56
+
57
+ describe ( "test Validators.integer" , ( ) => {
58
+
59
+ let field = { }
60
+
61
+ it ( "should give error if value is not integer" , ( ) => {
62
+ check ( v . integer , 3.14 , field , 1 ) ;
63
+ check ( v . integer , "3.14" , field , 1 ) ;
64
+ } ) ;
65
+
66
+ it ( "should not give error if value is integer" , ( ) => {
67
+ check ( v . integer , - 5 , field , 0 ) ;
68
+ check ( v . integer , 0 , field , 0 ) ;
69
+ check ( v . integer , 10 , field , 0 ) ;
70
+ } ) ;
71
+
72
+ } ) ;
73
+
74
+ describe ( "test Validators.double" , ( ) => {
75
+
76
+ let field = { }
77
+
78
+ it ( "should give error if value is not double" , ( ) => {
79
+ check ( v . double , "3,14" , field , 1 ) ;
80
+ check ( v . double , false , field , 1 ) ;
81
+ } ) ;
82
+
83
+ it ( "should not give error if value is double" , ( ) => {
84
+ check ( v . double , 3.14 , field , 0 ) ;
85
+ } ) ;
86
+
87
+ } ) ;
88
+
89
+ describe ( "test Validators.string" , ( ) => {
90
+
91
+ let field = {
92
+ required : true ,
93
+ min : 3 ,
94
+ max : 10
95
+ }
96
+
97
+ it ( "should give error if value is null, but field is required" , ( ) => {
98
+ check ( v . string , null , field , 1 ) ;
99
+ } ) ;
100
+
101
+ it ( "should give error if value is smaller than min" , ( ) => {
102
+ check ( v . string , "" , field , 1 ) ;
103
+ check ( v . string , "A" , field , 1 ) ;
104
+ check ( v . string , "ab" , field , 1 ) ;
105
+ } ) ;
106
+
107
+ it ( "should give error if value is greater than max" , ( ) => {
108
+ check ( v . string , "abcdefghijklmnop" , field , 1 ) ;
109
+ } ) ;
110
+
111
+ it ( "should not give error" , ( ) => {
112
+ check ( v . string , "Foo" , field , 0 ) ;
113
+ check ( v . string , "Foobar" , field , 0 ) ;
114
+ check ( v . string , "John Doe" , field , 0 ) ;
115
+ check ( v . string , "Foobar7890" , field , 0 ) ;
116
+ } ) ;
117
+
118
+ it ( "should not give error if value is null and field is not required" , ( ) => {
119
+ field . required = false ;
120
+ check ( v . string , null , field , 0 ) ;
121
+ } ) ;
122
+ } ) ;
123
+
124
+ describe ( "test Validators.date" , ( ) => {
125
+
126
+ let field = {
127
+ required : true ,
128
+ min : 1262799081231 ,
129
+ max : 1562799081231
130
+ }
131
+
132
+ it ( "should give error if value is null, but field is required" , ( ) => {
133
+ check ( v . date , null , field , 1 ) ;
134
+ } ) ;
135
+
136
+ it ( "should not give error" , ( ) => {
137
+ check ( v . date , "2016-05-09" , field , 0 ) ;
138
+ check ( v . date , 1462799081231 , field , 0 ) ;
139
+ } ) ;
140
+
141
+ it ( "should give error if value is smaller than min" , ( ) => {
142
+ check ( v . date , 1220000000000 , field , 1 ) ;
143
+ check ( v . date , "1900-04-05" , field , 1 ) ;
144
+ } ) ;
145
+
146
+ it ( "should give error if value is greater than max" , ( ) => {
147
+ check ( v . date , 1600000000000 , field , 1 ) ;
148
+ check ( v . date , "2100-04-05" , field , 1 ) ;
149
+ } ) ;
150
+
151
+ } ) ;
152
+
153
+ } ) ;
0 commit comments