forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(useScrollLock): initialOverflow is not working (vueuse#3798)
- Loading branch information
Showing
2 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
import { defineComponent, h } from 'vue-demi' | ||
import { mount } from '../../.test' | ||
import { useScrollLock } from '.' | ||
|
||
describe('useScrollLock', () => { | ||
let targetEl: HTMLElement | ||
|
||
beforeEach(() => { | ||
targetEl = document.createElement('div') | ||
}) | ||
|
||
it('should lock the scroll', () => { | ||
const isLock = useScrollLock(targetEl) | ||
|
||
isLock.value = true | ||
expect(targetEl.style.overflow).toBe('hidden') | ||
|
||
isLock.value = false | ||
expect(targetEl.style.overflow).toBe('') | ||
}) | ||
|
||
it('should cache the initial overflow setting', () => { | ||
targetEl.style.overflow = 'auto' | ||
|
||
const isLock = useScrollLock(targetEl) | ||
|
||
isLock.value = true | ||
expect(targetEl.style.overflow).toBe('hidden') | ||
|
||
isLock.value = false | ||
expect(targetEl.style.overflow).toBe('auto') | ||
}) | ||
|
||
it('automatically unlocks on component unmount', async () => { | ||
const vm = mount(defineComponent({ | ||
setup() { | ||
const isLock = useScrollLock(targetEl) | ||
|
||
return { isLock } | ||
}, | ||
render() { | ||
return h('div') | ||
}, | ||
})) | ||
|
||
vm.isLock = true | ||
expect(targetEl.style.overflow).toBe('hidden') | ||
|
||
vm.unmount() | ||
expect(targetEl.style.overflow).toBe('') | ||
}) | ||
|
||
it('handles touchmove event on IOS devices', () => { | ||
vi.mock('@vueuse/shared', async () => { | ||
const actual = await vi.importActual('@vueuse/shared') | ||
return { | ||
...actual, | ||
isIOS: true, | ||
} | ||
}) | ||
|
||
const addEventListener = vi.spyOn(targetEl, 'addEventListener') | ||
const removeEventListener = vi.spyOn(targetEl, 'removeEventListener') | ||
const isLock = useScrollLock(targetEl) | ||
|
||
expect(addEventListener).toBeCalledTimes(0) | ||
|
||
isLock.value = true | ||
expect(addEventListener).toBeCalledTimes(1) | ||
expect(removeEventListener).toBeCalledTimes(0) | ||
|
||
isLock.value = false | ||
expect(removeEventListener).toBeCalledTimes(1) | ||
}) | ||
|
||
it('multiple instances point at the same element, will share the same initialOverflow', () => { | ||
const isLock1 = useScrollLock(targetEl) | ||
const isLock2 = useScrollLock(targetEl) | ||
|
||
isLock1.value = true | ||
isLock2.value = true | ||
expect(targetEl.style.overflow).toBe('hidden') | ||
|
||
isLock2.value = false | ||
expect(targetEl.style.overflow).toBe('') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters