@@ -23,7 +23,6 @@ describe('$anchorScroll', function() {
2323 } ;
2424 }
2525
26-
2726 function addElements ( ) {
2827 var elements = sliceArgs ( arguments ) ;
2928
@@ -49,9 +48,9 @@ describe('$anchorScroll', function() {
4948 } ;
5049 }
5150
52- function callAnchorScroll ( ) {
51+ function callAnchorScroll ( hash ) {
5352 return function ( $anchorScroll ) {
54- $anchorScroll ( ) ;
53+ $anchorScroll ( hash ) ;
5554 } ;
5655 }
5756
@@ -141,50 +140,120 @@ describe('$anchorScroll', function() {
141140 beforeEach ( createMockWindow ( ) ) ;
142141
143142
144- it ( 'should scroll to top of the window if empty hash' , inject (
145- changeHashAndScroll ( '' ) ,
146- expectScrollingToTop ) ) ;
143+ describe ( 'and implicitly using `$location.hash()`' , function ( ) {
144+
145+ it ( 'should scroll to top of the window if empty hash' , inject (
146+ changeHashAndScroll ( '' ) ,
147+ expectScrollingToTop ) ) ;
148+
149+
150+ it ( 'should not scroll if hash does not match any element' , inject (
151+ addElements ( 'id=one' , 'id=two' ) ,
152+ changeHashAndScroll ( 'non-existing' ) ,
153+ expectNoScrolling ( ) ) ) ;
154+
155+
156+ it ( 'should scroll to anchor element with name' , inject (
157+ addElements ( 'a name=abc' ) ,
158+ changeHashAndScroll ( 'abc' ) ,
159+ expectScrollingTo ( 'a name=abc' ) ) ) ;
160+
161+
162+ it ( 'should not scroll to other than anchor element with name' , inject (
163+ addElements ( 'input name=xxl' , 'select name=xxl' , 'form name=xxl' ) ,
164+ changeHashAndScroll ( 'xxl' ) ,
165+ expectNoScrolling ( ) ) ) ;
166+
167+
168+ it ( 'should scroll to anchor even if other element with given name exist' , inject (
169+ addElements ( 'input name=some' , 'a name=some' ) ,
170+ changeHashAndScroll ( 'some' ) ,
171+ expectScrollingTo ( 'a name=some' ) ) ) ;
172+
173+
174+ it ( 'should scroll to element with id with precedence over name' , inject (
175+ addElements ( 'name=abc' , 'id=abc' ) ,
176+ changeHashAndScroll ( 'abc' ) ,
177+ expectScrollingTo ( 'id=abc' ) ) ) ;
147178
148179
149- it ( 'should not scroll if hash does not match any element' , inject (
150- addElements ( 'id=one' , 'id=two' ) ,
151- changeHashAndScroll ( 'non-existing' ) ,
152- expectNoScrolling ( ) ) ) ;
180+ it ( 'should scroll to top if hash == "top" and no matching element' , inject (
181+ changeHashAndScroll ( 'top' ) ,
182+ expectScrollingToTop ) ) ;
153183
154184
155- it ( 'should scroll to anchor element with name' , inject (
156- addElements ( 'a name=abc' ) ,
157- changeHashAndScroll ( 'abc' ) ,
158- expectScrollingTo ( 'a name=abc' ) ) ) ;
185+ it ( 'should scroll to element with id "top" if present' , inject (
186+ addElements ( 'id=top' ) ,
187+ changeHashAndScroll ( 'top' ) ,
188+ expectScrollingTo ( 'id=top' ) ) ) ;
189+ } ) ;
190+
191+
192+ describe ( 'and specifying a hash' , function ( ) {
193+
194+ it ( 'should ignore the `hash` argument if not a string' , inject (
195+ spyOnJQLiteDocumentLoaded ( ) ,
196+ addElements ( 'id=one' , 'id=two' ) ,
197+ changeHashTo ( 'one' ) , // won't scroll since `jqLiteDocumentLoaded()` is spied upon
198+ callAnchorScroll ( { } ) ,
199+ expectScrollingTo ( 'id=one' ) ,
200+ unspyOnJQLiteDocumentLoaded ( ) ) ) ;
201+
202+
203+ it ( 'should ignore `$location.hash()` if `hash` is passed as argument' , inject (
204+ spyOnJQLiteDocumentLoaded ( ) ,
205+ addElements ( 'id=one' , 'id=two' ) ,
206+ changeHashTo ( 'one' ) , // won't scroll since `jqLiteDocumentLoaded()` is spied upon
207+ callAnchorScroll ( 'two' ) ,
208+ expectScrollingTo ( 'id=two' ) ,
209+ unspyOnJQLiteDocumentLoaded ( ) ) ) ;
210+
159211
212+ it ( 'should scroll to top of the window if empty hash' , inject (
213+ callAnchorScroll ( '' ) ,
214+ expectScrollingToTop ) ) ;
160215
161- it ( 'should not scroll to other than anchor element with name' , inject (
162- addElements ( 'input name=xxl' , 'select name=xxl' , 'form name=xxl' ) ,
163- changeHashAndScroll ( 'xxl' ) ,
164- expectNoScrolling ( ) ) ) ;
165216
217+ it ( 'should not scroll if hash does not match any element' , inject (
218+ addElements ( 'id=one' , 'id=two' ) ,
219+ callAnchorScroll ( 'non-existing' ) ,
220+ expectNoScrolling ( ) ) ) ;
166221
167- it ( 'should scroll to anchor even if other element with given name exist' , inject (
168- addElements ( 'input name=some' , 'a name=some' ) ,
169- changeHashAndScroll ( 'some' ) ,
170- expectScrollingTo ( 'a name=some' ) ) ) ;
171222
223+ it ( 'should scroll to anchor element with name' , inject (
224+ addElements ( 'a name=abc' ) ,
225+ callAnchorScroll ( 'abc' ) ,
226+ expectScrollingTo ( 'a name=abc' ) ) ) ;
172227
173- it ( 'should scroll to element with id with precedence over name' , inject (
174- addElements ( 'name=abc' , 'id=abc' ) ,
175- changeHashAndScroll ( 'abc' ) ,
176- expectScrollingTo ( 'id=abc' ) ) ) ;
177228
229+ it ( 'should not scroll to other than anchor element with name' , inject (
230+ addElements ( 'input name=xxl' , 'select name=xxl' , 'form name=xxl' ) ,
231+ callAnchorScroll ( 'xxl' ) ,
232+ expectNoScrolling ( ) ) ) ;
178233
179- it ( 'should scroll to top if hash == "top" and no matching element' , inject (
180- changeHashAndScroll ( 'top' ) ,
181- expectScrollingToTop ) ) ;
182234
235+ it ( 'should scroll to anchor even if other element with given name exist' , inject (
236+ addElements ( 'input name=some' , 'a name=some' ) ,
237+ callAnchorScroll ( 'some' ) ,
238+ expectScrollingTo ( 'a name=some' ) ) ) ;
183239
184- it ( 'should scroll to element with id "top" if present' , inject (
185- addElements ( 'id=top' ) ,
186- changeHashAndScroll ( 'top' ) ,
187- expectScrollingTo ( 'id=top' ) ) ) ;
240+
241+ it ( 'should scroll to element with id with precedence over name' , inject (
242+ addElements ( 'name=abc' , 'id=abc' ) ,
243+ callAnchorScroll ( 'abc' ) ,
244+ expectScrollingTo ( 'id=abc' ) ) ) ;
245+
246+
247+ it ( 'should scroll to top if hash == "top" and no matching element' , inject (
248+ callAnchorScroll ( 'top' ) ,
249+ expectScrollingToTop ) ) ;
250+
251+
252+ it ( 'should scroll to element with id "top" if present' , inject (
253+ addElements ( 'id=top' ) ,
254+ callAnchorScroll ( 'top' ) ,
255+ expectScrollingTo ( 'id=top' ) ) ) ;
256+ } ) ;
188257 } ) ;
189258
190259
0 commit comments