2021-01-14 17:01:37 +08:00
|
|
|
import { ref } from 'vue'
|
2022-02-21 14:28:22 +08:00
|
|
|
import {
|
2022-03-25 15:35:56 +08:00
|
|
|
afterAll,
|
2022-02-21 14:28:22 +08:00
|
|
|
beforeAll,
|
|
|
|
beforeEach,
|
2022-03-25 15:35:56 +08:00
|
|
|
describe,
|
|
|
|
expect,
|
|
|
|
it,
|
2022-04-12 01:10:25 +08:00
|
|
|
vi,
|
2022-02-21 14:28:22 +08:00
|
|
|
} from 'vitest'
|
2021-01-14 17:01:37 +08:00
|
|
|
import triggerEvent from '@element-plus/test-utils/trigger-event'
|
2021-11-29 15:58:44 +08:00
|
|
|
import { usePreventGlobal } from '../use-prevent-global'
|
2021-01-14 17:01:37 +08:00
|
|
|
|
|
|
|
describe('usePreventGlobal', () => {
|
|
|
|
const evtName = 'keydown'
|
2022-04-12 01:10:25 +08:00
|
|
|
const evtHandler = vi.fn()
|
2021-01-14 17:01:37 +08:00
|
|
|
beforeAll(() => {
|
2022-02-21 14:28:22 +08:00
|
|
|
document.body.addEventListener(evtName, evtHandler)
|
2021-01-14 17:01:37 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-02-21 14:28:22 +08:00
|
|
|
evtHandler.mockClear()
|
2021-01-14 17:01:37 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
afterAll(() => {
|
2022-02-21 14:28:22 +08:00
|
|
|
document.body.removeEventListener(evtName, evtHandler)
|
2021-01-14 17:01:37 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
2021-01-14 17:01:37 +08:00
|
|
|
usePreventGlobal(visible, evtName, evt2Trigger)
|
|
|
|
|
|
|
|
triggerEvent(document.body, evtName)
|
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
expect(evtHandler).not.toBeCalled()
|
2021-01-14 17:01:37 +08:00
|
|
|
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)
|
2021-01-14 17:01:37 +08:00
|
|
|
usePreventGlobal(visible, evtName, evt2Trigger)
|
|
|
|
|
|
|
|
triggerEvent(document.body, evtName)
|
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
expect(evtHandler).toHaveBeenCalled()
|
2021-01-14 17:01:37 +08:00
|
|
|
expect(evt2Trigger).toHaveBeenCalled()
|
|
|
|
|
|
|
|
visible.value = false
|
|
|
|
})
|
|
|
|
})
|