From 76a5308679c604fdf5c2694ef2fdbdf317c05189 Mon Sep 17 00:00:00 2001 From: Gemma Leigh Date: Tue, 20 Sep 2016 17:30:52 +0100 Subject: [PATCH] Update tests 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. --- spec/unit/stick-at-top-when-scrolling.spec.js | 112 +++++++++++++++--- 1 file changed, 97 insertions(+), 15 deletions(-) diff --git a/spec/unit/stick-at-top-when-scrolling.spec.js b/spec/unit/stick-at-top-when-scrolling.spec.js index b89aeef2..fab0b591 100644 --- a/spec/unit/stick-at-top-when-scrolling.spec.js +++ b/spec/unit/stick-at-top-when-scrolling.spec.js @@ -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 = $('
'); + 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); + }); + }); + });