1+ // const util = require('../lib/util');
2+ const validate = require ( '../lib/validate' ) ;
3+
4+ // Test data
5+ const validData = '' ;
6+ const invalidData = '' ;
7+ const validatorDefault = {
8+ name : 'default' ,
9+ check : function ( data , model ) {
10+ return ( data === validData ) ;
11+ }
12+ } ;
13+
14+ const validators = {
15+ clear : function ( ) {
16+ validate . validators = [ ] ;
17+ } ,
18+ add : function ( validator ) {
19+ if ( validator ) {
20+ validate . validators . push ( validator ) ;
21+ } else {
22+ validate . validators . push ( validatorDefault ) ;
23+ }
24+ } ,
25+ remove : function ( validator ) {
26+ if ( typeof validator === 'string' ) {
27+ remValidator ( validator ) ;
28+ } else if ( validator && typeof validator . name === 'string' ) {
29+ remValidator ( validator . name ) ;
30+ }
31+ }
32+ } ;
33+
34+ function remValidator ( name ) {
35+ for ( var i = validate . validators . length - 1 ; i >= 0 ; i -- ) {
36+ if ( validate . validators [ i ] . name === name ) {
37+ validate . validators . splice ( i , 1 ) ;
38+ }
39+ }
40+ }
41+
42+ // Start testing
43+ describe ( ' - Validate' , ( ) => {
44+ it ( 'shall be an object' , ( ) => {
45+ expect ( typeof validate ) . toEqual ( 'object' ) ;
46+ } ) ;
47+
48+ it ( 'shall have a "validators" property' , ( ) => {
49+ expect ( validate . validators ) . toBeDefined ( ) ;
50+ } ) ;
51+
52+ it ( 'shall have a "check" property' , ( ) => {
53+ expect ( validate . check ) . toBeDefined ( ) ;
54+ } ) ;
55+
56+
57+ describe ( '.validators' , ( ) => {
58+ it ( 'shall be an array' , ( ) => {
59+ expect ( typeof validate . validators ) . toEqual ( 'object' ) ;
60+ expect ( Array . isArray ( validate . validators ) ) . toEqual ( true ) ;
61+ } ) ;
62+
63+ it ( 'shall have some validations by default' , ( ) => {
64+ expect ( validate . validators . length > 0 ) . toEqual ( true ) ;
65+ } ) ;
66+ } ) ;
67+
68+ describe ( '.check' , ( ) => {
69+ it ( 'shall be a function' , ( ) => {
70+ expect ( typeof validate . check ) . toEqual ( 'function' ) ;
71+ } ) ;
72+
73+ it ( 'shall validate valid data' , ( ) => {
74+ validators . clear ( ) ;
75+ validators . add ( ) ;
76+ expect ( validate . check ( validData , { } ) ) . toEqual ( true ) ;
77+ validators . clear ( ) ;
78+ } ) ;
79+
80+ it ( 'shall not validate invalid data' , ( ) => {
81+ validators . clear ( ) ;
82+ validators . add ( ) ;
83+ expect ( validate . check ( invalidData , { } ) ) . toEqual ( true ) ;
84+ validators . clear ( ) ;
85+ } ) ;
86+ } ) ;
87+ } ) ;
0 commit comments