11describe ( '$httpBackend' , function ( ) {
22
33 var $backend , $browser , callbacks ,
4- xhr , fakeBody , callback ;
4+ xhr , fakeDocument , callback ;
55
66 // TODO(vojta): should be replaced by $defer mock
77 function fakeTimeout ( fn , delay ) {
@@ -21,8 +21,24 @@ describe('$httpBackend', function() {
2121 beforeEach ( inject ( function ( $injector ) {
2222 callbacks = { counter : 0 } ;
2323 $browser = $injector . get ( '$browser' ) ;
24- fakeBody = { removeChild : jasmine . createSpy ( 'body.removeChild' ) } ;
25- $backend = createHttpBackend ( $browser , MockXhr , fakeTimeout , callbacks , fakeBody ) ;
24+ fakeDocument = {
25+ $$scripts : [ ] ,
26+ createElement : jasmine . createSpy ( 'createElement' ) . andCallFake ( function ( ) {
27+ return { } ;
28+ } ) ,
29+ body : {
30+ appendChild : jasmine . createSpy ( 'body.appendChid' ) . andCallFake ( function ( script ) {
31+ fakeDocument . $$scripts . push ( script ) ;
32+ } ) ,
33+ removeChild : jasmine . createSpy ( 'body.removeChild' ) . andCallFake ( function ( script ) {
34+ var index = indexOf ( fakeDocument . $$scripts , script ) ;
35+ if ( index != - 1 ) {
36+ fakeDocument . $$scripts . splice ( index , 1 ) ;
37+ }
38+ } )
39+ }
40+ } ;
41+ $backend = createHttpBackend ( $browser , MockXhr , fakeTimeout , callbacks , fakeDocument ) ;
2642 callback = jasmine . createSpy ( 'done' ) ;
2743 } ) ) ;
2844
@@ -131,32 +147,44 @@ describe('$httpBackend', function() {
131147 } ) ;
132148
133149 $backend ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback ) ;
134- expect ( $browser . $$scripts . length ) . toBe ( 1 ) ;
150+ expect ( fakeDocument . $$scripts . length ) . toBe ( 1 ) ;
135151
136- var script = $browser . $$scripts . shift ( ) ,
137- url = script . url . match ( SCRIPT_URL ) ;
152+ var script = fakeDocument . $$scripts . shift ( ) ,
153+ url = script . src . match ( SCRIPT_URL ) ;
138154
139155 expect ( url [ 1 ] ) . toBe ( 'http://example.org/path' ) ;
140156 callbacks [ url [ 2 ] ] ( 'some-data' ) ;
141- script . done ( ) ;
157+
158+ if ( script . onreadystatechange ) {
159+ script . readyState = 'complete' ;
160+ script . onreadystatechange ( ) ;
161+ } else {
162+ script . onload ( )
163+ }
142164
143165 expect ( callback ) . toHaveBeenCalledOnce ( ) ;
144166 } ) ;
145167
146168
147169 it ( 'should clean up the callback and remove the script' , function ( ) {
148170 $backend ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback ) ;
149- expect ( $browser . $$scripts . length ) . toBe ( 1 ) ;
171+ expect ( fakeDocument . $$scripts . length ) . toBe ( 1 ) ;
172+
150173
151- var script = $browser . $$scripts . shift ( ) ,
152- callbackId = script . url . match ( SCRIPT_URL ) [ 2 ] ;
174+ var script = fakeDocument . $$scripts . shift ( ) ,
175+ callbackId = script . src . match ( SCRIPT_URL ) [ 2 ] ;
153176
154177 callbacks [ callbackId ] ( 'some-data' ) ;
155- script . done ( ) ;
178+
179+ if ( script . onreadystatechange ) {
180+ script . readyState = 'complete' ;
181+ script . onreadystatechange ( ) ;
182+ } else {
183+ script . onload ( )
184+ }
156185
157186 expect ( callbacks [ callbackId ] ) . toBeUndefined ( ) ;
158- expect ( fakeBody . removeChild ) . toHaveBeenCalledOnce ( ) ;
159- expect ( fakeBody . removeChild ) . toHaveBeenCalledWith ( script ) ;
187+ expect ( fakeDocument . body . removeChild ) . toHaveBeenCalledOnceWith ( script ) ;
160188 } ) ;
161189
162190
@@ -167,21 +195,26 @@ describe('$httpBackend', function() {
167195 } ) ;
168196
169197 $backend ( 'JSONP' , 'http://example.org/path?cb=JSON_CALLBACK' , null , callback ) ;
170- expect ( $browser . $$scripts . length ) . toBe ( 1 ) ;
171-
172- $browser . $$scripts . shift ( ) . done ( ) ;
198+ expect ( fakeDocument . $$scripts . length ) . toBe ( 1 ) ;
199+
200+ var script = fakeDocument . $$scripts . shift ( ) ;
201+ if ( script . onreadystatechange ) {
202+ script . readyState = 'complete' ;
203+ script . onreadystatechange ( ) ;
204+ } else {
205+ script . onload ( )
206+ }
173207 expect ( callback ) . toHaveBeenCalledOnce ( ) ;
174208 } ) ;
175209
176210
177211 it ( 'should set url to current location if not specified or empty string' , function ( ) {
178212 $backend ( 'JSONP' , undefined , null , callback ) ;
179- expect ( $browser . $$scripts [ 0 ] . url ) . toBe ( $browser . url ( ) ) ;
180- $browser . $$scripts . shift ( ) ;
213+ expect ( fakeDocument . $$scripts [ 0 ] . src ) . toBe ( $browser . url ( ) ) ;
214+ fakeDocument . $$scripts . shift ( ) ;
181215
182216 $backend ( 'JSONP' , '' , null , callback ) ;
183- expect ( $browser . $$scripts [ 0 ] . url ) . toBe ( $browser . url ( ) ) ;
184- $browser . $$scripts . shift ( ) ;
217+ expect ( fakeDocument . $$scripts [ 0 ] . src ) . toBe ( $browser . url ( ) ) ;
185218 } ) ;
186219
187220
0 commit comments