scrollbar.spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. import { clearBodyAndDocument, clearFixture, getFixture } from '../../helpers/fixture'
  2. import Manipulator from '../../../src/dom/manipulator'
  3. import ScrollBarHelper from '../../../src/util/scrollbar'
  4. describe('ScrollBar', () => {
  5. let fixtureEl
  6. const doc = document.documentElement
  7. const parseInt = arg => Number.parseInt(arg, 10)
  8. const getPaddingX = el => parseInt(window.getComputedStyle(el).paddingRight)
  9. const getMarginX = el => parseInt(window.getComputedStyle(el).marginRight)
  10. const getOverFlow = el => el.style.overflow
  11. const getPaddingAttr = el => Manipulator.getDataAttribute(el, 'padding-right')
  12. const getMarginAttr = el => Manipulator.getDataAttribute(el, 'margin-right')
  13. const getOverFlowAttr = el => Manipulator.getDataAttribute(el, 'overflow')
  14. const windowCalculations = () => {
  15. return {
  16. htmlClient: document.documentElement.clientWidth,
  17. htmlOffset: document.documentElement.offsetWidth,
  18. docClient: document.body.clientWidth,
  19. htmlBound: document.documentElement.getBoundingClientRect().width,
  20. bodyBound: document.body.getBoundingClientRect().width,
  21. window: window.innerWidth,
  22. width: Math.abs(window.innerWidth - document.documentElement.clientWidth)
  23. }
  24. }
  25. const isScrollBarHidden = () => { // IOS devices, Android devices and Browsers on Mac, hide scrollbar by default and appear it, only while scrolling. So the tests for scrollbar would fail
  26. const calc = windowCalculations()
  27. return calc.htmlClient === calc.htmlOffset && calc.htmlClient === calc.window
  28. }
  29. beforeAll(() => {
  30. fixtureEl = getFixture()
  31. // custom fixture to avoid extreme style values
  32. fixtureEl.removeAttribute('style')
  33. })
  34. afterAll(() => {
  35. fixtureEl.remove()
  36. })
  37. afterEach(() => {
  38. clearFixture()
  39. clearBodyAndDocument()
  40. })
  41. beforeEach(() => {
  42. clearBodyAndDocument()
  43. })
  44. describe('isBodyOverflowing', () => {
  45. it('should return true if body is overflowing', () => {
  46. document.documentElement.style.overflowY = 'scroll'
  47. document.body.style.overflowY = 'scroll'
  48. fixtureEl.innerHTML = [
  49. '<div style="height: 110vh; width: 100%"></div>'
  50. ].join('')
  51. const result = new ScrollBarHelper().isOverflowing()
  52. if (isScrollBarHidden()) {
  53. expect(result).toEqual(false)
  54. } else {
  55. expect(result).toEqual(true)
  56. }
  57. })
  58. it('should return false if body is not overflowing', () => {
  59. doc.style.overflowY = 'hidden'
  60. document.body.style.overflowY = 'hidden'
  61. fixtureEl.innerHTML = [
  62. '<div style="height: 110vh; width: 100%"></div>'
  63. ].join('')
  64. const scrollBar = new ScrollBarHelper()
  65. const result = scrollBar.isOverflowing()
  66. expect(result).toEqual(false)
  67. })
  68. })
  69. describe('getWidth', () => {
  70. it('should return an integer greater than zero, if body is overflowing', () => {
  71. doc.style.overflowY = 'scroll'
  72. document.body.style.overflowY = 'scroll'
  73. fixtureEl.innerHTML = [
  74. '<div style="height: 110vh; width: 100%"></div>'
  75. ].join('')
  76. const result = new ScrollBarHelper().getWidth()
  77. if (isScrollBarHidden()) {
  78. expect(result).toBe(0)
  79. } else {
  80. expect(result).toBeGreaterThan(1)
  81. }
  82. })
  83. it('should return 0 if body is not overflowing', () => {
  84. document.documentElement.style.overflowY = 'hidden'
  85. document.body.style.overflowY = 'hidden'
  86. fixtureEl.innerHTML = [
  87. '<div style="height: 110vh; width: 100%"></div>'
  88. ].join('')
  89. const result = new ScrollBarHelper().getWidth()
  90. expect(result).toEqual(0)
  91. })
  92. })
  93. describe('hide - reset', () => {
  94. it('should adjust the inline padding of fixed elements which are full-width', done => {
  95. fixtureEl.innerHTML = [
  96. '<div style="height: 110vh; width: 100%">' +
  97. '<div class="fixed-top" id="fixed1" style="padding-right: 0px; width: 100vw"></div>',
  98. '<div class="fixed-top" id="fixed2" style="padding-right: 5px; width: 100vw"></div>',
  99. '</div>'
  100. ].join('')
  101. doc.style.overflowY = 'scroll'
  102. const fixedEl = fixtureEl.querySelector('#fixed1')
  103. const fixedEl2 = fixtureEl.querySelector('#fixed2')
  104. const originalPadding = getPaddingX(fixedEl)
  105. const originalPadding2 = getPaddingX(fixedEl2)
  106. const scrollBar = new ScrollBarHelper()
  107. const expectedPadding = originalPadding + scrollBar.getWidth()
  108. const expectedPadding2 = originalPadding2 + scrollBar.getWidth()
  109. scrollBar.hide()
  110. let currentPadding = getPaddingX(fixedEl)
  111. let currentPadding2 = getPaddingX(fixedEl2)
  112. expect(getPaddingAttr(fixedEl)).toEqual(`${originalPadding}px`, 'original fixed element padding should be stored in data-bs-padding-right')
  113. expect(getPaddingAttr(fixedEl2)).toEqual(`${originalPadding2}px`, 'original fixed element padding should be stored in data-bs-padding-right')
  114. expect(currentPadding).toEqual(expectedPadding, 'fixed element padding should be adjusted while opening')
  115. expect(currentPadding2).toEqual(expectedPadding2, 'fixed element padding should be adjusted while opening')
  116. scrollBar.reset()
  117. currentPadding = getPaddingX(fixedEl)
  118. currentPadding2 = getPaddingX(fixedEl2)
  119. expect(getPaddingAttr(fixedEl)).toEqual(null, 'data-bs-padding-right should be cleared after closing')
  120. expect(getPaddingAttr(fixedEl2)).toEqual(null, 'data-bs-padding-right should be cleared after closing')
  121. expect(currentPadding).toEqual(originalPadding, 'fixed element padding should be reset after closing')
  122. expect(currentPadding2).toEqual(originalPadding2, 'fixed element padding should be reset after closing')
  123. done()
  124. })
  125. it('should adjust the inline margin and padding of sticky elements', done => {
  126. fixtureEl.innerHTML = [
  127. '<div style="height: 110vh">' +
  128. '<div class="sticky-top" style="margin-right: 10px; padding-right: 20px; width: 100vw; height: 10px"></div>',
  129. '</div>'
  130. ].join('')
  131. doc.style.overflowY = 'scroll'
  132. const stickyTopEl = fixtureEl.querySelector('.sticky-top')
  133. const originalMargin = getMarginX(stickyTopEl)
  134. const originalPadding = getPaddingX(stickyTopEl)
  135. const scrollBar = new ScrollBarHelper()
  136. const expectedMargin = originalMargin - scrollBar.getWidth()
  137. const expectedPadding = originalPadding + scrollBar.getWidth()
  138. scrollBar.hide()
  139. expect(getMarginAttr(stickyTopEl)).toEqual(`${originalMargin}px`, 'original sticky element margin should be stored in data-bs-margin-right')
  140. expect(getMarginX(stickyTopEl)).toEqual(expectedMargin, 'sticky element margin should be adjusted while opening')
  141. expect(getPaddingAttr(stickyTopEl)).toEqual(`${originalPadding}px`, 'original sticky element margin should be stored in data-bs-margin-right')
  142. expect(getPaddingX(stickyTopEl)).toEqual(expectedPadding, 'sticky element margin should be adjusted while opening')
  143. scrollBar.reset()
  144. expect(getMarginAttr(stickyTopEl)).toEqual(null, 'data-bs-margin-right should be cleared after closing')
  145. expect(getMarginX(stickyTopEl)).toEqual(originalMargin, 'sticky element margin should be reset after closing')
  146. expect(getPaddingAttr(stickyTopEl)).toEqual(null, 'data-bs-margin-right should be cleared after closing')
  147. expect(getPaddingX(stickyTopEl)).toEqual(originalPadding, 'sticky element margin should be reset after closing')
  148. done()
  149. })
  150. it('should not adjust the inline margin and padding of sticky and fixed elements when element do not have full width', () => {
  151. fixtureEl.innerHTML = [
  152. '<div class="sticky-top" style="margin-right: 0px; padding-right: 0px; width: 50vw"></div>'
  153. ].join('')
  154. const stickyTopEl = fixtureEl.querySelector('.sticky-top')
  155. const originalMargin = getMarginX(stickyTopEl)
  156. const originalPadding = getPaddingX(stickyTopEl)
  157. const scrollBar = new ScrollBarHelper()
  158. scrollBar.hide()
  159. const currentMargin = getMarginX(stickyTopEl)
  160. const currentPadding = getPaddingX(stickyTopEl)
  161. expect(currentMargin).toEqual(originalMargin, 'sticky element\'s margin should not be adjusted while opening')
  162. expect(currentPadding).toEqual(originalPadding, 'sticky element\'s padding should not be adjusted while opening')
  163. scrollBar.reset()
  164. })
  165. it('should not put data-attribute if element doesn\'t have the proper style property, should just remove style property if element didn\'t had one', () => {
  166. fixtureEl.innerHTML = [
  167. '<div style="height: 110vh; width: 100%">' +
  168. '<div class="sticky-top" id="sticky" style="width: 100vw"></div>',
  169. '</div>'
  170. ].join('')
  171. document.body.style.overflowY = 'scroll'
  172. const scrollBar = new ScrollBarHelper()
  173. const hasPaddingAttr = el => el.hasAttribute('data-bs-padding-right')
  174. const hasMarginAttr = el => el.hasAttribute('data-bs-margin-right')
  175. const stickyEl = fixtureEl.querySelector('#sticky')
  176. const originalPadding = getPaddingX(stickyEl)
  177. const originalMargin = getMarginX(stickyEl)
  178. const scrollBarWidth = scrollBar.getWidth()
  179. scrollBar.hide()
  180. expect(getPaddingX(stickyEl)).toEqual(scrollBarWidth + originalPadding)
  181. const expectedMargin = scrollBarWidth + originalMargin
  182. expect(getMarginX(stickyEl)).toEqual(expectedMargin === 0 ? expectedMargin : -expectedMargin)
  183. expect(hasMarginAttr(stickyEl)).toBeFalse() // We do not have to keep css margin
  184. expect(hasPaddingAttr(stickyEl)).toBeFalse() // We do not have to keep css padding
  185. scrollBar.reset()
  186. expect(getPaddingX(stickyEl)).toEqual(originalPadding)
  187. expect(getPaddingX(stickyEl)).toEqual(originalPadding)
  188. })
  189. describe('Body Handling', () => {
  190. it('should ignore other inline styles when trying to restore body defaults ', () => {
  191. document.body.style.color = 'red'
  192. const scrollBar = new ScrollBarHelper()
  193. const scrollBarWidth = scrollBar.getWidth()
  194. scrollBar.hide()
  195. expect(getPaddingX(document.body)).toEqual(scrollBarWidth, 'body does not have inline padding set')
  196. expect(document.body.style.color).toEqual('red', 'body still has other inline styles set')
  197. scrollBar.reset()
  198. })
  199. it('should hide scrollbar and reset it to its initial value', () => {
  200. const styleSheetPadding = '7px'
  201. fixtureEl.innerHTML = [
  202. '<style>',
  203. ' body {',
  204. ` padding-right: ${styleSheetPadding} }`,
  205. ' }',
  206. '</style>'
  207. ].join('')
  208. const el = document.body
  209. const inlineStylePadding = '10px'
  210. el.style.paddingRight = inlineStylePadding
  211. const originalPadding = getPaddingX(el)
  212. expect(originalPadding).toEqual(parseInt(inlineStylePadding)) // Respect only the inline style as it has prevails this of css
  213. const originalOverFlow = 'auto'
  214. el.style.overflow = originalOverFlow
  215. const scrollBar = new ScrollBarHelper()
  216. const scrollBarWidth = scrollBar.getWidth()
  217. scrollBar.hide()
  218. const currentPadding = getPaddingX(el)
  219. expect(currentPadding).toEqual(scrollBarWidth + originalPadding)
  220. expect(currentPadding).toEqual(scrollBarWidth + parseInt(inlineStylePadding))
  221. expect(getPaddingAttr(el)).toEqual(inlineStylePadding)
  222. expect(getOverFlow(el)).toEqual('hidden')
  223. expect(getOverFlowAttr(el)).toEqual(originalOverFlow)
  224. scrollBar.reset()
  225. const currentPadding1 = getPaddingX(el)
  226. expect(currentPadding1).toEqual(originalPadding)
  227. expect(getPaddingAttr(el)).toEqual(null)
  228. expect(getOverFlow(el)).toEqual(originalOverFlow)
  229. expect(getOverFlowAttr(el)).toEqual(null)
  230. })
  231. it('should hide scrollbar and reset it to its initial value - respecting css rules', () => {
  232. const styleSheetPadding = '7px'
  233. fixtureEl.innerHTML = [
  234. '<style>',
  235. ' body {',
  236. ` padding-right: ${styleSheetPadding} }`,
  237. ' }',
  238. '</style>'
  239. ].join('')
  240. const el = document.body
  241. const originalPadding = getPaddingX(el)
  242. const originalOverFlow = 'scroll'
  243. el.style.overflow = originalOverFlow
  244. const scrollBar = new ScrollBarHelper()
  245. const scrollBarWidth = scrollBar.getWidth()
  246. scrollBar.hide()
  247. const currentPadding = getPaddingX(el)
  248. expect(currentPadding).toEqual(scrollBarWidth + originalPadding)
  249. expect(currentPadding).toEqual(scrollBarWidth + parseInt(styleSheetPadding))
  250. expect(getPaddingAttr(el)).toBeNull() // We do not have to keep css padding
  251. expect(getOverFlow(el)).toEqual('hidden')
  252. expect(getOverFlowAttr(el)).toEqual(originalOverFlow)
  253. scrollBar.reset()
  254. const currentPadding1 = getPaddingX(el)
  255. expect(currentPadding1).toEqual(originalPadding)
  256. expect(getPaddingAttr(el)).toEqual(null)
  257. expect(getOverFlow(el)).toEqual(originalOverFlow)
  258. expect(getOverFlowAttr(el)).toEqual(null)
  259. })
  260. it('should not adjust the inline body padding when it does not overflow', () => {
  261. const originalPadding = getPaddingX(document.body)
  262. const scrollBar = new ScrollBarHelper()
  263. // Hide scrollbars to prevent the body overflowing
  264. doc.style.overflowY = 'hidden'
  265. doc.style.paddingRight = '0px'
  266. scrollBar.hide()
  267. const currentPadding = getPaddingX(document.body)
  268. expect(currentPadding).toEqual(originalPadding, 'body padding should not be adjusted')
  269. scrollBar.reset()
  270. })
  271. it('should not adjust the inline body padding when it does not overflow, even on a scaled display', () => {
  272. const originalPadding = getPaddingX(document.body)
  273. const scrollBar = new ScrollBarHelper()
  274. // Remove body margins as would be done by Bootstrap css
  275. document.body.style.margin = '0'
  276. // Hide scrollbars to prevent the body overflowing
  277. doc.style.overflowY = 'hidden'
  278. // Simulate a discrepancy between exact, i.e. floating point body width, and rounded body width
  279. // as it can occur when zooming or scaling the display to something else than 100%
  280. doc.style.paddingRight = '.48px'
  281. scrollBar.hide()
  282. const currentPadding = getPaddingX(document.body)
  283. expect(currentPadding).toEqual(originalPadding, 'body padding should not be adjusted')
  284. scrollBar.reset()
  285. })
  286. })
  287. })
  288. })