From 15fd735793cffe89fdf9662275409cdcdb3e801a Mon Sep 17 00:00:00 2001
From: Vojta Jina
Date: Thu, 12 Jan 2012 03:00:34 -0800
Subject: refactor($autoScroll): rename to $anchorScroll and allow disabling
auto scrolling (links)
Now, that we have autoscroll attribute on ng:include, there is no reason to disable the service completely, so $anchorScrollProvider.disableAutoScrolling() means it won't be scrolling when $location.hash() changes.
And then, it's not $autoScroll at all, it actually scrolls to anchor when it's called, so I renamed
it to $anchorScroll.
---
test/service/anchorScrollSpec.js | 186 +++++++++++++++++++++++++++++++++++++
test/service/autoScrollSpec.js | 196 ---------------------------------------
test/widgetsSpec.js | 16 ++--
3 files changed, 194 insertions(+), 204 deletions(-)
create mode 100644 test/service/anchorScrollSpec.js
delete mode 100644 test/service/autoScrollSpec.js
(limited to 'test')
diff --git a/test/service/anchorScrollSpec.js b/test/service/anchorScrollSpec.js
new file mode 100644
index 00000000..7e4b3aa3
--- /dev/null
+++ b/test/service/anchorScrollSpec.js
@@ -0,0 +1,186 @@
+describe('$anchorScroll', function() {
+
+ var elmSpy;
+
+ function addElements() {
+ var elements = sliceArgs(arguments);
+
+ return function() {
+ forEach(elements, function(identifier) {
+ var match = identifier.match(/(\w* )?(\w*)=(\w*)/),
+ jqElm = jqLite('<' + (match[1] || 'a ') + match[2] + '="' + match[3] + '"/>'),
+ elm = jqElm[0];
+
+ elmSpy[identifier] = spyOn(elm, 'scrollIntoView');
+ jqLite(document.body).append(jqElm);
+ });
+ };
+ }
+
+ function changeHashAndScroll(hash) {
+ return function($location, $anchorScroll) {
+ $location.hash(hash);
+ $anchorScroll();
+ };
+ }
+
+ function expectScrollingToTop($window) {
+ forEach(elmSpy, function(spy, id) {
+ expect(spy).not.toHaveBeenCalled();
+ });
+
+ expect($window.scrollTo).toHaveBeenCalledWith(0, 0);
+ }
+
+ function expectScrollingTo(identifier) {
+ return function($window) {
+ forEach(elmSpy, function(spy, id) {
+ if (identifier === id) expect(spy).toHaveBeenCalledOnce();
+ else expect(spy).not.toHaveBeenCalled();
+ });
+ expect($window.scrollTo).not.toHaveBeenCalled();
+ };
+ }
+
+ function expectNoScrolling() {
+ return expectScrollingTo(NaN);
+ }
+
+
+ beforeEach(module(function($provide) {
+ elmSpy = {};
+ $provide.value('$window', {
+ scrollTo: jasmine.createSpy('$window.scrollTo'),
+ document: document
+ });
+ }));
+
+
+ it('should scroll to top of the window if empty hash', inject(
+ changeHashAndScroll(''),
+ expectScrollingToTop));
+
+
+ it('should not scroll if hash does not match any element', inject(
+ addElements('id=one', 'id=two'),
+ changeHashAndScroll('non-existing'),
+ expectNoScrolling()));
+
+
+ it('should scroll to anchor element with name', inject(
+ addElements('a name=abc'),
+ changeHashAndScroll('abc'),
+ expectScrollingTo('a name=abc')));
+
+
+ it('should not scroll to other than anchor element with name', inject(
+ addElements('input name=xxl', 'select name=xxl', 'form name=xxl'),
+ changeHashAndScroll('xxl'),
+ expectNoScrolling()));
+
+
+ it('should scroll to anchor even if other element with given name exist', inject(
+ addElements('input name=some', 'a name=some'),
+ changeHashAndScroll('some'),
+ expectScrollingTo('a name=some')));
+
+
+ it('should scroll to element with id with precedence over name', inject(
+ addElements('name=abc', 'id=abc'),
+ changeHashAndScroll('abc'),
+ expectScrollingTo('id=abc')));
+
+
+ it('should scroll to top if hash == "top" and no matching element', inject(
+ changeHashAndScroll('top'),
+ expectScrollingToTop));
+
+
+ it('should scroll to element with id "top" if present', inject(
+ addElements('id=top'),
+ changeHashAndScroll('top'),
+ expectScrollingTo('id=top')));
+
+
+ describe('watcher', function() {
+
+ function initLocation(config) {
+ return function($provide, $locationProvider) {
+ $provide.value('$sniffer', {history: config.historyApi});
+ $locationProvider.html5Mode(config.html5Mode);
+ };
+ }
+
+ function changeHashTo(hash) {
+ return function ($location, $rootScope, $anchorScroll) {
+ $rootScope.$apply(function() {
+ $location.hash(hash);
+ });
+ };
+ }
+
+ function disableAutoScrolling() {
+ return function($anchorScrollProvider) {
+ $anchorScrollProvider.disableAutoScrolling();
+ };
+ }
+
+ afterEach(inject(function($document) {
+ dealoc($document);
+ }));
+
+
+ it('should scroll to element when hash change in hashbang mode', function() {
+ module(initLocation({html5Mode: false, historyApi: true}));
+ inject(
+ addElements('id=some'),
+ changeHashTo('some'),
+ expectScrollingTo('id=some')
+ );
+ });
+
+
+ it('should scroll to element when hash change in html5 mode with no history api', function() {
+ module(initLocation({html5Mode: true, historyApi: false}));
+ inject(
+ addElements('id=some'),
+ changeHashTo('some'),
+ expectScrollingTo('id=some')
+ );
+ });
+
+
+ it('should not scroll when element does not exist', function() {
+ module(initLocation({html5Mode: false, historyApi: false}));
+ inject(
+ addElements('id=some'),
+ changeHashTo('other'),
+ expectNoScrolling()
+ );
+ });
+
+
+ it('should scroll when html5 mode with history api', function() {
+ module(initLocation({html5Mode: true, historyApi: true}));
+ inject(
+ addElements('id=some'),
+ changeHashTo('some'),
+ expectScrollingTo('id=some')
+ );
+ });
+
+
+ it('should not scroll when disabled', function() {
+ module(
+ disableAutoScrolling(),
+ initLocation({html5Mode: false, historyApi: false})
+ );
+ inject(
+ addElements('id=fake'),
+ changeHashTo('fake'),
+ expectNoScrolling()
+ );
+ });
+ });
+});
+
diff --git a/test/service/autoScrollSpec.js b/test/service/autoScrollSpec.js
deleted file mode 100644
index 72fc3424..00000000
--- a/test/service/autoScrollSpec.js
+++ /dev/null
@@ -1,196 +0,0 @@
-describe('$autoScroll', function() {
-
- var elmSpy;
-
- function addElements() {
- var elements = sliceArgs(arguments);
-
- return function() {
- forEach(elements, function(identifier) {
- var match = identifier.match(/(\w* )?(\w*)=(\w*)/),
- jqElm = jqLite('<' + (match[1] || 'a ') + match[2] + '="' + match[3] + '"/>'),
- elm = jqElm[0];
-
- elmSpy[identifier] = spyOn(elm, 'scrollIntoView');
- jqLite(document.body).append(jqElm);
- });
- };
- }
-
- function changeHashAndScroll(hash) {
- return function($location, $autoScroll) {
- $location.hash(hash);
- $autoScroll();
- };
- }
-
- function expectScrollingToTop($window) {
- forEach(elmSpy, function(spy, id) {
- expect(spy).not.toHaveBeenCalled();
- });
-
- expect($window.scrollTo).toHaveBeenCalledWith(0, 0);
- }
-
- function expectScrollingTo(identifier) {
- return function($window) {
- forEach(elmSpy, function(spy, id) {
- if (identifier === id) expect(spy).toHaveBeenCalledOnce();
- else expect(spy).not.toHaveBeenCalled();
- });
- expect($window.scrollTo).not.toHaveBeenCalled();
- };
- }
-
- function expectNoScrolling() {
- return expectScrollingTo(NaN);
- }
-
- function disableScroller() {
- return function($autoScrollProvider) {
- $autoScrollProvider.disable();
- };
- }
-
-
- beforeEach(module(function($provide) {
- elmSpy = {};
- $provide.value('$window', {
- scrollTo: jasmine.createSpy('$window.scrollTo'),
- document: document
- });
- }));
-
-
- it('should scroll to top of the window if empty hash', inject(
- changeHashAndScroll(''),
- expectScrollingToTop));
-
-
- it('should not scroll if hash does not match any element', inject(
- addElements('id=one', 'id=two'),
- changeHashAndScroll('non-existing'),
- expectNoScrolling()));
-
-
- it('should scroll to anchor element with name', inject(
- addElements('a name=abc'),
- changeHashAndScroll('abc'),
- expectScrollingTo('a name=abc')));
-
-
- it('should not scroll to other than anchor element with name', inject(
- addElements('input name=xxl', 'select name=xxl', 'form name=xxl'),
- changeHashAndScroll('xxl'),
- expectNoScrolling()));
-
-
- it('should scroll to anchor even if other element with given name exist', inject(
- addElements('input name=some', 'a name=some'),
- changeHashAndScroll('some'),
- expectScrollingTo('a name=some')));
-
-
- it('should scroll to element with id with precedence over name', inject(
- addElements('name=abc', 'id=abc'),
- changeHashAndScroll('abc'),
- expectScrollingTo('id=abc')));
-
-
- it('should scroll to top if hash == "top" and no matching element', inject(
- changeHashAndScroll('top'),
- expectScrollingToTop));
-
-
- it('should scroll to element with id "top" if present', inject(
- addElements('id=top'),
- changeHashAndScroll('top'),
- expectScrollingTo('id=top')));
-
-
- it('should not scroll when disabled', function() {
- module(disableScroller());
- inject(
- addElements('id=fake', 'a name=fake', 'input name=fake'),
- changeHashAndScroll('fake'),
- expectNoScrolling()
- );
- });
-
-
- describe('watcher', function() {
-
- function initLocation(config) {
- return function($provide, $locationProvider) {
- $provide.value('$sniffer', {history: config.historyApi});
- $locationProvider.html5Mode(config.html5Mode);
- };
- }
-
- function changeHashTo(hash) {
- return function ($location, $rootScope, $autoScroll) {
- $rootScope.$apply(function() {
- $location.hash(hash);
- });
- };
- }
-
- afterEach(inject(function($document) {
- dealoc($document);
- }));
-
-
- it('should scroll to element when hash change in hashbang mode', function() {
- module(initLocation({html5Mode: false, historyApi: true}));
- inject(
- addElements('id=some'),
- changeHashTo('some'),
- expectScrollingTo('id=some')
- );
- });
-
-
- it('should scroll to element when hash change in html5 mode with no history api', function() {
- module(initLocation({html5Mode: true, historyApi: false}));
- inject(
- addElements('id=some'),
- changeHashTo('some'),
- expectScrollingTo('id=some')
- );
- });
-
-
- it('should not scroll when element does not exist', function() {
- module(initLocation({html5Mode: false, historyApi: false}));
- inject(
- addElements('id=some'),
- changeHashTo('other'),
- expectNoScrolling()
- );
- });
-
-
- it('should scroll when html5 mode with history api', function() {
- module(initLocation({html5Mode: true, historyApi: true}));
- inject(
- addElements('id=some'),
- changeHashTo('some'),
- expectScrollingTo('id=some')
- );
- });
-
-
- it('should not scroll when disabled', function() {
- module(
- disableScroller(),
- initLocation({html5Mode: false, historyApi: false})
- );
- inject(
- addElements('id=fake'),
- changeHashTo('fake'),
- expectNoScrolling()
- );
- });
- });
-});
-
diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js
index 753a36b4..f119174f 100644
--- a/test/widgetsSpec.js
+++ b/test/widgetsSpec.js
@@ -225,10 +225,10 @@ describe('widget', function() {
describe('autoscoll', function() {
var autoScrollSpy;
- function spyOnAutoScroll() {
+ function spyOnAnchorScroll() {
return function($provide) {
- autoScrollSpy = jasmine.createSpy('$autoScroll');
- $provide.value('$autoScroll', autoScrollSpy);
+ autoScrollSpy = jasmine.createSpy('$anchorScroll');
+ $provide.value('$anchorScroll', autoScrollSpy);
};
}
@@ -247,20 +247,20 @@ describe('widget', function() {
};
}
- beforeEach(module(spyOnAutoScroll()));
+ beforeEach(module(spyOnAnchorScroll()));
beforeEach(inject(
putIntoCache('template.html', 'CONTENT'),
putIntoCache('another.html', 'CONTENT')));
- it('should call $autoScroll if autoscroll attribute is present', inject(
+ it('should call $anchorScroll if autoscroll attribute is present', inject(
compileAndLink(''),
changeTplAndValueTo('template.html'), function() {
expect(autoScrollSpy).toHaveBeenCalledOnce();
}));
- it('should call $autoScroll if autoscroll evaluates to true', inject(
+ it('should call $anchorScroll if autoscroll evaluates to true', inject(
compileAndLink(''),
changeTplAndValueTo('template.html', true),
changeTplAndValueTo('another.html', 'some-string'),
@@ -270,14 +270,14 @@ describe('widget', function() {
}));
- it('should not call $autoScroll if autoscroll attribute is not present', inject(
+ it('should not call $anchorScroll if autoscroll attribute is not present', inject(
compileAndLink(''),
changeTplAndValueTo('template.html'), function() {
expect(autoScrollSpy).not.toHaveBeenCalled();
}));
- it('should not call $autoScroll if autoscroll evaluates to false', inject(
+ it('should not call $anchorScroll if autoscroll evaluates to false', inject(
compileAndLink(''),
changeTplAndValueTo('template.html', false),
changeTplAndValueTo('template.html', undefined),
--
cgit v1.2.3