repeat-click.test.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { mount } from '@vue/test-utils'
  2. import { describe, expect, it, vi } from 'vitest'
  3. import { REPEAT_DELAY, REPEAT_INTERVAL, vRepeatClick } from '../repeat-click'
  4. const PRESS_TIME = REPEAT_DELAY + REPEAT_INTERVAL
  5. let handler: ReturnType<typeof vi.fn>
  6. const _mount = () =>
  7. mount(
  8. {
  9. setup() {
  10. handler = vi.fn()
  11. return () => (
  12. <div id="block" v-repeat-click={handler}>
  13. TEST
  14. </div>
  15. )
  16. },
  17. },
  18. {
  19. global: {
  20. directives: {
  21. RepeatClick: vRepeatClick,
  22. },
  23. },
  24. }
  25. )
  26. describe('Directives.vue', () => {
  27. it('single click', async () => {
  28. const wrapper = _mount()
  29. const block = wrapper.find('#block')
  30. vi.useFakeTimers()
  31. block.trigger('mousedown')
  32. vi.advanceTimersByTime(PRESS_TIME - 1)
  33. document.dispatchEvent(new MouseEvent('mouseup'))
  34. expect(handler).toHaveBeenCalledTimes(1)
  35. vi.useRealTimers()
  36. })
  37. it('click and hold on', async () => {
  38. const wrapper = _mount()
  39. const block = wrapper.find('#block')
  40. vi.useFakeTimers()
  41. block.trigger('mousedown')
  42. vi.advanceTimersByTime(PRESS_TIME)
  43. document.dispatchEvent(new MouseEvent('mouseup'))
  44. expect(handler).toHaveBeenCalledTimes(2)
  45. vi.useRealTimers()
  46. })
  47. })