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( 'isElementCloseToViewport', 3, function ( assert
) {
71 distantElement
= $( '<div />' )
72 .appendTo( '#qunit-fixture' )
81 assert
.ok( mw
.viewport
.isElementCloseToViewport( this.el
, 60, viewport
),
82 'It should return true when the element is within the given threshold away' );
83 assert
.notOk( mw
.viewport
.isElementCloseToViewport( this.el
, 20, viewport
),
84 'It should return false when the element is further than the given threshold away' );
85 assert
.notOk( mw
.viewport
.isElementCloseToViewport( distantElement
),
86 'It should default to a threshold of 50px and the window\'s viewport' );
89 }( mediaWiki
, jQuery
) );