element-plus/packages/hooks/__tests__/use-prevent-global.test.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

import { ref } from 'vue'
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import triggerEvent from '@element-plus/test-utils/trigger-event'
import { usePreventGlobal } from '../use-prevent-global'
describe('usePreventGlobal', () => {
const evtName = 'keydown'
2022-04-12 01:10:25 +08:00
const evtHandler = vi.fn()
beforeAll(() => {
document.body.addEventListener(evtName, evtHandler)
})
afterAll(() => {
document.body.removeEventListener(evtName, evtHandler)
})
it('should prevent global event from happening', () => {
const visible = ref(true)
2022-04-12 01:10:25 +08:00
const evt2Trigger = vi.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)
2022-04-12 01:10:25 +08:00
const evt2Trigger = vi.fn().mockReturnValue(false)
usePreventGlobal(visible, evtName, evt2Trigger)
triggerEvent(document.body, evtName)
expect(evtHandler).toHaveBeenCalled()
expect(evt2Trigger).toHaveBeenCalled()
visible.value = false
})
})