You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.7 KiB
66 lines
1.7 KiB
// @vitest-environment jsdom
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { BodyScrollLock } from './body-scroll-lock.svelte'
|
|
|
|
describe('BodyScrollLock', () => {
|
|
beforeEach(() => {
|
|
vi.useFakeTimers()
|
|
BodyScrollLock.reset()
|
|
document.body.setAttribute('style', 'background: red;')
|
|
Object.defineProperty(window, 'innerWidth', {
|
|
configurable: true,
|
|
writable: true,
|
|
value: 1200
|
|
})
|
|
Object.defineProperty(document.documentElement, 'clientWidth', {
|
|
configurable: true,
|
|
value: 1180
|
|
})
|
|
})
|
|
|
|
afterEach(() => {
|
|
BodyScrollLock.reset()
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
it('locks body scroll and restores the initial body style when unlocked', async () => {
|
|
const lock = new BodyScrollLock(true)
|
|
await Promise.resolve()
|
|
|
|
expect(document.body.style.overflow).toBe('hidden')
|
|
expect(document.body.style.getPropertyValue('--scrollbar-width')).toBe('20px')
|
|
|
|
lock.locked.current = false
|
|
vi.runAllTimers()
|
|
await Promise.resolve()
|
|
|
|
expect(document.body.getAttribute('style')).toBe('background: red;')
|
|
})
|
|
|
|
it('keeps the body locked until the last lock is released', async () => {
|
|
const first = new BodyScrollLock(true)
|
|
const second = new BodyScrollLock(true)
|
|
await Promise.resolve()
|
|
|
|
first.locked.current = false
|
|
vi.runAllTimers()
|
|
await Promise.resolve()
|
|
|
|
expect(document.body.style.overflow).toBe('hidden')
|
|
|
|
second.destroy()
|
|
vi.runAllTimers()
|
|
await Promise.resolve()
|
|
|
|
expect(document.body.getAttribute('style')).toBe('background: red;')
|
|
})
|
|
|
|
it('allows creating unlocked instances without mutating the body', () => {
|
|
new BodyScrollLock(false)
|
|
|
|
expect(document.body.getAttribute('style')).toBe('background: red;')
|
|
})
|
|
})
|