Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Steven Bellamy
committed
Jul 28, 2016
1 parent
a480dd8
commit 427c696
Showing
3 changed files
with
91 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,83 @@ | ||
'use strict'; | ||
var BASE_JS_PATH = '../../../cfgov/unprocessed/js/'; | ||
|
||
var chai = require( 'chai' ); | ||
var expect = chai.expect; | ||
var Footer = require( BASE_JS_PATH + 'organisms/Footer' ); | ||
var jsdom = require( 'mocha-jsdom' ); | ||
var sinon = require( 'sinon' ); | ||
|
||
var BASE_JS_PATH = '../../../cfgov/unprocessed/js/'; | ||
var FooterButton = require( BASE_JS_PATH + 'modules/footer-button.js' ); | ||
|
||
var footerBtnDom; | ||
var bodyDom; | ||
var sandbox; | ||
var lastTime = 0; | ||
|
||
var HTML_SNIPPET = | ||
'<a class="btn btn__secondary o-footer_top-button" ' + | ||
'data-gtm_ignore="true" data-js-hook="behavior_return-to-top" ' + | ||
'href="#">' + | ||
'Back to top <span class="cf-icon cf-icon-arrow-up"></span>' + | ||
'</a>'; | ||
|
||
function triggerClick( target ) { | ||
var clickEvent = new MouseEvent( 'click', { | ||
bubbles: true, | ||
cancelable: true, | ||
view: window | ||
} ); | ||
|
||
target.dispatchEvent( clickEvent ); | ||
} | ||
|
||
function scrollTo( xCoord, yCoord ) { | ||
window.scrollX = xCoord; | ||
window.scrollY = yCoord; | ||
} | ||
|
||
function requestAnimationFrame( callback ) { | ||
var currTime = new Date().getTime(); | ||
var timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) ); | ||
var id = window.setTimeout( | ||
function() { | ||
callback( currTime + timeToCall ); | ||
}, timeToCall | ||
); | ||
|
||
lastTime = currTime + timeToCall; | ||
|
||
return id; | ||
} | ||
|
||
describe( 'Footer', function() { | ||
// TODO: Implement tests. | ||
jsdom(); | ||
|
||
before( function() { | ||
sandbox = sinon.sandbox.create(); | ||
} ); | ||
|
||
beforeEach( function( ) { | ||
bodyDom = document.body; | ||
bodyDom.innerHTML = HTML_SNIPPET; | ||
footerBtnDom = document.querySelector( '.o-footer_top-button' ); | ||
|
||
window.requestAnimationFrame = requestAnimationFrame; | ||
window.scrollTo = scrollTo; | ||
} ); | ||
|
||
afterEach( function() { | ||
sandbox.restore(); | ||
} ); | ||
|
||
it( 'button should initiate scroll to page top when clicked', | ||
function( done ) { | ||
window.scrollY = 10; | ||
FooterButton.init(); | ||
triggerClick( footerBtnDom ); | ||
this.timeout( 3000 ); | ||
window.setTimeout( function( ) { | ||
expect( window.scrollY ).to.equal( 0 ); | ||
done(); | ||
}, 2000 ); | ||
} ); | ||
} ); |