@@ -40,10 +40,13 @@ describe('$anchorScroll', function() {
40
40
}
41
41
42
42
function changeHashTo ( hash ) {
43
- return function ( $anchorScroll , $location , $rootScope ) {
43
+ return function ( $anchorScroll , $browser , $ location, $rootScope ) {
44
44
$rootScope . $apply ( function ( ) {
45
45
$location . hash ( hash ) ;
46
46
} ) ;
47
+ if ( $browser . deferredFns . length ) {
48
+ $browser . defer . flush ( ) ;
49
+ }
47
50
} ;
48
51
}
49
52
@@ -81,19 +84,12 @@ describe('$anchorScroll', function() {
81
84
}
82
85
83
86
function mockDocumentReadyState ( ) {
84
- var docMock = {
85
- readyState : 'mocked' ,
86
- updateReadyState : function ( newState ) {
87
- this . readyState = newState ;
88
- this . dispatchFakeReadyStateChangeEvent ( ) ;
89
- }
90
- } ;
91
-
87
+ var docMock = { } ;
92
88
docSpies = { } ;
93
89
94
- return function ( $rootScope , $window ) {
95
- var propsToMock = [ 'body' , 'documentElement' ] ;
96
- var methodsToMock = [
90
+ return function ( $browser , $ rootScope, $window ) {
91
+ var propsToPassThrough = [ 'body' , 'documentElement' ] ;
92
+ var methodsToPassThrough = [
97
93
'getElementById' ,
98
94
'getElementsByName' ,
99
95
'addEventListener' ,
@@ -102,19 +98,26 @@ describe('$anchorScroll', function() {
102
98
103
99
var document_ = $window . document ;
104
100
105
- propsToMock . forEach ( function ( prop ) {
101
+ propsToPassThrough . forEach ( function ( prop ) {
106
102
docMock [ prop ] = document_ [ prop ] ;
107
103
} ) ;
108
- methodsToMock . forEach ( function ( method ) {
104
+ methodsToPassThrough . forEach ( function ( method ) {
109
105
docMock [ method ] = document_ [ method ] . bind ( document_ ) ;
110
106
docSpies [ method ] = spyOn ( docMock , method ) . andCallThrough ( ) ;
111
107
} ) ;
112
108
109
+ docMock . readyState = 'mocked' ;
113
110
docMock . dispatchFakeReadyStateChangeEvent = function ( ) {
114
111
var evt = document . createEvent ( 'Event' ) ;
115
112
evt . initEvent ( 'readystatechange' , false , false ) ;
116
113
document_ . dispatchEvent ( evt ) ;
117
- $rootScope . $digest ( ) ;
114
+ } ;
115
+ docMock . updateReadyState = function ( newState ) {
116
+ this . readyState = newState ;
117
+ this . dispatchFakeReadyStateChangeEvent ( ) ;
118
+ if ( $browser . deferredFns . length ) {
119
+ $browser . defer . flush ( ) ;
120
+ }
118
121
} ;
119
122
120
123
$window . document = docMock ;
@@ -218,7 +221,7 @@ describe('$anchorScroll', function() {
218
221
expectScrollingTo ( 'id=some1' ) ) ) ;
219
222
220
223
221
- it ( 'should only register one execution for when `document.readyState === "complete"' , inject (
224
+ it ( 'should only register once for execution when `document.readyState === "complete"' , inject (
222
225
mockDocumentReadyState ( ) ,
223
226
addElements ( 'id=some1' , 'id=some2' ) ,
224
227
@@ -259,6 +262,20 @@ describe('$anchorScroll', function() {
259
262
} ) ) ;
260
263
261
264
265
+ it ( 'should scroll immediately if `readyState === "complete"`' , inject (
266
+ mockDocumentReadyState ( ) ,
267
+ addElements ( 'id=some1' ) ,
268
+
269
+ updateMockReadyState ( 'complete' ) ,
270
+ changeHashTo ( 'some1' ) ,
271
+
272
+ expectScrollingTo ( 'id=some1' ) ,
273
+ function ( ) {
274
+ expect ( docSpies . addEventListener . callCount ) . toBe ( 0 ) ;
275
+ expect ( docSpies . removeEventListener . callCount ) . toBe ( 0 ) ;
276
+ } ) ) ;
277
+
278
+
262
279
describe ( 'watcher' , function ( ) {
263
280
264
281
function initLocation ( config ) {
@@ -356,7 +373,7 @@ describe('$anchorScroll', function() {
356
373
var list = isArray ( offsetList ) ? offsetList : [ offsetList ] ;
357
374
358
375
return function ( $window ) {
359
- expectScrollingTo ( identifierCountMap ) ( $window ) ;
376
+ inject ( expectScrollingTo ( identifierCountMap ) ) ;
360
377
expect ( $window . scrollBy . callCount ) . toBe ( list . length ) ;
361
378
forEach ( list , function ( offset , idx ) {
362
379
// Due to sub-pixel rendering, there is a +/-1 error margin in the actual offset
0 commit comments