@@ -293,7 +293,7 @@ describe('$http', function() {
293
293
$httpBackend = $hb ;
294
294
$http = $h ;
295
295
$rootScope = $rs ;
296
- spyOn ( $rootScope , '$apply ' ) . andCallThrough ( ) ;
296
+ spyOn ( $rootScope , '$digest ' ) . andCallThrough ( ) ;
297
297
} ] ) ) ;
298
298
299
299
it ( 'should throw error if the request configuration is not an object' , function ( ) {
@@ -1012,37 +1012,6 @@ describe('$http', function() {
1012
1012
} ) ;
1013
1013
1014
1014
1015
- describe ( 'scope.$apply' , function ( ) {
1016
-
1017
- it ( 'should $apply after success callback' , function ( ) {
1018
- $httpBackend . when ( 'GET' ) . respond ( 200 ) ;
1019
- $http ( { method : 'GET' , url : '/some' } ) ;
1020
- $httpBackend . flush ( ) ;
1021
- expect ( $rootScope . $apply ) . toHaveBeenCalledOnce ( ) ;
1022
- } ) ;
1023
-
1024
-
1025
- it ( 'should $apply after error callback' , function ( ) {
1026
- $httpBackend . when ( 'GET' ) . respond ( 404 ) ;
1027
- $http ( { method : 'GET' , url : '/some' } ) ;
1028
- $httpBackend . flush ( ) ;
1029
- expect ( $rootScope . $apply ) . toHaveBeenCalledOnce ( ) ;
1030
- } ) ;
1031
-
1032
-
1033
- it ( 'should $apply even if exception thrown during callback' , inject ( function ( $exceptionHandler ) {
1034
- $httpBackend . when ( 'GET' ) . respond ( 200 ) ;
1035
- callback . andThrow ( 'error in callback' ) ;
1036
-
1037
- $http ( { method : 'GET' , url : '/some' } ) . then ( callback ) ;
1038
- $httpBackend . flush ( ) ;
1039
- expect ( $rootScope . $apply ) . toHaveBeenCalledOnce ( ) ;
1040
-
1041
- $exceptionHandler . errors = [ ] ;
1042
- } ) ) ;
1043
- } ) ;
1044
-
1045
-
1046
1015
describe ( 'transformData' , function ( ) {
1047
1016
1048
1017
describe ( 'request' , function ( ) {
@@ -1900,35 +1869,27 @@ describe('$http', function() {
1900
1869
} ) ;
1901
1870
1902
1871
1903
- describe ( '$http with $applyAsync' , function ( ) {
1904
- var $http , $httpBackend , $rootScope , $browser , log ;
1905
- beforeEach ( module ( function ( $httpProvider ) {
1906
- $httpProvider . useApplyAsync ( true ) ;
1907
- } , provideLog ) ) ;
1908
-
1872
+ describe ( '$http interacting with digest cycle' , function ( ) {
1873
+ var $http , $httpBackend , $browser , log ;
1874
+ beforeEach ( module ( provideLog ) ) ;
1909
1875
1910
- beforeEach ( inject ( [ '$http' , '$httpBackend' , '$rootScope' , '$ browser', 'log' , function ( http , backend , scope , browser , logger ) {
1876
+ beforeEach ( inject ( [ '$http' , '$httpBackend' , '$browser' , 'log' , function ( http , backend , browser , logger ) {
1911
1877
$http = http ;
1912
1878
$httpBackend = backend ;
1913
- $rootScope = scope ;
1914
1879
$browser = browser ;
1915
- spyOn ( $rootScope , '$apply' ) . andCallThrough ( ) ;
1916
- spyOn ( $rootScope , '$applyAsync' ) . andCallThrough ( ) ;
1917
- spyOn ( $rootScope , '$digest' ) . andCallThrough ( ) ;
1918
- spyOn ( $browser . defer , 'cancel' ) . andCallThrough ( ) ;
1919
1880
log = logger ;
1920
1881
} ] ) ) ;
1921
1882
1922
1883
1923
- it ( 'should schedule coalesced apply on response ' , function ( ) {
1884
+ it ( 'should execute callbacks asynchronously in $digest ' , function ( ) {
1924
1885
var handler = jasmine . createSpy ( 'handler' ) ;
1925
1886
$httpBackend . expect ( 'GET' , '/template1.html' ) . respond ( 200 , '<h1>Header!</h1>' , { } ) ;
1926
1887
$http . get ( '/template1.html' ) . then ( handler ) ;
1927
- // Ensure requests are sent
1928
- $rootScope . $digest ( ) ;
1888
+ // Ensure requests are sent. ($http is internally promise-based and doesn't start working until
1889
+ // $digest occurs.)
1890
+ $browser . defer . flush ( ) ;
1929
1891
1930
1892
$httpBackend . flush ( null , false ) ;
1931
- expect ( $rootScope . $applyAsync ) . toHaveBeenCalledOnce ( ) ;
1932
1893
expect ( handler ) . not . toHaveBeenCalled ( ) ;
1933
1894
1934
1895
$browser . defer . flush ( ) ;
@@ -1943,11 +1904,14 @@ describe('$http with $applyAsync', function() {
1943
1904
$http . get ( '/template1.html' ) . then ( log . fn ( 'response 1' ) ) ;
1944
1905
$http . get ( '/template2.html' ) . then ( log . fn ( 'response 2' ) ) ;
1945
1906
// Ensure requests are sent
1946
- $rootScope . $digest ( ) ;
1907
+ $browser . defer . flush ( ) ;
1947
1908
1909
+ // Resolve the promises. When a $q promise is resolved it uses $rootScope.$evalAsync to schedule
1910
+ // the execution of its callbacks.
1948
1911
$httpBackend . flush ( null , false ) ;
1949
1912
expect ( log ) . toEqual ( [ ] ) ;
1950
1913
1914
+ // Execute the promises' callbacks in the $digest scheduled with $evalAsync
1951
1915
$browser . defer . flush ( ) ;
1952
1916
expect ( log ) . toEqual ( [ 'response 1' , 'response 2' ] ) ;
1953
1917
} ) ;
@@ -1961,16 +1925,16 @@ describe('$http with $applyAsync', function() {
1961
1925
$http . get ( '/template1.html' ) . then ( log . fn ( 'response 1' ) ) ;
1962
1926
$http . get ( '/template2.html' ) . then ( log . fn ( 'response 2' ) ) ;
1963
1927
$http . get ( '/template3.html' ) . then ( log . fn ( 'response 3' ) ) ;
1964
- // Ensure requests are sent
1965
- $rootScope . $digest ( ) ;
1966
1928
1967
1929
// Intermediate $digest occurs before 3rd response is received, assert that pending responses
1968
- /// are handled
1930
+ // are handled. Unless false is passed as the second parameter, $httpBackend.flush calls
1931
+ // $rootScope.$digest at least twice (before and after doing the flush).
1969
1932
$httpBackend . flush ( 2 ) ;
1970
1933
expect ( log ) . toEqual ( [ 'response 1' , 'response 2' ] ) ;
1971
1934
1972
- // Finally, third response is received, and a second coalesced $apply is started
1935
+ // Finally, third response is received, and its callback is scheduled with $evalAsync
1973
1936
$httpBackend . flush ( null , false ) ;
1937
+ // Execute the promises' callbacks in the $digest scheduled with $evalAsync
1974
1938
$browser . defer . flush ( ) ;
1975
1939
expect ( log ) . toEqual ( [ 'response 1' , 'response 2' , 'response 3' ] ) ;
1976
1940
} ) ;
0 commit comments