3 // Simulate square element with 20px long edges placed at (20, 20) on the page
12 QUnit.module( 'mediawiki.viewport', QUnit.newMwEnvironment( {
14 this.el = $( '<div />' )
15 .appendTo( '#qunit-fixture' )
23 this.sandbox.stub( mw.viewport, 'makeViewportFromWindow' )
24 .returns( DEFAULT_VIEWPORT );
28 QUnit.test( 'isElementInViewport', 6, function ( assert ) {
29 var viewport = $.extend( {}, DEFAULT_VIEWPORT );
30 assert.ok( mw.viewport.isElementInViewport( this.el, viewport ),
31 'It should return true when the element is fully enclosed in the viewport' );
35 assert.ok( mw.viewport.isElementInViewport( this.el, viewport ),
36 'It should return true when only the top-left of the element is within the viewport' );
42 assert.ok( mw.viewport.isElementInViewport( this.el, viewport ),
43 'It should return true when only the bottom-right is within the viewport' );
49 assert.ok( mw.viewport.isElementInViewport( this.el, viewport ),
50 'It should return true when the element encapsulates the viewport' );
56 assert.notOk( mw.viewport.isElementInViewport( this.el, viewport ),
57 'It should return false when the element is not within the viewport' );
59 assert.ok( mw.viewport.isElementInViewport( this.el ),
60 'It should default to the window object if no viewport is given' );
63 QUnit.test( 'isElementInViewport with scrolled page', 1, function ( assert ) {
71 .appendTo( '#qunit-fixture' )
79 window.scrollTo( viewport.left, viewport.top );
80 assert.ok( mw.viewport.isElementInViewport( el, viewport ),
81 'It should return true when the element is fully enclosed in the ' +
82 'viewport even when the page is scrolled down' );
83 window.scrollTo( 0, 0 );
86 QUnit.test( 'isElementCloseToViewport', 3, function ( assert ) {
94 distantElement = $( '<div />' )
95 .appendTo( '#qunit-fixture' )
104 assert.ok( mw.viewport.isElementCloseToViewport( this.el, 60, viewport ),
105 'It should return true when the element is within the given threshold away' );
106 assert.notOk( mw.viewport.isElementCloseToViewport( this.el, 20, viewport ),
107 'It should return false when the element is further than the given threshold away' );
108 assert.notOk( mw.viewport.isElementCloseToViewport( distantElement ),
109 'It should default to a threshold of 50px and the window\'s viewport' );
112 }( mediaWiki, jQuery ) );