aboutsummaryrefslogtreecommitdiffstats
path: root/spec/spec/modules/core/event.js
blob: e259593988efdc68fce13b337a3fed065adf5405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* globals describe, expect, it, spyOn, jasmine */
import {
  windowEvents,
  getEvents,
  getEventTarget,
  clearEvents,
  dispatch,
  on,
  off
} from '../../../../src/modules/core/event.js'
import { getWindow } from '../../../../src/utils/window.js'
import { EventTarget, SVG } from '../../../../src/main.js'

const { any, createSpy } = jasmine

describe('event.js', () => {
  describe('getEvents()', () => {
    it('returns the instance events for an EventTarget', () => {
      const eventTarget = new EventTarget()
      eventTarget.events = 'Test'
      const events = getEvents(eventTarget)
      expect(events).toBe('Test')
    })

    it('accesses windowEvents if instance is window', () => {
      windowEvents.events = 'bla'
      const events = getEvents(SVG(getWindow()))
      expect(events).toBe(windowEvents.events)
    })
  })

  describe('getEventTarget()', () => {
    it('calls getEventTarget() on the instance', () => {
      const eventTarget = new EventTarget()
      const spy = spyOn(eventTarget, 'getEventTarget')
      getEventTarget(eventTarget)
      expect(spy).toHaveBeenCalled()
    })
  })

  describe('clearEvents()', () => {
    it('sets events to an empty object', () => {
      const eventTarget = new EventTarget()
      eventTarget.events = 'Test'
      clearEvents(eventTarget)
      expect(eventTarget.events).toEqual({})
    })

    it('does not do anything if no event object is found on the instance', () => {
      const eventTarget = new EventTarget()
      delete eventTarget.events
      clearEvents(eventTarget)
      expect(eventTarget.events).toBe(undefined)
    })
  })

  describe('on()', () => {
    it('binds an event to an EventTarget', () => {
      const eventTarget = new EventTarget()
      const spy = createSpy('spy')
      on(eventTarget, 'event', spy)
      dispatch(eventTarget, 'event')
      expect(spy).toHaveBeenCalledWith(any(getWindow().CustomEvent))
    })

    it('binds to multiple events with space or comma separated string', () => {
      const eventTarget = new EventTarget()
      const spy = createSpy('spy')
      on(eventTarget, 'event1 event2, event3', spy)
      dispatch(eventTarget, 'event1')
      dispatch(eventTarget, 'event2')
      dispatch(eventTarget, 'event3')
      expect(spy).toHaveBeenCalledTimes(3)
    })

    it('binds to multiple events passed as array', () => {
      const eventTarget = new EventTarget()
      const spy = createSpy('spy')
      on(eventTarget, ['event1', 'event2', 'event3'], spy)
      dispatch(eventTarget, 'event1')
      dispatch(eventTarget, 'event2')
      dispatch(eventTarget, 'event3')
      expect(spy).toHaveBeenCalledTimes(3)
    })

    it('binds a namespaced event of form event.namespace', () => {
      const eventTarget = new EventTarget()
      const spy = createSpy('spy')
      on(eventTarget, 'event.namespace', spy)
      dispatch(eventTarget, 'event')
      expect(spy).toHaveBeenCalledWith(any(getWindow().CustomEvent))
    })
  })

  describe('off()', () => {
    it('unbinds an event of an EventTarget', () => {
      const eventTarget = new EventTarget()
      const spy = createSpy('spy')
      on(eventTarget, 'event', spy)
      dispatch(eventTarget, 'event')
      off(eventTarget, 'event', spy)
      dispatch(eventTarget, 'event')
      expect(spy).toHaveBeenCalledTimes(1)
    })

    it('unbinds multiple events with space or comma separated string', () => {
      const eventTarget = new EventTarget()
      const spy = createSpy('spy')
      on(eventTarget, 'event1 event2, event3', spy)
      dispatch(eventTarget, 'event1')
      dispatch(eventTarget, 'event2')
      dispatch(eventTarget, 'event3')
      off(eventTarget, 'event1 event2, event3', spy)
      dispatch(eventTarget, 'event1')
      dispatch(eventTarget, 'event2')
      dispatch(eventTarget, 'event3')
      expect(spy).toHaveBeenCalledTimes(3)
    })

    it('unbinds multiple events with space or comma separated string', () => {
      const eventTarget = new EventTarget()
      const spy = createSpy('spy')
      on(eventTarget, ['event1', 'event2', 'event3'], spy)
      dispatch(eventTarget, 'event1')
      dispatch(eventTarget, 'event2')
      dispatch(eventTarget, 'event3')
      off(eventTarget, ['event1', 'event2', 'event3'], spy)
      dispatch(eventTarget, 'event1')
      dispatch(eventTarget, 'event2')
      dispatch(eventTarget, 'event3')
      expect(spy).toHaveBeenCalledTimes(3)
    })

    it('unbinds a namespaced event', () => {
      const eventTarget = new EventTarget()
      const spy = createSpy('spy')
      on(eventTarget, 'event.namespace', spy)
      dispatch(eventTarget, 'event')
      off(eventTarget, 'event.namespace', spy)
      dispatch(eventTarget, 'event')
      expect(spy).toHaveBeenCalledTimes(1)
    })

    it('unbinds all events including namespaced ones when only event is passed', () => {
      const eventTarget = new EventTarget()
      const spy = createSpy('spy')
      on(eventTarget, ['event1.ns1', 'event2.ns2', 'event3'], spy)
      dispatch(eventTarget, 'event1')
      dispatch(eventTarget, 'event2')
      dispatch(eventTarget, 'event3')
      off(eventTarget, ['event1', 'event2', 'event3'])
      dispatch(eventTarget, 'event1')
      dispatch(eventTarget, 'event2')
      dispatch(eventTarget, 'event3')
      expect(spy).toHaveBeenCalledTimes(3)
    })

    it('unbinds with namespace only', () => {
      const eventTarget = new EventTarget()
      const spy1 = createSpy('spy1')
      const spy2 = createSpy('spy2')
      const spy3 = createSpy('spy3')
      on(eventTarget, 'event1.ns1', spy1)
      on(eventTarget, 'event2.ns1', spy2)
      on(eventTarget, 'event3.ns2', spy3)
      dispatch(eventTarget, 'event1')
      dispatch(eventTarget, 'event2')
      dispatch(eventTarget, 'event3')
      off(eventTarget, '.ns1')
      dispatch(eventTarget, 'event1')
      dispatch(eventTarget, 'event2')
      dispatch(eventTarget, 'event3')
      expect(spy1).toHaveBeenCalledTimes(1)
      expect(spy2).toHaveBeenCalledTimes(1)
      expect(spy3).toHaveBeenCalledTimes(2)
    })

    it('unbinds all events when called without event', () => {
      const eventTarget = new EventTarget()
      const spy1 = createSpy('spy1')
      const spy2 = createSpy('spy2')
      const spy3 = createSpy('spy3')
      on(eventTarget, 'event1.ns1', spy1)
      on(eventTarget, 'event2.ns1', spy2)
      on(eventTarget, 'event3.ns2', spy3)
      dispatch(eventTarget, 'event1')
      dispatch(eventTarget, 'event2')
      dispatch(eventTarget, 'event3')
      off(eventTarget)
      dispatch(eventTarget, 'event1')
      dispatch(eventTarget, 'event2')
      dispatch(eventTarget, 'event3')
      expect(spy1).toHaveBeenCalledTimes(1)
      expect(spy2).toHaveBeenCalledTimes(1)
      expect(spy3).toHaveBeenCalledTimes(1)
    })
  })

  describe('dispatch()', () => {
    it('dispatches a custom event on the EventTarget by calling dispatchEvent()', () => {
      const eventTarget = new EventTarget()
      const spy = spyOn(eventTarget, 'dispatchEvent')
      const event = dispatch(
        eventTarget,
        'event',
        { some: 'data' },
        { cancelable: false }
      )
      expect(event).toEqual(any(getWindow().CustomEvent))
      expect(spy).toHaveBeenCalledWith(event)
      expect(event.detail).toEqual({ some: 'data' })
      expect(event.cancelable).toBe(false)
    })

    it('dispatches the passed event directly', () => {
      const eventTarget = new EventTarget()
      const spy = spyOn(eventTarget, 'dispatchEvent')

      const CustomEvent = getWindow().CustomEvent
      const event1 = new CustomEvent('event', { detail: { some: 'data' } })
      const event2 = dispatch(eventTarget, event1)
      expect(event1).toBe(event2)
      expect(spy).toHaveBeenCalledWith(event1)
    })
  })
})