2021-01-14 17:01:37 +08:00
|
|
|
import { ref } from 'vue'
|
2022-02-11 11:03:15 +08:00
|
|
|
import { on, off } from '@element-plus/utils'
|
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'
|
|
|
|
const evt = jest.fn()
|
|
|
|
beforeAll(() => {
|
|
|
|
on(document.body, evtName, evt)
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
evt.mockClear()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
off(document.body, evtName, evt)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should prevent global event from happening', () => {
|
|
|
|
const visible = ref(true)
|
|
|
|
const evt2Trigger = jest.fn().mockReturnValue(true)
|
|
|
|
usePreventGlobal(visible, evtName, evt2Trigger)
|
|
|
|
|
|
|
|
triggerEvent(document.body, evtName)
|
|
|
|
|
|
|
|
expect(evt).not.toBeCalled()
|
|
|
|
expect(evt2Trigger).toHaveBeenCalled()
|
|
|
|
visible.value = false
|
|
|
|
// clean up
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not prevent global event from happening', () => {
|
|
|
|
const visible = ref(true)
|
|
|
|
const evt2Trigger = jest.fn().mockReturnValue(false)
|
|
|
|
usePreventGlobal(visible, evtName, evt2Trigger)
|
|
|
|
|
|
|
|
triggerEvent(document.body, evtName)
|
|
|
|
|
|
|
|
expect(evt).toHaveBeenCalled()
|
|
|
|
expect(evt2Trigger).toHaveBeenCalled()
|
|
|
|
|
|
|
|
visible.value = false
|
|
|
|
})
|
|
|
|
})
|