tests.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * getSize tests
  3. * with QUnit
  4. **/
  5. /*jshint browser: true, devel: true, strict: true, undef: true */
  6. /*global equal: false, getSize: false, ok: false, test: false, strictEqual: false */
  7. ( function( window ) {
  8. 'use strict';
  9. function getBoxSize( num ) {
  10. var box = document.querySelector( '#ex' + num + ' .box' );
  11. return getSize( box );
  12. }
  13. test( 'arguments', function() {
  14. ok( !getSize( 0 ), 'Number returns falsey' );
  15. ok( !getSize( document.querySelector('#foobabbles') ), 'bad querySelector returns falsey' );
  16. ok( getSize('#ex1'), 'query selector string works' );
  17. });
  18. test( 'ex1: no styling', function() {
  19. var size = getBoxSize(1);
  20. equal( size.width, 400, 'Inherit container width' );
  21. equal( size.height, 0, 'No height' );
  22. equal( size.isBorderBox, false, 'isBorderBox' );
  23. });
  24. test( 'ex2: height: 100%', function() {
  25. var size = getBoxSize(2);
  26. equal( size.height, 200, 'Inherit height' );
  27. });
  28. test( 'ex3: width: 50%; height: 50%', function() {
  29. var size = getBoxSize(3);
  30. equal( size.width, 200, 'half width' );
  31. equal( size.height, 100, 'half height' );
  32. });
  33. test( 'ex4: border: 10px solid', function() {
  34. var size = getBoxSize(4);
  35. // console.log( size );
  36. equal( size.width, 220, 'width = 220 width' );
  37. equal( size.height, 120, 'height = 120 height' );
  38. equal( size.innerWidth, 200, 'innerWidth = 200 width' );
  39. equal( size.innerHeight, 100, 'innerHeight = 200 width' );
  40. equal( size.outerWidth, 220, 'outerWidth = 200 width + 10 border + 10 border' );
  41. equal( size.outerHeight, 120, 'outerHeight = 100 height + 10 border + 10 border' );
  42. });
  43. test( 'ex5: border: 10px solid; margin: 15px', function() {
  44. // margin: 10px 20px 30px 40px;
  45. var size = getBoxSize(5);
  46. // console.log( size );
  47. equal( size.width, 220, 'width = 220 width' );
  48. equal( size.height, 120, 'height = 120 height' );
  49. equal( size.marginTop, 10, 'marginTop' );
  50. equal( size.marginRight, 20, 'marginRight' );
  51. equal( size.marginBottom, 30, 'marginBottom' );
  52. equal( size.marginLeft, 40, 'marginLeft ' );
  53. equal( size.innerWidth, 200, 'innerWidth = 200 width' );
  54. equal( size.innerHeight, 100, 'innerHeight = 200 width' );
  55. equal( size.outerWidth, 280, 'outerWidth = 200 width + 20 border + 60 margin' );
  56. equal( size.outerHeight, 160, 'outerHeight = 100 height + 20 border + 40 margin' );
  57. });
  58. test( 'ex6: padding, set width/height', function() {
  59. var size = getBoxSize(6);
  60. // console.log( size );
  61. equal( size.width, 260, 'width' );
  62. equal( size.height, 140, 'height' );
  63. equal( size.innerWidth, 200, 'innerWidth = 200 width - 20 padding - 40 padding' );
  64. equal( size.innerHeight, 100, 'innerHeight = 200 height - 10 padding - 30 padding' );
  65. equal( size.outerWidth, 260, 'outerWidth' );
  66. equal( size.outerHeight, 140, 'outerHeight' );
  67. });
  68. test( 'ex7: padding, inherit width', function() {
  69. // padding: 10px 20px 30px 40px;
  70. var size = getBoxSize(7);
  71. // console.log( size );
  72. equal( size.width, 400, 'width' );
  73. equal( size.height, 140, 'height' );
  74. equal( size.paddingTop, 10, 'paddingTop' );
  75. equal( size.paddingRight, 20, 'paddingRight' );
  76. equal( size.paddingBottom, 30, 'paddingBottom' );
  77. equal( size.paddingLeft, 40, 'paddingLeft ' );
  78. equal( size.innerWidth, 340, 'innerWidth = 400 width - 20 padding - 40 padding' );
  79. equal( size.innerHeight, 100, 'innerHeight = 200 height - 10 padding - 30 padding' );
  80. equal( size.outerWidth, 400, 'outerWidth' );
  81. equal( size.outerHeight, 140, 'outerHeight' );
  82. });
  83. test( 'ex8: 66.666% values', function() {
  84. var size = getBoxSize(8);
  85. if ( size.width % 1 ) {
  86. ok( size.width > 266.6 && size.width < 266.7, 'width is between 266.6 and 266.7' );
  87. } else {
  88. // IE8 and Safari
  89. equal( size.width, 267, 'width is 267' );
  90. }
  91. if ( size.height % 1 ) {
  92. ok( size.height > 133.3 && size.height < 133.4, 'height is between 133.3 and 133.4' );
  93. } else {
  94. // IE8
  95. equal( size.height, 133, 'width is 133' );
  96. }
  97. });
  98. test( 'ex9: border-box', function() {
  99. var size = getBoxSize(9);
  100. equal( size.isBorderBox, true, 'isBorderBox' );
  101. equal( size.width, 400, 'width' );
  102. equal( size.height, 200, 'height' );
  103. equal( size.innerWidth, 280, 'innerWidth' );
  104. equal( size.innerHeight, 120, 'innerHeight' );
  105. equal( size.outerWidth, 400, 'outerWidth' );
  106. equal( size.outerHeight, 200, 'outerHeight' );
  107. });
  108. test( 'display: none', function() {
  109. var size = getSize( document.querySelector('#hidden .box1') );
  110. strictEqual( size.width, 0, 'width' );
  111. strictEqual( size.height, 0, 'height' );
  112. strictEqual( size.innerWidth, 0, 'innerWidth' );
  113. strictEqual( size.innerHeight, 0, 'innerHeight' );
  114. strictEqual( size.outerWidth, 0, 'outerWidth' );
  115. strictEqual( size.outerHeight, 0, 'outerHeight' );
  116. size.width = 300;
  117. size = getSize( document.querySelector('#hidden .box2') );
  118. strictEqual( size.width, 0, 'cannot over write zeroSize' );
  119. });
  120. test( 'percent values', function() {
  121. var size = getSize( document.querySelector('#percent .box') );
  122. strictEqual( size.marginLeft, 40, 'marginLeft' );
  123. strictEqual( size.marginTop, 80, 'marginTop' );
  124. strictEqual( size.width, 200, 'width' );
  125. strictEqual( size.height, 100, 'height' );
  126. strictEqual( size.innerWidth, 200, 'innerWidth' );
  127. strictEqual( size.innerHeight, 100, 'innerHeight' );
  128. strictEqual( size.outerWidth, 240, 'outerWidth' );
  129. strictEqual( size.outerHeight, 180, 'outerHeight' );
  130. });
  131. })( window );