element-plus/packages/hooks/__tests__/use-prevent-global.ts
三咲智子 55348b30b6
style: use prettier (#3228)
* style: use prettier

* style: just prettier format, no code changes

* style: eslint fix
object-shorthand, prefer-const

* style: fix no-void

* style: no-console
2021-09-04 19:29:28 +08:00

47 lines
1.2 KiB
TypeScript

import { ref } from 'vue'
import { on, off } from '@element-plus/utils/dom'
import triggerEvent from '@element-plus/test-utils/trigger-event'
import usePreventGlobal from '../use-prevent-global'
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
})
})