@@ -22,16 +22,16 @@ describe('$timeout', function() {
22
22
it ( 'should call $apply after each callback is executed' , inject ( function ( $timeout , $rootScope ) {
23
23
var applySpy = spyOn ( $rootScope , '$apply' ) . andCallThrough ( ) ;
24
24
25
- $timeout ( function ( ) { } ) ;
25
+ $timeout ( noop ) ;
26
26
expect ( applySpy ) . not . toHaveBeenCalled ( ) ;
27
27
28
28
$timeout . flush ( ) ;
29
29
expect ( applySpy ) . toHaveBeenCalledOnce ( ) ;
30
30
31
31
applySpy . reset ( ) ;
32
32
33
- $timeout ( function ( ) { } ) ;
34
- $timeout ( function ( ) { } ) ;
33
+ $timeout ( noop ) ;
34
+ $timeout ( noop ) ;
35
35
$timeout . flush ( ) ;
36
36
expect ( applySpy . callCount ) . toBe ( 2 ) ;
37
37
} ) ) ;
@@ -40,7 +40,7 @@ describe('$timeout', function() {
40
40
it ( 'should NOT call $apply if skipApply is set to true' , inject ( function ( $timeout , $rootScope ) {
41
41
var applySpy = spyOn ( $rootScope , '$apply' ) . andCallThrough ( ) ;
42
42
43
- $timeout ( function ( ) { } , 12 , false ) ;
43
+ $timeout ( noop , 12 , false ) ;
44
44
expect ( applySpy ) . not . toHaveBeenCalled ( ) ;
45
45
46
46
$timeout . flush ( ) ;
@@ -89,8 +89,8 @@ describe('$timeout', function() {
89
89
// $browser.defer.cancel is only called on cancel if the deferred object is still referenced
90
90
var cancelSpy = spyOn ( $browser . defer , 'cancel' ) . andCallThrough ( ) ;
91
91
92
- var promise1 = $timeout ( function ( ) { } , 0 , false ) ;
93
- var promise2 = $timeout ( function ( ) { } , 100 , false ) ;
92
+ var promise1 = $timeout ( noop , 0 , false ) ;
93
+ var promise2 = $timeout ( noop , 100 , false ) ;
94
94
expect ( cancelSpy ) . not . toHaveBeenCalled ( ) ;
95
95
96
96
$timeout . flush ( 0 ) ;
@@ -104,7 +104,6 @@ describe('$timeout', function() {
104
104
expect ( cancelSpy ) . toHaveBeenCalled ( ) ;
105
105
} ) ) ;
106
106
107
-
108
107
it ( 'should allow the `fn` parameter to be optional' , inject ( function ( $timeout , log ) {
109
108
110
109
$timeout ( ) . then ( function ( value ) { log ( 'promise success: ' + value ) ; } , log . fn ( 'promise error' ) ) ;
@@ -123,6 +122,35 @@ describe('$timeout', function() {
123
122
expect ( log ) . toEqual ( [ 'promise success: undefined' ] ) ;
124
123
} ) ) ;
125
124
125
+ it ( 'should pass the timeout arguments in the timeout callback' ,
126
+ inject ( function ( $timeout , $browser , log ) {
127
+ var task1 = jasmine . createSpy ( 'Nappa' ) ,
128
+ task2 = jasmine . createSpy ( 'Vegeta' ) ;
129
+
130
+ $timeout ( task1 , 9000 , true , 'What does' , 'the timeout' , 'say about' , 'its delay level' ) ;
131
+ expect ( $browser . deferredFns . length ) . toBe ( 1 ) ;
132
+
133
+ $timeout ( task2 , 9001 , false , 'It\'s' , 'over' , 9000 ) ;
134
+ expect ( $browser . deferredFns . length ) . toBe ( 2 ) ;
135
+
136
+ $timeout ( 9000 , false , 'What!' , 9000 ) . then ( function ( value ) { log ( 'There\'s no way that can be right! ' + value ) ; } , log . fn ( 'It can\'t!' ) ) ;
137
+ expect ( $browser . deferredFns . length ) . toBe ( 3 ) ;
138
+ expect ( log ) . toEqual ( [ ] ) ;
139
+
140
+ $timeout . flush ( 0 ) ;
141
+ expect ( task1 ) . not . toHaveBeenCalled ( ) ;
142
+
143
+ $timeout . flush ( 9000 ) ;
144
+ expect ( task1 ) . toHaveBeenCalledWith ( 'What does' , 'the timeout' , 'say about' , 'its delay level' ) ;
145
+
146
+ $timeout . flush ( 1 ) ;
147
+ expect ( task2 ) . toHaveBeenCalledWith ( 'It\'s' , 'over' , 9000 ) ;
148
+
149
+ $timeout . flush ( 9000 ) ;
150
+ expect ( log ) . toEqual ( [ 'There\'s no way that can be right! undefined' ] ) ;
151
+
152
+ } ) ) ;
153
+
126
154
127
155
describe ( 'exception handling' , function ( ) {
128
156
@@ -133,7 +161,7 @@ describe('$timeout', function() {
133
161
134
162
it ( 'should delegate exception to the $exceptionHandler service' , inject (
135
163
function ( $timeout , $exceptionHandler ) {
136
- $timeout ( function ( ) { throw "Test Error" ; } ) ;
164
+ $timeout ( function ( ) { throw "Test Error" ; } ) ;
137
165
expect ( $exceptionHandler . errors ) . toEqual ( [ ] ) ;
138
166
139
167
$timeout . flush ( ) ;
@@ -145,7 +173,7 @@ describe('$timeout', function() {
145
173
function ( $timeout , $rootScope ) {
146
174
var applySpy = spyOn ( $rootScope , '$apply' ) . andCallThrough ( ) ;
147
175
148
- $timeout ( function ( ) { throw "Test Error" ; } ) ;
176
+ $timeout ( function ( ) { throw "Test Error" ; } ) ;
149
177
expect ( applySpy ) . not . toHaveBeenCalled ( ) ;
150
178
151
179
$timeout . flush ( ) ;
@@ -164,6 +192,25 @@ describe('$timeout', function() {
164
192
} ) ) ;
165
193
166
194
195
+ it ( 'should pass the timeout arguments in the timeout callback even if an exception is thrown' ,
196
+ inject ( function ( $timeout , log ) {
197
+ var promise1 = $timeout ( function ( arg ) { throw arg ; } , 9000 , true , 'Some Arguments' ) ;
198
+ var promise2 = $timeout ( function ( arg1 , args2 ) { throw arg1 + ' ' + args2 ; } , 9001 , false , 'Are Meant' , 'To Be Thrown' ) ;
199
+
200
+ promise1 . then ( log . fn ( 'success' ) , function ( reason ) { log ( 'error: ' + reason ) ; } ) ;
201
+ promise2 . then ( log . fn ( 'success' ) , function ( reason ) { log ( 'error: ' + reason ) ; } ) ;
202
+
203
+ $timeout . flush ( 0 ) ;
204
+ expect ( log ) . toEqual ( '' ) ;
205
+
206
+ $timeout . flush ( 9000 ) ;
207
+ expect ( log ) . toEqual ( 'error: Some Arguments' ) ;
208
+
209
+ $timeout . flush ( 1 ) ;
210
+ expect ( log ) . toEqual ( 'error: Some Arguments; error: Are Meant To Be Thrown' ) ;
211
+ } ) ) ;
212
+
213
+
167
214
it ( 'should forget references to relevant deferred even when exception is thrown' ,
168
215
inject ( function ( $timeout , $browser ) {
169
216
// $browser.defer.cancel is only called on cancel if the deferred object is still referenced
@@ -242,7 +289,7 @@ describe('$timeout', function() {
242
289
// $browser.defer.cancel is only called on cancel if the deferred object is still referenced
243
290
var cancelSpy = spyOn ( $browser . defer , 'cancel' ) . andCallThrough ( ) ;
244
291
245
- var promise = $timeout ( function ( ) { } , 0 , false ) ;
292
+ var promise = $timeout ( noop , 0 , false ) ;
246
293
247
294
expect ( cancelSpy ) . not . toHaveBeenCalled ( ) ;
248
295
$timeout . cancel ( promise ) ;
0 commit comments