mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-16 02:11:48 +08:00
aaf90d99d0
* test: use vitest * test: add script and ci * chore: improve tsconfig * refactor: use-form-item * fix: remove unused * chore: improve scripts * test: improve mock * refactor: change coverage
54 lines
1.2 KiB
TypeScript
54 lines
1.2 KiB
TypeScript
import { ref } from 'vue'
|
|
import {
|
|
describe,
|
|
it,
|
|
expect,
|
|
beforeAll,
|
|
beforeEach,
|
|
afterAll,
|
|
fn,
|
|
} from 'vitest'
|
|
import triggerEvent from '@element-plus/test-utils/trigger-event'
|
|
import { usePreventGlobal } from '../use-prevent-global'
|
|
|
|
describe('usePreventGlobal', () => {
|
|
const evtName = 'keydown'
|
|
const evtHandler = fn()
|
|
beforeAll(() => {
|
|
document.body.addEventListener(evtName, evtHandler)
|
|
})
|
|
|
|
beforeEach(() => {
|
|
evtHandler.mockClear()
|
|
})
|
|
|
|
afterAll(() => {
|
|
document.body.removeEventListener(evtName, evtHandler)
|
|
})
|
|
|
|
it('should prevent global event from happening', () => {
|
|
const visible = ref(true)
|
|
const evt2Trigger = fn().mockReturnValue(true)
|
|
usePreventGlobal(visible, evtName, evt2Trigger)
|
|
|
|
triggerEvent(document.body, evtName)
|
|
|
|
expect(evtHandler).not.toBeCalled()
|
|
expect(evt2Trigger).toHaveBeenCalled()
|
|
visible.value = false
|
|
})
|
|
|
|
it('should not prevent global event from happening', () => {
|
|
const visible = ref(true)
|
|
const evt2Trigger = fn().mockReturnValue(false)
|
|
usePreventGlobal(visible, evtName, evt2Trigger)
|
|
|
|
triggerEvent(document.body, evtName)
|
|
|
|
expect(evtHandler).toHaveBeenCalled()
|
|
expect(evt2Trigger).toHaveBeenCalled()
|
|
|
|
visible.value = false
|
|
})
|
|
})
|