( function() { if ( !jQuery.fn.width ) { return; } QUnit.module( "dimensions", { teardown: moduleTeardown } ); function pass( val ) { return val; } function fn( val ) { return function() { return val; }; } /* ======== local reference ======= pass and fn can be used to test passing functions to setters See testWidth below for an example pass( value ); This function returns whatever value is passed in fn( value ); Returns a function that returns the value */ function testWidth( val, assert ) { assert.expect( 9 ); var $div, blah; $div = jQuery( "#nothiddendiv" ); $div.width( val( 30 ) ); assert.equal( $div.width(), 30, "Test set to 30 correctly" ); $div.hide(); assert.equal( $div.width(), 30, "Test hidden div" ); $div.show(); $div.width( val( -1 ) ); // handle negative numbers by setting to 0 #11604 assert.equal( $div.width(), 0, "Test negative width normalized to 0" ); $div.css( "padding", "20px" ); assert.equal( $div.width(), 0, "Test padding specified with pixels" ); $div.css( "border", "2px solid #fff" ); assert.equal( $div.width(), 0, "Test border specified with pixels" ); $div.css( { "display": "", "border": "", "padding": "" } ); jQuery( "#nothiddendivchild" ).css( { "width": 20, "padding": "3px", "border": "2px solid #fff" } ); assert.equal( jQuery( "#nothiddendivchild" ).width(), 20, "Test child width with border and padding" ); jQuery( "#nothiddendiv, #nothiddendivchild" ).css( { "border": "", "padding": "", "width": "" } ); blah = jQuery( "blah" ); assert.equal( blah.width( val( 10 ) ), blah, "Make sure that setting a width on an empty set returns the set." ); assert.equal( blah.width(), null, "Make sure 'null' is returned on an empty set" ); assert.equal( jQuery( window ).width(), document.documentElement.clientWidth, "Window width is equal to width reported by window/document." ); QUnit.expectJqData( this, $div[ 0 ], "olddisplay" ); } QUnit.test( "width()", function( assert ) { testWidth( pass, assert ); } ); QUnit.test( "width(Function)", function( assert ) { testWidth( fn, assert ); } ); QUnit.test( "width(Function(args))", function( assert ) { assert.expect( 2 ); var $div = jQuery( "#nothiddendiv" ); $div.width( 30 ).width( function( i, width ) { assert.equal( width, 30, "Make sure previous value is correct." ); return width + 1; } ); assert.equal( $div.width(), 31, "Make sure value was modified correctly." ); } ); function testHeight( val, assert ) { assert.expect( 9 ); var $div, blah; $div = jQuery( "#nothiddendiv" ); $div.height( val( 30 ) ); assert.equal( $div.height(), 30, "Test set to 30 correctly" ); $div.hide(); assert.equal( $div.height(), 30, "Test hidden div" ); $div.show(); $div.height( val( -1 ) ); // handle negative numbers by setting to 0 #11604 assert.equal( $div.height(), 0, "Test negative height normalized to 0" ); $div.css( "padding", "20px" ); assert.equal( $div.height(), 0, "Test padding specified with pixels" ); $div.css( "border", "2px solid #fff" ); assert.equal( $div.height(), 0, "Test border specified with pixels" ); $div.css( { "display": "", "border": "", "padding": "", "height": "1px" } ); jQuery( "#nothiddendivchild" ).css( { "height": 20, "padding": "3px", "border": "2px solid #fff" } ); assert.equal( jQuery( "#nothiddendivchild" ).height(), 20, "Test child height with border and padding" ); jQuery( "#nothiddendiv, #nothiddendivchild" ).css( { "border": "", "padding": "", "height": "" } ); blah = jQuery( "blah" ); assert.equal( blah.height( val( 10 ) ), blah, "Make sure that setting a height on an empty set returns the set." ); assert.equal( blah.height(), null, "Make sure 'null' is returned on an empty set" ); assert.equal( jQuery( window ).height(), document.documentElement.clientHeight, "Window width is equal to width reported by window/document." ); QUnit.expectJqData( this, $div[ 0 ], "olddisplay" ); } QUnit.test( "height()", function( assert ) { testHeight( pass, assert ); } ); QUnit.test( "height(Function)", function( assert ) { testHeight( fn, assert ); } ); QUnit.test( "height(Function(args))", function( assert ) { assert.expect( 2 ); var $div = jQuery( "#nothiddendiv" ); $div.height( 30 ).height( function( i, height ) { assert.equal( height, 30, "Make sure previous value is correct." ); return height + 1; } ); assert.equal( $div.height(), 31, "Make sure value was modified correctly." ); } ); QUnit.test( "innerWidth()", function( assert ) { assert.expect( 6 ); var $div, div, $win = jQuery( window ), $doc = jQuery( document ); assert.equal( jQuery( window ).innerWidth(), $win.width(), "Test on window" ); assert.equal( jQuery( document ).innerWidth(), $doc.width(), "Test on document" ); $div = jQuery( "#nothiddendiv" ); $div.css( { "margin": 10, "border": "2px solid #fff", "width": 30 } ); assert.equal( $div.innerWidth(), 30, "Test with margin and border" ); $div.css( "padding", "20px" ); assert.equal( $div.innerWidth(), 70, "Test with margin, border and padding" ); $div.hide(); assert.equal( $div.innerWidth(), 70, "Test hidden div" ); // reset styles $div.css( { "display": "", "border": "", "padding": "", "width": "", "height": "" } ); div = jQuery( "
a | |
a |