@@ -81,3 +81,58 @@ ValidatorTest.prototype.testJson = function() {
8181 assertNotNull ( angular . validator . json ( "''X" ) ) ;
8282 assertNull ( angular . validator . json ( "{}" ) ) ;
8383} ;
84+
85+ describe ( 'Validator:asynchronous' , function ( ) {
86+ var asynchronous = angular . validator . asynchronous ;
87+ var self ;
88+ var value , fn ;
89+
90+ beforeEach ( function ( ) {
91+ value = null ;
92+ fn = null ;
93+ self = {
94+ $element :$ ( '<input />' ) [ 0 ] ,
95+ $invalidWidgets :[ ] ,
96+ $updateView : noop
97+ } ;
98+ } ) ;
99+
100+ it ( 'should make a request and show spinner' , function ( ) {
101+ var x = compile ( '<input name="name" ng-validate="asynchronous:asyncFn"/>' )
102+ var asyncFn = function ( v , f ) { value = v ; fn = f } ;
103+ var input = x . node . find ( ":input" ) ;
104+ x . scope . set ( "asyncFn" , asyncFn ) ;
105+ x . scope . set ( "name" , "misko" ) ;
106+ x . binder . updateView ( ) ;
107+ expect ( value ) . toEqual ( 'misko' ) ;
108+ expect ( input . hasClass ( 'ng-input-indicator-wait' ) ) . toBeTruthy ( ) ;
109+ fn ( "myError" ) ;
110+ expect ( input . hasClass ( 'ng-input-indicator-wait' ) ) . toBeFalsy ( ) ;
111+ expect ( input . attr ( 'ng-error' ) ) . toEqual ( "myError" ) ;
112+ } ) ;
113+
114+ it ( "should not make second request to same value" , function ( ) {
115+ asynchronous . call ( self , "kai" , function ( v , f ) { value = v ; fn = f ; } ) ;
116+ expect ( value ) . toEqual ( 'kai' ) ;
117+ expect ( self . $invalidWidgets ) . toEqual ( [ self . $element ] ) ;
118+
119+ var spy = jasmine . createSpy ( ) ;
120+ asynchronous . call ( self , "kai" , spy ) ;
121+ expect ( spy ) . wasNotCalled ( ) ;
122+
123+ asynchronous . call ( self , "misko" , spy ) ;
124+ expect ( spy ) . wasCalled ( ) ;
125+ } ) ;
126+
127+ it ( "should ignore old callbacks, and not remove spinner" , function ( ) {
128+ var firstCb , secondCb ;
129+ asynchronous . call ( self , "first" , function ( v , f ) { value = v ; firstCb = f ; } ) ;
130+ asynchronous . call ( self , "second" , function ( v , f ) { value = v ; secondCb = f ; } ) ;
131+
132+ firstCb ( ) ;
133+ expect ( $ ( self . $element ) . hasClass ( 'ng-input-indicator-wait' ) ) . toBeTruthy ( ) ;
134+
135+ secondCb ( ) ;
136+ expect ( $ ( self . $element ) . hasClass ( 'ng-input-indicator-wait' ) ) . toBeFalsy ( ) ;
137+ } ) ;
138+ } ) ;
0 commit comments