patchEvents.spec.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { patchProp } from '../src/patchProp'
  2. const timeout = () => new Promise(r => setTimeout(r))
  3. describe(`runtime-dom: events patching`, () => {
  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. patchProp(el, 'onClick', null, fn)
  9. el.dispatchEvent(event)
  10. await timeout()
  11. el.dispatchEvent(event)
  12. await timeout()
  13. el.dispatchEvent(event)
  14. await timeout()
  15. expect(fn).toHaveBeenCalledTimes(3)
  16. })
  17. it('should update event handler', async () => {
  18. const el = document.createElement('div')
  19. const event = new Event('click')
  20. const prevFn = jest.fn()
  21. const nextFn = jest.fn()
  22. patchProp(el, 'onClick', null, prevFn)
  23. el.dispatchEvent(event)
  24. patchProp(el, 'onClick', prevFn, nextFn)
  25. await timeout()
  26. el.dispatchEvent(event)
  27. await timeout()
  28. el.dispatchEvent(event)
  29. await timeout()
  30. expect(prevFn).toHaveBeenCalledTimes(1)
  31. expect(nextFn).toHaveBeenCalledTimes(2)
  32. })
  33. it('should support multiple event handlers', async () => {
  34. const el = document.createElement('div')
  35. const event = new Event('click')
  36. const fn1 = jest.fn()
  37. const fn2 = jest.fn()
  38. patchProp(el, 'onClick', null, [fn1, fn2])
  39. el.dispatchEvent(event)
  40. await timeout()
  41. expect(fn1).toHaveBeenCalledTimes(1)
  42. expect(fn2).toHaveBeenCalledTimes(1)
  43. })
  44. it('should unassign event handler', async () => {
  45. const el = document.createElement('div')
  46. const event = new Event('click')
  47. const fn = jest.fn()
  48. patchProp(el, 'onClick', null, fn)
  49. patchProp(el, 'onClick', fn, null)
  50. el.dispatchEvent(event)
  51. await timeout()
  52. expect(fn).not.toHaveBeenCalled()
  53. })
  54. it('should support event options', async () => {
  55. const el = document.createElement('div')
  56. const event = new Event('click')
  57. const fn = jest.fn()
  58. const nextValue = {
  59. handler: fn,
  60. options: {
  61. once: true
  62. }
  63. }
  64. patchProp(el, 'onClick', null, nextValue)
  65. el.dispatchEvent(event)
  66. await timeout()
  67. el.dispatchEvent(event)
  68. await timeout()
  69. expect(fn).toHaveBeenCalledTimes(1)
  70. })
  71. it('should support varying event options', async () => {
  72. const el = document.createElement('div')
  73. const event = new Event('click')
  74. const prevFn = jest.fn()
  75. const nextFn = jest.fn()
  76. const nextValue = {
  77. handler: nextFn,
  78. options: {
  79. once: true
  80. }
  81. }
  82. patchProp(el, 'onClick', null, prevFn)
  83. patchProp(el, 'onClick', prevFn, nextValue)
  84. el.dispatchEvent(event)
  85. await timeout()
  86. el.dispatchEvent(event)
  87. await timeout()
  88. expect(prevFn).not.toHaveBeenCalled()
  89. expect(nextFn).toHaveBeenCalledTimes(1)
  90. })
  91. it('should unassign event handler with options', async () => {
  92. const el = document.createElement('div')
  93. const event = new Event('click')
  94. const fn = jest.fn()
  95. const nextValue = {
  96. handler: fn,
  97. options: {
  98. once: true
  99. }
  100. }
  101. patchProp(el, 'onClick', null, nextValue)
  102. patchProp(el, 'onClick', nextValue, null)
  103. el.dispatchEvent(event)
  104. await timeout()
  105. el.dispatchEvent(event)
  106. await timeout()
  107. expect(fn).not.toHaveBeenCalled()
  108. })
  109. it('should support native onclick', async () => {
  110. const el = document.createElement('div')
  111. const event = new Event('click')
  112. // string should be set as attribute
  113. const fn = ((window as any).__globalSpy = jest.fn())
  114. patchProp(el, 'onclick', null, '__globalSpy(1)')
  115. el.dispatchEvent(event)
  116. await timeout()
  117. delete (window as any).__globalSpy
  118. expect(fn).toHaveBeenCalledWith(1)
  119. const fn2 = jest.fn()
  120. patchProp(el, 'onclick', '__globalSpy(1)', fn2)
  121. el.dispatchEvent(event)
  122. await timeout()
  123. expect(fn).toHaveBeenCalledTimes(1)
  124. expect(fn2).toHaveBeenCalledWith(event)
  125. })
  126. it('should support stopImmediatePropagation on multiple listeners', async () => {
  127. const el = document.createElement('div')
  128. const event = new Event('click')
  129. const fn1 = jest.fn((e: Event) => {
  130. e.stopImmediatePropagation()
  131. })
  132. const fn2 = jest.fn()
  133. patchProp(el, 'onClick', null, [fn1, fn2])
  134. el.dispatchEvent(event)
  135. await timeout()
  136. expect(fn1).toHaveBeenCalledTimes(1)
  137. expect(fn2).toHaveBeenCalledTimes(0)
  138. })
  139. })