2020-09-16 14:49:21 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2022-07-21 16:22:50 +08:00
|
|
|
import { describe, expect, it, vi } from 'vitest'
|
2022-04-19 12:46:57 +08:00
|
|
|
import sleep from '@element-plus/test-utils/sleep'
|
2020-09-16 14:49:21 +08:00
|
|
|
import RepeatClick from '../repeat-click'
|
|
|
|
|
2022-07-21 16:22:50 +08:00
|
|
|
let handler: ReturnType<typeof vi.fn>
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
const _mount = () =>
|
2022-07-12 19:42:38 +08:00
|
|
|
mount(
|
|
|
|
{
|
|
|
|
setup() {
|
2022-07-21 16:22:50 +08:00
|
|
|
handler = vi.fn()
|
2022-07-12 19:42:38 +08:00
|
|
|
return () => (
|
|
|
|
<div id="block" v-repeat-click={handler}>
|
|
|
|
TEST
|
|
|
|
</div>
|
|
|
|
)
|
2021-09-04 19:29:28 +08:00
|
|
|
},
|
|
|
|
},
|
2022-07-12 19:42:38 +08:00
|
|
|
{
|
|
|
|
global: {
|
|
|
|
directives: {
|
|
|
|
RepeatClick,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2020-09-16 14:49:21 +08:00
|
|
|
|
|
|
|
describe('Directives.vue', () => {
|
2022-07-21 16:22:50 +08:00
|
|
|
it('click test', async () => {
|
2020-09-16 14:49:21 +08:00
|
|
|
const wrapper = _mount()
|
|
|
|
const block = wrapper.find('#block')
|
2022-07-21 16:22:50 +08:00
|
|
|
|
2020-09-16 14:49:21 +08:00
|
|
|
block.trigger('mousedown')
|
2022-07-21 16:22:50 +08:00
|
|
|
await sleep(330)
|
|
|
|
document.dispatchEvent(new MouseEvent('mouseup'))
|
|
|
|
|
|
|
|
expect(handler).toHaveBeenCalledTimes(3)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('time interval between mousedown and mouseup is slightly less than 100ms', async () => {
|
|
|
|
const wrapper = _mount()
|
|
|
|
const block = wrapper.find('#block')
|
|
|
|
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
block.trigger('mousedown')
|
|
|
|
await sleep(99)
|
|
|
|
document.dispatchEvent(new MouseEvent('mouseup'))
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(handler).toHaveBeenCalledTimes(10)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('time interval between mousedown and mouseup is slightly more than 100ms', async () => {
|
|
|
|
const wrapper = _mount()
|
|
|
|
const block = wrapper.find('#block')
|
|
|
|
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
block.trigger('mousedown')
|
|
|
|
await sleep(101)
|
|
|
|
document.dispatchEvent(new MouseEvent('mouseup'))
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(handler).toHaveBeenCalledTimes(10)
|
2020-09-16 14:49:21 +08:00
|
|
|
})
|
|
|
|
})
|