|
| 1 | +import { patchEvent } from '../src/modules/events' |
| 2 | + |
| 3 | +describe(`events`, () => { |
| 4 | + it('should assign event handler', () => { |
| 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 | + el.dispatchEvent(event) |
| 11 | + el.dispatchEvent(event) |
| 12 | + expect(fn).toHaveBeenCalledTimes(3) |
| 13 | + }) |
| 14 | + |
| 15 | + it('should update event handler', () => { |
| 16 | + const el = document.createElement('div') |
| 17 | + const event = new Event('click') |
| 18 | + const prevFn = jest.fn() |
| 19 | + const nextFn = jest.fn() |
| 20 | + patchEvent(el, 'click', null, prevFn, null) |
| 21 | + el.dispatchEvent(event) |
| 22 | + patchEvent(el, 'click', prevFn, nextFn, null) |
| 23 | + el.dispatchEvent(event) |
| 24 | + el.dispatchEvent(event) |
| 25 | + expect(prevFn).toHaveBeenCalledTimes(1) |
| 26 | + expect(nextFn).toHaveBeenCalledTimes(2) |
| 27 | + }) |
| 28 | + |
| 29 | + it('should support multiple event handlers', () => { |
| 30 | + const el = document.createElement('div') |
| 31 | + const event = new Event('click') |
| 32 | + const fn1 = jest.fn() |
| 33 | + const fn2 = jest.fn() |
| 34 | + patchEvent(el, 'click', null, [fn1, fn2], null) |
| 35 | + el.dispatchEvent(event) |
| 36 | + expect(fn1).toHaveBeenCalledTimes(1) |
| 37 | + expect(fn2).toHaveBeenCalledTimes(1) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should unassign event handler', () => { |
| 41 | + const el = document.createElement('div') |
| 42 | + const event = new Event('click') |
| 43 | + const fn = jest.fn() |
| 44 | + patchEvent(el, 'click', null, fn, null) |
| 45 | + patchEvent(el, 'click', fn, null, null) |
| 46 | + el.dispatchEvent(event) |
| 47 | + expect(fn).not.toHaveBeenCalled() |
| 48 | + }) |
| 49 | + |
| 50 | + it('should support event options', () => { |
| 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 | + el.dispatchEvent(event) |
| 63 | + expect(fn).toHaveBeenCalledTimes(1) |
| 64 | + }) |
| 65 | + |
| 66 | + it('should support varying event options', () => { |
| 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 | + el.dispatchEvent(event) |
| 81 | + expect(prevFn).not.toHaveBeenCalled() |
| 82 | + expect(nextFn).toHaveBeenCalledTimes(1) |
| 83 | + }) |
| 84 | + |
| 85 | + it('should unassign event handler with options', () => { |
| 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 | + el.dispatchEvent(event) |
| 99 | + expect(fn).not.toHaveBeenCalled() |
| 100 | + }) |
| 101 | +}) |
0 commit comments