Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
Re-order tests - when sticking and unsticking.

Add mocks to test that sticking and unsticking works for smaller and
larger screens, test that all three conditions in the checkScroll
function stick/unstick.
  • Loading branch information
gemmaleigh committed Sep 21, 2016
1 parent 04812ca commit 76c62bb
Showing 1 changed file with 97 additions and 15 deletions.
112 changes: 97 additions & 15 deletions spec/unit/stick-at-top-when-scrolling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,109 @@ describe("stick-at-top-when-scrolling", function(){
$stickyWrapper.remove();
});

it('should add fixed class on stick', function(){
expect(!$stickyElement.hasClass('content-fixed')).toBe(true);
GOVUK.stickAtTopWhenScrolling.stick($stickyElement);
expect($stickyElement.hasClass('content-fixed')).toBe(true);
describe('when stick is called', function(){

it('should add fixed class on stick', function(){
expect(!$stickyElement.hasClass('content-fixed')).toBe(true);
GOVUK.stickAtTopWhenScrolling.stick($stickyElement);
expect($stickyElement.hasClass('content-fixed')).toBe(true);
});

it('should insert shim when sticking the element', function(){
expect($('.shim').length).toBe(0);
GOVUK.stickAtTopWhenScrolling.stick($stickyElement);
expect($('.shim').length).toBe(1);
});

it('should insert shim with minimum height', function(){
GOVUK.stickAtTopWhenScrolling.stick($stickyElement);
expect($('.shim').height()).toBe(1);
});

});

it('should remove fixed class on release', function(){
$stickyElement.addClass('content-fixed');
GOVUK.stickAtTopWhenScrolling.release($stickyElement);
expect(!$stickyElement.hasClass('content-fixed')).toBe(true);
describe('when release is called', function(){

it('should remove fixed class', function(){
$stickyElement.addClass('content-fixed');
GOVUK.stickAtTopWhenScrolling.release($stickyElement);
expect($stickyElement.hasClass('content-fixed')).toBe(false);
});

it('should remove the shim', function(){
$stickyElement = $('<div class="stick-at-top-when-scrolling content-fixed"></div>');
GOVUK.stickAtTopWhenScrolling.release($stickyElement);
expect($('.shim').length).toBe(0);
});

});

it('should insert shim when sticking content', function(){
expect($('.shim').length).toBe(0);
GOVUK.stickAtTopWhenScrolling.stick($stickyElement);
expect($('.shim').length).toBe(1);
describe('for larger screens (>768px)', function(){

beforeEach(function() {
GOVUK.stickAtTopWhenScrolling.getWindowPositions = function(){
return {
scrollTop: 300
};
};
GOVUK.stickAtTopWhenScrolling.getElementOffset = function(){
return {
top: 300
};
};
GOVUK.stickAtTopWhenScrolling.getWindowDimensions = function(){
return {
height: 768,
width: 769
};
};
GOVUK.stickAtTopWhenScrolling.$els = $stickyElement;
GOVUK.stickAtTopWhenScrolling._hasScrolled = true;
GOVUK.stickAtTopWhenScrolling.checkScroll();
});

it('should stick, if the scroll position is past the element position', function(){
expect($stickyElement.hasClass('content-fixed')).toBe(true);
});

it('should unstick, if the scroll position is less than the point at which scrolling started', function(){
GOVUK.stickAtTopWhenScrolling.getWindowPositions = function() {
return {
scrollTop: 0
};
};
GOVUK.stickAtTopWhenScrolling.$els = $stickyElement;
GOVUK.stickAtTopWhenScrolling._hasScrolled = true;
GOVUK.stickAtTopWhenScrolling.checkScroll();
expect($stickyElement.hasClass('content-fixed')).toBe(false);
});

});

it('should insert shim with minimum height', function(){
GOVUK.stickAtTopWhenScrolling.stick($stickyElement);
expect($('.shim').height()).toBe(1);
describe('for smaller screens (<=768px)', function(){

beforeEach(function() {
GOVUK.stickAtTopWhenScrolling.getWindowDimensions = function() {
return {
height: 768,
width: 767
};
};
GOVUK.stickAtTopWhenScrolling.getElementOffset = function() {
return {
top: 300
};
};
GOVUK.stickAtTopWhenScrolling.$els = $stickyElement;
GOVUK.stickAtTopWhenScrolling._hasScrolled = true;
GOVUK.stickAtTopWhenScrolling.checkScroll();
});

it('should unstick the element', function(){
expect($stickyElement.hasClass('content-fixed')).toBe(false);
});

});

});

0 comments on commit 76c62bb

Please sign in to comment.