@@ -23,7 +23,6 @@ describe('$anchorScroll', function() {
23
23
} ;
24
24
}
25
25
26
-
27
26
function addElements ( ) {
28
27
var elements = sliceArgs ( arguments ) ;
29
28
@@ -49,9 +48,10 @@ describe('$anchorScroll', function() {
49
48
} ;
50
49
}
51
50
52
- function callAnchorScroll ( ) {
51
+ function callAnchorScroll ( hash ) {
52
+ var args = arguments ;
53
53
return function ( $anchorScroll ) {
54
- $anchorScroll ( ) ;
54
+ $anchorScroll . apply ( null , args ) ;
55
55
} ;
56
56
}
57
57
@@ -141,50 +141,120 @@ describe('$anchorScroll', function() {
141
141
beforeEach ( createMockWindow ( ) ) ;
142
142
143
143
144
- it ( 'should scroll to top of the window if empty hash' , inject (
145
- changeHashAndScroll ( '' ) ,
146
- expectScrollingToTop ) ) ;
144
+ describe ( 'and implicitly using `$location.hash()`' , function ( ) {
145
+
146
+ it ( 'should scroll to top of the window if empty hash' , inject (
147
+ changeHashAndScroll ( '' ) ,
148
+ expectScrollingToTop ) ) ;
149
+
150
+
151
+ it ( 'should not scroll if hash does not match any element' , inject (
152
+ addElements ( 'id=one' , 'id=two' ) ,
153
+ changeHashAndScroll ( 'non-existing' ) ,
154
+ expectNoScrolling ( ) ) ) ;
155
+
156
+
157
+ it ( 'should scroll to anchor element with name' , inject (
158
+ addElements ( 'a name=abc' ) ,
159
+ changeHashAndScroll ( 'abc' ) ,
160
+ expectScrollingTo ( 'a name=abc' ) ) ) ;
161
+
162
+
163
+ it ( 'should not scroll to other than anchor element with name' , inject (
164
+ addElements ( 'input name=xxl' , 'select name=xxl' , 'form name=xxl' ) ,
165
+ changeHashAndScroll ( 'xxl' ) ,
166
+ expectNoScrolling ( ) ) ) ;
167
+
168
+
169
+ it ( 'should scroll to anchor even if other element with given name exist' , inject (
170
+ addElements ( 'input name=some' , 'a name=some' ) ,
171
+ changeHashAndScroll ( 'some' ) ,
172
+ expectScrollingTo ( 'a name=some' ) ) ) ;
173
+
174
+
175
+ it ( 'should scroll to element with id with precedence over name' , inject (
176
+ addElements ( 'name=abc' , 'id=abc' ) ,
177
+ changeHashAndScroll ( 'abc' ) ,
178
+ expectScrollingTo ( 'id=abc' ) ) ) ;
147
179
148
180
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 ( ) ) ) ;
181
+ it ( 'should scroll to top if hash == "top" and no matching element' , inject (
182
+ changeHashAndScroll ( 'top' ) ,
183
+ expectScrollingToTop ) ) ;
153
184
154
185
155
- it ( 'should scroll to anchor element with name' , inject (
156
- addElements ( 'a name=abc' ) ,
157
- changeHashAndScroll ( 'abc' ) ,
158
- expectScrollingTo ( 'a name=abc' ) ) ) ;
186
+ it ( 'should scroll to element with id "top" if present' , inject (
187
+ addElements ( 'id=top' ) ,
188
+ changeHashAndScroll ( 'top' ) ,
189
+ expectScrollingTo ( 'id=top' ) ) ) ;
190
+ } ) ;
191
+
192
+
193
+ describe ( 'and specifying a hash' , function ( ) {
194
+
195
+ it ( 'should ignore the `hash` argument if not a string' , inject (
196
+ spyOnJQLiteDocumentLoaded ( ) ,
197
+ addElements ( 'id=one' , 'id=two' ) ,
198
+ changeHashTo ( 'one' ) , // won't scroll since `jqLiteDocumentLoaded()` is spied upon
199
+ callAnchorScroll ( { } ) ,
200
+ expectScrollingTo ( 'id=one' ) ,
201
+ unspyOnJQLiteDocumentLoaded ( ) ) ) ;
202
+
203
+
204
+ it ( 'should ignore `$location.hash()` if `hash` is passed as argument' , inject (
205
+ spyOnJQLiteDocumentLoaded ( ) ,
206
+ addElements ( 'id=one' , 'id=two' ) ,
207
+ changeHashTo ( 'one' ) , // won't scroll since `jqLiteDocumentLoaded()` is spied upon
208
+ callAnchorScroll ( 'two' ) ,
209
+ expectScrollingTo ( 'id=two' ) ,
210
+ unspyOnJQLiteDocumentLoaded ( ) ) ) ;
211
+
159
212
213
+ it ( 'should scroll to top of the window if empty hash' , inject (
214
+ callAnchorScroll ( '' ) ,
215
+ expectScrollingToTop ) ) ;
160
216
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 ( ) ) ) ;
165
217
218
+ it ( 'should not scroll if hash does not match any element' , inject (
219
+ addElements ( 'id=one' , 'id=two' ) ,
220
+ callAnchorScroll ( 'non-existing' ) ,
221
+ expectNoScrolling ( ) ) ) ;
166
222
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' ) ) ) ;
171
223
224
+ it ( 'should scroll to anchor element with name' , inject (
225
+ addElements ( 'a name=abc' ) ,
226
+ callAnchorScroll ( 'abc' ) ,
227
+ expectScrollingTo ( 'a name=abc' ) ) ) ;
172
228
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' ) ) ) ;
177
229
230
+ it ( 'should not scroll to other than anchor element with name' , inject (
231
+ addElements ( 'input name=xxl' , 'select name=xxl' , 'form name=xxl' ) ,
232
+ callAnchorScroll ( 'xxl' ) ,
233
+ expectNoScrolling ( ) ) ) ;
178
234
179
- it ( 'should scroll to top if hash == "top" and no matching element' , inject (
180
- changeHashAndScroll ( 'top' ) ,
181
- expectScrollingToTop ) ) ;
182
235
236
+ it ( 'should scroll to anchor even if other element with given name exist' , inject (
237
+ addElements ( 'input name=some' , 'a name=some' ) ,
238
+ callAnchorScroll ( 'some' ) ,
239
+ expectScrollingTo ( 'a name=some' ) ) ) ;
183
240
184
- it ( 'should scroll to element with id "top" if present' , inject (
185
- addElements ( 'id=top' ) ,
186
- changeHashAndScroll ( 'top' ) ,
187
- expectScrollingTo ( 'id=top' ) ) ) ;
241
+
242
+ it ( 'should scroll to element with id with precedence over name' , inject (
243
+ addElements ( 'name=abc' , 'id=abc' ) ,
244
+ callAnchorScroll ( 'abc' ) ,
245
+ expectScrollingTo ( 'id=abc' ) ) ) ;
246
+
247
+
248
+ it ( 'should scroll to top if hash == "top" and no matching element' , inject (
249
+ callAnchorScroll ( 'top' ) ,
250
+ expectScrollingToTop ) ) ;
251
+
252
+
253
+ it ( 'should scroll to element with id "top" if present' , inject (
254
+ addElements ( 'id=top' ) ,
255
+ callAnchorScroll ( 'top' ) ,
256
+ expectScrollingTo ( 'id=top' ) ) ) ;
257
+ } ) ;
188
258
} ) ;
189
259
190
260
0 commit comments