feat(click-outside): return mousedown event (#2038)

* feat(click-outside):  return mousedown event

Return mousedown event for binding function.

* feat(click-outside): expose both mouseup and mousedown event

* feat(directives): add test cases for click-outside

* feat(directives):  clear the previously assigned event object
This commit is contained in:
BelinChung 2021-05-24 17:29:19 +08:00 committed by GitHub
parent 886f2a3075
commit 0bb128315b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -4,7 +4,15 @@ import ClickOutside from '../click-outside'
const AXIOM = 'Rem is the best girl'
const TRIGGER = 'trigger'
const OTHER_CLASS = 'other-class'
const handler = jest.fn()
// init some local variables
let mousedownObject
let mouseupObject
// mock handler with implementations which makes assignment to the local variable that we registered above.
const handler = jest.fn((eMouseup, eMousedown) => {
mouseupObject = eMouseup
mousedownObject = eMousedown
})
const Component = {
template: `
<div>
@ -29,6 +37,10 @@ const _mount = () => mount(Component, {
describe('Directives.vue', () => {
beforeEach(() => {
// clear the previously assigned event object
mousedownObject = null
mouseupObject = null
handler.mockClear()
})
test('render test', () => {
@ -47,11 +59,15 @@ describe('Directives.vue', () => {
const mousedown = document.createEvent('MouseEvents')
mousedown.initMouseEvent('mousedown', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
document.dispatchEvent(mousedown)
const mouseup = document.createEvent('MouseEvents')
mouseup.initMouseEvent('mouseup', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
document.dispatchEvent(mouseup)
expect(handler).toHaveBeenCalledTimes(1)
// here is the different part
// we test the existence of the local variable.
expect(mousedownObject).toBeDefined()
expect(mouseupObject).toBeDefined()
})
})

View File

@ -71,7 +71,7 @@ function createDocumentHandler(
) {
return
}
binding.value()
binding.value(mouseup, mousedown)
}
}