Skip to content

Commit

Permalink
test and readability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
WickyNilliams committed Jan 9, 2014
1 parent f78bdda commit d203245
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 25 deletions.
114 changes: 92 additions & 22 deletions spec/Headroom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
headroom.attachEvent();

expect(headroom.initialised).toBe(true);
expect(global.addEventListener).toHaveBeenCalledWith('scroll', headroom.debouncer, false);
expect(addEventListener).toHaveBeenCalledWith('scroll', headroom.debouncer, false);
});

it('will only ever add one listener', function() {
Expand All @@ -131,38 +131,95 @@

describe('pin', function() {

it('should add pinned class and remove unpinned class', function(){
headroom.pin();
beforeEach(function() {
headroom.onPin = jasmine.createSpy();
});

describe('when unpinned class is present', function() {

beforeEach(function() {
classList.contains.andReturn(true);
headroom.pin();
});

it('should add pinned class and remove unpinned class', function(){
expect(classList.remove).toHaveBeenCalledWith(headroom.classes.unpinned);
expect(classList.add).toHaveBeenCalledWith(headroom.classes.pinned);
});

it('should invoke callback if supplied', function() {
expect(headroom.onPin).toHaveBeenCalled();
});

expect(classList.remove).toHaveBeenCalledWith(headroom.classes.unpinned);
expect(classList.add).toHaveBeenCalledWith(headroom.classes.pinned);
});

it('should invoke callback if supplied', function() {
headroom.onPin = jasmine.createSpy();
describe('when unpinned class not present', function() {

headroom.pin();
beforeEach(function() {
headroom.pin();
});

it('should do nothing', function() {
expect(headroom.onPin).not.toHaveBeenCalled();
});

expect(headroom.onPin).toHaveBeenCalled();
});

});

describe('unpin', function() {

it('should add unpinned class and remove pinned class', function(){
headroom.unpin();
var classes;

expect(classList.add).toHaveBeenCalledWith(headroom.classes.unpinned);
expect(classList.remove).toHaveBeenCalledWith(headroom.classes.pinned);
});

it('should invoke callback if supplied', function() {
beforeEach(function() {
headroom.onUnpin = jasmine.createSpy();
classes = {};

headroom.unpin();
classList.contains.andCallFake(function(className) {
return classes[className];
});
});

function setupFixture (pinned, unpinned) {
classes[Headroom.options.classes.unpinned] = unpinned;
classes[Headroom.options.classes.pinned] = pinned;
}

expect(headroom.onUnpin).toHaveBeenCalled();
describe('when currently pinned', function() {

beforeEach(function() {
setupFixture(true, false);
headroom.unpin();
});

it('will add unpinned class', function() {
expect(classList.add).toHaveBeenCalledWith(headroom.classes.unpinned);
});

it('will remove pinned class', function() {
expect(classList.remove).toHaveBeenCalledWith(headroom.classes.pinned);
});

it('will invoke callback if supplied', function() {
expect(headroom.onUnpin).toHaveBeenCalled();
});
});

describe('when currently unpinned', function() {
it('will do nothing', function() {
setupFixture(false, true);
headroom.unpin();
expect(headroom.onUnpin).not.toHaveBeenCalled();
});
});

describe('when never been unpinned', function() {
it('will unpin', function() {
setupFixture(false, false);
headroom.unpin();
expect(headroom.onUnpin).toHaveBeenCalled();
});
});

});
Expand Down Expand Up @@ -196,19 +253,32 @@
getDocumentHeight = spyOn(headroom, 'getDocumentHeight');
});

it('return true if past bottom', function() {
it('return true if past top', function() {
var result = headroom.isOutOfBounds(-1);
expect(result).toBe(true);
});

it('return true if past top', function() {
getViewportHeight.andReturn(2);
getDocumentHeight.andReturn(2);
it('return true if past bottom', function() {
var documentHeight = 20;
var viewportHeight = 20;

var result = headroom.isOutOfBounds(1);
getDocumentHeight.andReturn(documentHeight);
getViewportHeight.andReturn(viewportHeight);
var result = headroom.isOutOfBounds(viewportHeight + 1);

expect(result).toBe(true);
});

it('return false if in bounds', function() {
var documentHeight = 200;
var viewportHeight = 20;

getDocumentHeight.andReturn(documentHeight);
getViewportHeight.andReturn(viewportHeight);
var result = headroom.isOutOfBounds(10);

expect(result).toBe(false);
});
});

describe('getDocumentHeight', function() {
Expand Down
17 changes: 14 additions & 3 deletions src/Headroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ Headroom.prototype = {
* @return {Number} pixels the page has scrolled along the Y-axis
*/
getScrollY : function() {
return (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
return (window.pageYOffset !== undefined)
? window.pageYOffset
: (document.documentElement || document.body.parentNode || document.body).scrollTop;
},

/**
Expand Down Expand Up @@ -166,6 +168,15 @@ Headroom.prototype = {
return pastTop || pastBottom;
},

/**
* determines if the tolerance has been exceeded
* @param {int} currentScrollY the current scroll y position
* @return {bool} true if tolerance exceeded, false otherwise
*/
toleranceExceeded : function (currentScrollY) {
return Math.abs(currentScrollY-this.lastKnownScrollY) >= this.tolerance;
},

/**
* determine if it is appropriate to unpin
* @param {int} currentScrollY the current y scroll position
Expand Down Expand Up @@ -196,8 +207,8 @@ Headroom.prototype = {
* Handles updating the state of the widget
*/
update : function() {
var currentScrollY = this.getScrollY(),
toleranceExceeded = Math.abs(currentScrollY-this.lastKnownScrollY) >= this.tolerance;
var currentScrollY = this.getScrollY(),
toleranceExceeded = this.toleranceExceeded(currentScrollY);

if(this.isOutOfBounds(currentScrollY)) { // Ignore bouncy scrolling in OSX
return;
Expand Down

0 comments on commit d203245

Please sign in to comment.