@@ -6,14 +6,33 @@ var runSync = function (scope, tmpl) {
66 directiveScope . render ( schema , form ) ;
77 } ) ;
88 scope . $apply ( ) ;
9- }
9+ } ;
1010
11- describe ( 'directive' , function ( ) {
11+ describe ( 'schema-validate.directive.js' , function ( ) {
12+ var exampleSchema ;
1213 beforeEach ( module ( 'schemaForm' ) ) ;
1314 beforeEach (
1415 module ( function ( $sceProvider ) {
1516 $sceProvider . enabled ( false ) ;
17+ exampleSchema = {
18+ "type" : "object" ,
19+ "title" : "Person" ,
20+ "properties" : {
21+ "name" : {
22+ "title" : "Name" ,
23+ "type" : "string" ,
24+ "minLength" : 10
25+ } ,
26+ "email" : {
27+ "type" : "string" ,
28+ "maxLength" : 255 ,
29+ "format" : "email" ,
30+ "email" : true
31+ }
32+ }
33+ } ;
1634 } )
35+
1736 ) ;
1837
1938 tv4 . defineError ( 'EMAIL' , 10001 , 'Invalid email address' ) ;
@@ -29,98 +48,82 @@ describe('directive', function() {
2948 return null ;
3049 } ) ;
3150
32- exampleSchema = {
33- "type" : "object" ,
34- "title" : "Person" ,
35- "properties" : {
36- "name" : {
37- "title" : "Name" ,
38- "type" : "string" ,
39- "minLength" : 10
40- } ,
41- "email" : {
42- "type" : "string" ,
43- "maxLength" : 255 ,
44- "format" : "email" ,
45- "email" : true
46- }
47- }
48- } ;
4951
5052 it ( 'should validate the form on event [ノಠ益ಠ]ノ彡┻━┻' , function ( ) {
5153
52- tmpl = angular . element (
53- '<div>' +
54- '<form name="testform" sf-schema="schema" sf-form="form" sf-model="obj"></form>' +
55- '<input class="validate" type="button" ng-click="validate_all()" />' +
56- '</div>'
57- ) ;
54+ tmpl = angular . element ( '<form name="testform" sf-schema="schema" sf-form="form" sf-model="obj"></form>' ) ;
5855
5956 inject ( function ( $compile , $rootScope ) {
6057 var scope = $rootScope . $new ( ) ;
61- scope . obj = { "name" : "Json " } ;
58+ scope . obj = { "name" : "Freddy " } ;
6259
6360 scope . schema = exampleSchema ;
6461
65- scope . form = [ "*" ] ;
62+ scope . form = [
63+ "*" ,
64+ {
65+ "type" : "button" ,
66+ "style" : "validate" ,
67+ "onClick" : "validate_all()"
68+ }
69+ ] ;
6670
6771 scope . validate_all = function ( ) {
6872 scope . $broadcast ( 'schemaFormValidate' ) ;
6973 } ;
7074
7175 $compile ( tmpl ) ( scope ) ;
72- tmpl . find ( 'form' ) . each ( function ( ) {
73- runSync ( scope , $ ( this ) ) ;
74- } ) ;
76+ runSync ( scope , tmpl ) ;
7577
76- var form = tmpl . find ( 'form' ) . eq ( 0 ) . controller ( 'form' ) ;
78+ var form = tmpl . eq ( 0 ) . controller ( 'form' ) ;
7779
7880 form . $valid . should . be . true ;
7981 scope . validate_all . should . not . have . beenCalled ;
80- tmpl . find ( 'input .validate' ) . click ( ) ;
82+ tmpl . find ( 'button .validate' ) . click ( ) ;
8183 scope . validate_all . should . have . beenCalledOnce ;
8284 form . $valid . should . be . false ;
83-
8485 } ) ;
8586 } ) ;
8687
8788 it ( 'should process custom tv4 errors' , function ( ) {
8889
89- tmpl = angular . element (
90- '<div>' +
91- '<form name="testform" sf-schema="schema" sf-form="form" sf-model="obj"></form>' +
92- '<input class="validate" type="button" ng-click="validate_all()" />{{obj}}' +
93- '</div>'
94- ) ;
90+ tmpl = angular . element ( '<form name="testform" sf-schema="schema" sf-form="form" sf-model="obj"></form>' ) ;
9591
9692 inject ( function ( $compile , $rootScope ) {
9793 var scope = $rootScope . $new ( ) ;
9894 scope . obj = { "email" : "NULL" } ;
9995
10096 scope . schema = exampleSchema ;
10197
102- scope . form = [ {
103- "key" : "email" ,
104- "placeholder" : "Enter contact email" ,
105- "feedback" : false
106- } ] ;
98+ scope . form = [
99+ {
100+ "key" : "email" ,
101+ "placeholder" : "Enter contact email" ,
102+ "feedback" : false
103+ } ,
104+ {
105+ "type" : "button" ,
106+ "style" : "validate" ,
107+ "onClick" : "validate_all()"
108+ }
109+ ] ;
107110
108111 scope . validate_all = function ( ) {
109112 scope . $broadcast ( 'schemaFormValidate' ) ;
110113 } ;
111114
112115 $compile ( tmpl ) ( scope ) ;
113- tmpl . find ( 'form' ) . each ( function ( ) {
114- runSync ( scope , $ ( this ) ) ;
115- } ) ;
116+ runSync ( scope , tmpl ) ;
116117
117- var form = tmpl . find ( 'form' ) . eq ( 0 ) . controller ( 'form' ) ;
118+ var form = tmpl . eq ( 0 ) . controller ( 'form' ) ;
118119
119120 form . $valid . should . be . true ;
120121 scope . validate_all . should . not . have . beenCalled ;
121- tmpl . find ( 'input.validate' ) . click ( ) ;
122+ angular . element ( tmpl . find ( '#testform-email' ) ) . val ( 'invalid' ) . trigger ( 'input' ) ;
123+ tmpl . find ( 'button.validate' ) . click ( ) ;
122124 scope . validate_all . should . have . beenCalledOnce ;
123125 form . $valid . should . be . false ;
126+ form . $error [ 'tv4-10001' ] . should . be . false ;
124127 } ) ;
125128 } ) ;
126129
0 commit comments