events.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { patchEvent } from '../../src/modules/events'
  2. import { nextTick } from '@vue/runtime-dom'
  3. describe(`events`, () => {
  4. it('should assign event handler', async () => {
  5. const el = document.createElement('div')
  6. const event = new Event('click')
  7. const fn = jest.fn()
  8. patchEvent(el, 'click', null, fn, null)
  9. el.dispatchEvent(event)
  10. await nextTick()
  11. el.dispatchEvent(event)
  12. await nextTick()
  13. el.dispatchEvent(event)
  14. expect(fn).toHaveBeenCalledTimes(3)
  15. })
  16. it('should update event handler', async () => {
  17. const el = document.createElement('div')
  18. const event = new Event('click')
  19. const prevFn = jest.fn()
  20. const nextFn = jest.fn()
  21. patchEvent(el, 'click', null, prevFn, null)
  22. el.dispatchEvent(event)
  23. patchEvent(el, 'click', prevFn, nextFn, null)
  24. await nextTick()
  25. el.dispatchEvent(event)
  26. await nextTick()
  27. el.dispatchEvent(event)
  28. expect(prevFn).toHaveBeenCalledTimes(1)
  29. expect(nextFn).toHaveBeenCalledTimes(2)
  30. })
  31. it('should support multiple event handlers', () => {
  32. const el = document.createElement('div')
  33. const event = new Event('click')
  34. const fn1 = jest.fn()
  35. const fn2 = jest.fn()
  36. patchEvent(el, 'click', null, [fn1, fn2], null)
  37. el.dispatchEvent(event)
  38. expect(fn1).toHaveBeenCalledTimes(1)
  39. expect(fn2).toHaveBeenCalledTimes(1)
  40. })
  41. it('should unassign event handler', () => {
  42. const el = document.createElement('div')
  43. const event = new Event('click')
  44. const fn = jest.fn()
  45. patchEvent(el, 'click', null, fn, null)
  46. patchEvent(el, 'click', fn, null, null)
  47. el.dispatchEvent(event)
  48. expect(fn).not.toHaveBeenCalled()
  49. })
  50. it('should support event options', async () => {
  51. const el = document.createElement('div')
  52. const event = new Event('click')
  53. const fn = jest.fn()
  54. const nextValue = {
  55. handler: fn,
  56. options: {
  57. once: true
  58. }
  59. }
  60. patchEvent(el, 'click', null, nextValue, null)
  61. el.dispatchEvent(event)
  62. await nextTick()
  63. el.dispatchEvent(event)
  64. expect(fn).toHaveBeenCalledTimes(1)
  65. })
  66. it('should support varying event options', async () => {
  67. const el = document.createElement('div')
  68. const event = new Event('click')
  69. const prevFn = jest.fn()
  70. const nextFn = jest.fn()
  71. const nextValue = {
  72. handler: nextFn,
  73. options: {
  74. once: true
  75. }
  76. }
  77. patchEvent(el, 'click', null, prevFn, null)
  78. patchEvent(el, 'click', prevFn, nextValue, null)
  79. el.dispatchEvent(event)
  80. await nextTick()
  81. el.dispatchEvent(event)
  82. expect(prevFn).not.toHaveBeenCalled()
  83. expect(nextFn).toHaveBeenCalledTimes(1)
  84. })
  85. it('should unassign event handler with options', async () => {
  86. const el = document.createElement('div')
  87. const event = new Event('click')
  88. const fn = jest.fn()
  89. const nextValue = {
  90. handler: fn,
  91. options: {
  92. once: true
  93. }
  94. }
  95. patchEvent(el, 'click', null, nextValue, null)
  96. patchEvent(el, 'click', nextValue, null, null)
  97. el.dispatchEvent(event)
  98. await nextTick()
  99. el.dispatchEvent(event)
  100. expect(fn).not.toHaveBeenCalled()
  101. })
  102. })