events.spec.ts 2.9 KB

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