|
1 | 1 | import * as Scrollbar from '../../../src/util/scrollbar'
|
2 |
| -import { clearFixture, getFixture } from '../../helpers/fixture' |
| 2 | +import { clearBodyAndDocument, clearFixture, getFixture } from '../../helpers/fixture' |
| 3 | +import Manipulator from '../../../src/dom/manipulator' |
3 | 4 |
|
4 | 5 | describe('ScrollBar', () => {
|
5 | 6 | let fixtureEl
|
| 7 | + const parseInt = arg => Number.parseInt(arg, 10) |
| 8 | + const getRightPadding = el => parseInt(window.getComputedStyle(el).paddingRight) |
| 9 | + const getOverFlow = el => el.style.overflow |
| 10 | + const getPaddingAttr = el => Manipulator.getDataAttribute(el, 'padding-right') |
| 11 | + const getOverFlowAttr = el => Manipulator.getDataAttribute(el, 'overflow') |
6 | 12 | const windowCalculations = () => {
|
7 | 13 | return {
|
8 | 14 | htmlClient: document.documentElement.clientWidth,
|
@@ -32,15 +38,11 @@ describe('ScrollBar', () => {
|
32 | 38 |
|
33 | 39 | afterEach(() => {
|
34 | 40 | clearFixture()
|
35 |
| - document.documentElement.removeAttribute('style') |
36 |
| - document.body.removeAttribute('style') |
37 |
| - document.body.removeAttribute('data-bs-padding-right') |
| 41 | + clearBodyAndDocument() |
38 | 42 | })
|
39 | 43 |
|
40 | 44 | beforeEach(() => {
|
41 |
| - document.documentElement.removeAttribute('style') |
42 |
| - document.body.removeAttribute('style') |
43 |
| - document.body.removeAttribute('data-bs-padding-right') |
| 45 | + clearBodyAndDocument() |
44 | 46 | })
|
45 | 47 |
|
46 | 48 | describe('isBodyOverflowing', () => {
|
@@ -180,5 +182,80 @@ describe('ScrollBar', () => {
|
180 | 182 |
|
181 | 183 | Scrollbar.reset()
|
182 | 184 | })
|
| 185 | + |
| 186 | + describe('Body Handling', () => { |
| 187 | + it('should hide scrollbar and reset it to its initial value', () => { |
| 188 | + const styleSheetPadding = '7px' |
| 189 | + fixtureEl.innerHTML = [ |
| 190 | + '<style>', |
| 191 | + ' body {', |
| 192 | + ` padding-right: ${styleSheetPadding} }`, |
| 193 | + ' }', |
| 194 | + '</style>' |
| 195 | + ].join('') |
| 196 | + |
| 197 | + const el = document.body |
| 198 | + const inlineStylePadding = '10px' |
| 199 | + el.style.paddingRight = inlineStylePadding |
| 200 | + |
| 201 | + const originalPadding = getRightPadding(el) |
| 202 | + expect(originalPadding).toEqual(parseInt(inlineStylePadding)) // Respect only the inline style as it has prevails this of css |
| 203 | + const originalOverFlow = 'auto' |
| 204 | + el.style.overflow = originalOverFlow |
| 205 | + const scrollBarWidth = Scrollbar.getWidth() |
| 206 | + |
| 207 | + Scrollbar.hide() |
| 208 | + |
| 209 | + const currentPadding = getRightPadding(el) |
| 210 | + |
| 211 | + expect(currentPadding).toEqual(scrollBarWidth + originalPadding) |
| 212 | + expect(currentPadding).toEqual(scrollBarWidth + parseInt(inlineStylePadding)) |
| 213 | + expect(getPaddingAttr(el)).toEqual(inlineStylePadding) |
| 214 | + expect(getOverFlow(el)).toEqual('hidden') |
| 215 | + expect(getOverFlowAttr(el)).toEqual(originalOverFlow) |
| 216 | + |
| 217 | + Scrollbar.reset() |
| 218 | + |
| 219 | + const currentPadding1 = getRightPadding(el) |
| 220 | + expect(currentPadding1).toEqual(originalPadding) |
| 221 | + expect(getPaddingAttr(el)).toEqual(null) |
| 222 | + expect(getOverFlow(el)).toEqual(originalOverFlow) |
| 223 | + expect(getOverFlowAttr(el)).toEqual(null) |
| 224 | + }) |
| 225 | + |
| 226 | + it('should hide scrollbar and reset it to its initial value - respecting css rules', () => { |
| 227 | + const styleSheetPadding = '7px' |
| 228 | + fixtureEl.innerHTML = [ |
| 229 | + '<style>', |
| 230 | + ' body {', |
| 231 | + ` padding-right: ${styleSheetPadding} }`, |
| 232 | + ' }', |
| 233 | + '</style>' |
| 234 | + ].join('') |
| 235 | + const el = document.body |
| 236 | + const originalPadding = getRightPadding(el) |
| 237 | + const originalOverFlow = 'scroll' |
| 238 | + el.style.overflow = originalOverFlow |
| 239 | + const scrollBarWidth = Scrollbar.getWidth() |
| 240 | + |
| 241 | + Scrollbar.hide() |
| 242 | + |
| 243 | + const currentPadding = getRightPadding(el) |
| 244 | + |
| 245 | + expect(currentPadding).toEqual(scrollBarWidth + originalPadding) |
| 246 | + expect(currentPadding).toEqual(scrollBarWidth + parseInt(styleSheetPadding)) |
| 247 | + expect(getPaddingAttr(el)).toBeNull() // We do not have to keep css padding |
| 248 | + expect(getOverFlow(el)).toEqual('hidden') |
| 249 | + expect(getOverFlowAttr(el)).toEqual(originalOverFlow) |
| 250 | + |
| 251 | + Scrollbar.reset() |
| 252 | + |
| 253 | + const currentPadding1 = getRightPadding(el) |
| 254 | + expect(currentPadding1).toEqual(originalPadding) |
| 255 | + expect(getPaddingAttr(el)).toEqual(null) |
| 256 | + expect(getOverFlow(el)).toEqual(originalOverFlow) |
| 257 | + expect(getOverFlowAttr(el)).toEqual(null) |
| 258 | + }) |
| 259 | + }) |
183 | 260 | })
|
184 | 261 | })
|
0 commit comments