getClientRect.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { getStockScrollbarWidths } from '../lib/dom-misc'
  2. describe('getClientRect', function() {
  3. var getClientRect = $.fullCalendar.getClientRect
  4. defineTests(
  5. 'when margin',
  6. { margin: '5px 10px' },
  7. { width: 100, height: 100 },
  8. { width: 100, height: 100 }
  9. )
  10. defineTests(
  11. 'when padding',
  12. { padding: '5px 10px' },
  13. { width: 100, height: 100 },
  14. { width: 120, height: 110 }
  15. )
  16. /// / getClientRect doesn't work with borders anymore
  17. // defineTests(
  18. // 'when border',
  19. // { border: '5px solid red' },
  20. // { width: 100, height: 100 },
  21. // { width: 100, height: 100 }
  22. // );
  23. // defineTests(
  24. // 'when border and padding',
  25. // { border: '5px solid red', padding: '5px 10px' },
  26. // { width: 100, height: 100 },
  27. // { width: 120, height: 110 }
  28. // );
  29. function defineTests(description, cssProps, innerDims, dims) {
  30. describe(description, function() {
  31. describe('when no scrolling', function() {
  32. describe('when LTR', function() {
  33. defineTest(false, 'ltr', cssProps, innerDims, dims)
  34. })
  35. describe('when RTL', function() {
  36. defineTest(false, 'rtl', cssProps, innerDims, dims)
  37. })
  38. })
  39. describe('when scrolling', function() {
  40. describe('when LTR', function() {
  41. defineTest(true, 'ltr', cssProps, innerDims, dims)
  42. })
  43. describe('when RTL', function() {
  44. defineTest(true, 'rtl', cssProps, innerDims, dims)
  45. })
  46. })
  47. })
  48. }
  49. function defineTest(isScrolling, dir, cssProps, innerDims, dims) {
  50. it('computes correct dimensions', function() {
  51. var el = $(
  52. '<div style="position:absolute" />'
  53. )
  54. .css('overflow', isScrolling ? 'scroll' : 'hidden')
  55. .css('direction', dir)
  56. .css(cssProps)
  57. .append(
  58. $('<div style="position:relative" />').css(innerDims)
  59. )
  60. .appendTo('body')
  61. var rect = getClientRect(el)
  62. var offset = el.offset()
  63. var borderLeftWidth = parseFloat(el.css('border-left-width')) || 0
  64. var borderTopWidth = parseFloat(el.css('border-top-width')) || 0
  65. var scrollbarWidths
  66. if (isScrolling) {
  67. scrollbarWidths = getStockScrollbarWidths(dir)
  68. } else {
  69. scrollbarWidths = { left: 0, right: 0, top: 0, bottom: 0 }
  70. }
  71. expect(rect.left).toBe(offset.left + borderLeftWidth + scrollbarWidths.left)
  72. expect(rect.top).toBe(offset.top + borderTopWidth + scrollbarWidths.top)
  73. expect(rect.right - rect.left).toBe(dims.width)
  74. expect(rect.bottom - rect.top).toBe(dims.height)
  75. el.remove()
  76. })
  77. }
  78. })