4
4
describe ( '$httpBackend' , function ( ) {
5
5
6
6
var $backend , $browser , callbacks ,
7
- xhr , fakeDocument , callback ,
8
- fakeTimeoutId = 0 ;
9
-
10
- // TODO(vojta): should be replaced by $defer mock
11
- function fakeTimeout ( fn , delay ) {
12
- fakeTimeout . fns . push ( fn ) ;
13
- fakeTimeout . delays . push ( delay ) ;
14
- fakeTimeout . ids . push ( ++ fakeTimeoutId ) ;
15
- return fakeTimeoutId ;
16
- }
17
-
18
- fakeTimeout . fns = [ ] ;
19
- fakeTimeout . delays = [ ] ;
20
- fakeTimeout . ids = [ ] ;
21
- fakeTimeout . flush = function ( ) {
22
- var len = fakeTimeout . fns . length ;
23
- fakeTimeout . delays = [ ] ;
24
- fakeTimeout . ids = [ ] ;
25
- while ( len -- ) fakeTimeout . fns . shift ( ) ( ) ;
26
- } ;
27
- fakeTimeout . cancel = function ( id ) {
28
- var i = fakeTimeout . ids . indexOf ( id ) ;
29
- if ( i >= 0 ) {
30
- fakeTimeout . fns . splice ( i , 1 ) ;
31
- fakeTimeout . delays . splice ( i , 1 ) ;
32
- fakeTimeout . ids . splice ( i , 1 ) ;
33
- return true ;
34
- }
35
- return false ;
36
- } ;
7
+ xhr , fakeDocument , callback ;
37
8
38
9
39
10
beforeEach ( inject ( function ( $injector ) {
@@ -57,7 +28,7 @@ describe('$httpBackend', function() {
57
28
} )
58
29
}
59
30
} ;
60
- $backend = createHttpBackend ( $browser , createMockXhr , fakeTimeout , callbacks , fakeDocument ) ;
31
+ $backend = createHttpBackend ( $browser , createMockXhr , $browser . defer , callbacks , fakeDocument ) ;
61
32
callback = jasmine . createSpy ( 'done' ) ;
62
33
} ) ) ;
63
34
@@ -154,7 +125,7 @@ describe('$httpBackend', function() {
154
125
xhr = MockXhr . $$lastInstance ;
155
126
spyOn ( xhr , 'abort' ) ;
156
127
157
- fakeTimeout . flush ( ) ;
128
+ $browser . defer . flush ( ) ;
158
129
expect ( xhr . abort ) . toHaveBeenCalledOnce ( ) ;
159
130
160
131
xhr . status = 0 ;
@@ -171,9 +142,9 @@ describe('$httpBackend', function() {
171
142
xhr = MockXhr . $$lastInstance ;
172
143
spyOn ( xhr , 'abort' ) ;
173
144
174
- expect ( fakeTimeout . delays [ 0 ] ) . toBe ( 2000 ) ;
145
+ expect ( $browser . deferredFns [ 0 ] . time ) . toBe ( 2000 ) ;
175
146
176
- fakeTimeout . flush ( ) ;
147
+ $browser . defer . flush ( ) ;
177
148
expect ( xhr . abort ) . toHaveBeenCalledOnce ( ) ;
178
149
179
150
xhr . status = 0 ;
@@ -227,13 +198,13 @@ describe('$httpBackend', function() {
227
198
xhr = MockXhr . $$lastInstance ;
228
199
spyOn ( xhr , 'abort' ) ;
229
200
230
- expect ( fakeTimeout . delays [ 0 ] ) . toBe ( 2000 ) ;
201
+ expect ( $browser . deferredFns [ 0 ] . time ) . toBe ( 2000 ) ;
231
202
232
203
xhr . status = 200 ;
233
204
xhr . onload ( ) ;
234
205
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
235
206
236
- expect ( fakeTimeout . delays . length ) . toBe ( 0 ) ;
207
+ expect ( $browser . deferredFns . length ) . toBe ( 0 ) ;
237
208
expect ( xhr . abort ) . not . toHaveBeenCalled ( ) ;
238
209
} ) ;
239
210
@@ -342,12 +313,12 @@ describe('$httpBackend', function() {
342
313
343
314
$backend ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback , null , 2000 ) ;
344
315
expect ( fakeDocument . $$scripts . length ) . toBe ( 1 ) ;
345
- expect ( fakeTimeout . delays [ 0 ] ) . toBe ( 2000 ) ;
316
+ expect ( $browser . deferredFns [ 0 ] . time ) . toBe ( 2000 ) ;
346
317
347
318
var script = fakeDocument . $$scripts . shift ( ) ,
348
319
callbackId = script . src . match ( SCRIPT_URL ) [ 2 ] ;
349
320
350
- fakeTimeout . flush ( ) ;
321
+ $browser . defer . flush ( ) ;
351
322
expect ( fakeDocument . $$scripts . length ) . toBe ( 0 ) ;
352
323
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
353
324
0 commit comments