Skip to content

Commit ccf90cf

Browse files
pheeriaKent C. Dodds
authored andcommitted
test: add bubbling tests (#386)
* test: add bubbling tests * chore: fix a typo * chore: split events
1 parent 79661e1 commit ccf90cf

File tree

1 file changed

+118
-1
lines changed

1 file changed

+118
-1
lines changed

src/__tests__/events.js

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,93 @@ const eventTypes = [
137137
},
138138
]
139139

140+
const bubblingEvents = [
141+
'copy',
142+
'cut',
143+
'paste',
144+
'compositionEnd',
145+
'compositionStart',
146+
'compositionUpdate',
147+
'keyDown',
148+
'keyPress',
149+
'keyUp',
150+
'focusIn',
151+
'focusOut',
152+
'change',
153+
'input',
154+
'submit',
155+
'reset',
156+
'click',
157+
'contextMenu',
158+
'dblClick',
159+
'drag',
160+
'dragEnd',
161+
'dragEnter',
162+
'dragExit',
163+
'dragLeave',
164+
'dragOver',
165+
'dragStart',
166+
'drop',
167+
'mouseDown',
168+
'mouseMove',
169+
'mouseOut',
170+
'mouseOver',
171+
'mouseUp',
172+
'select',
173+
'touchCancel',
174+
'touchEnd',
175+
'touchMove',
176+
'touchStart',
177+
'wheel',
178+
'animationStart',
179+
'animationEnd',
180+
'animationIteration',
181+
'transitionEnd',
182+
'pointerOver',
183+
'pointerDown',
184+
'pointerMove',
185+
'pointerUp',
186+
'pointerCancel',
187+
'pointerOut',
188+
]
189+
190+
const nonBubblingEvents = [
191+
'focus',
192+
'blur',
193+
'invalid',
194+
'mouseEnter',
195+
'mouseLeave',
196+
'scroll',
197+
'abort',
198+
'canPlay',
199+
'canPlayThrough',
200+
'durationChange',
201+
'emptied',
202+
'encrypted',
203+
'ended',
204+
'loadedData',
205+
'loadedMetadata',
206+
'loadStart',
207+
'pause',
208+
'play',
209+
'playing',
210+
'progress',
211+
'rateChange',
212+
'seeked',
213+
'seeking',
214+
'stalled',
215+
'suspend',
216+
'timeUpdate',
217+
'volumeChange',
218+
'waiting',
219+
'load',
220+
'error',
221+
'pointerEnter',
222+
'pointerLeave',
223+
'gotPointerCapture',
224+
'lostPointerCapture',
225+
]
226+
140227
eventTypes.forEach(({type, events, elementType}) => {
141228
describe(`${type} Events`, () => {
142229
events.forEach(eventName => {
@@ -151,6 +238,36 @@ eventTypes.forEach(({type, events, elementType}) => {
151238
})
152239
})
153240

241+
describe(`Bubbling Events`, () => {
242+
bubblingEvents.forEach(event =>
243+
it(`bubbles ${event}`, () => {
244+
const node = document.createElement('div')
245+
const spy = jest.fn()
246+
node.addEventListener(event.toLowerCase(), spy)
247+
248+
const innerNode = document.createElement('div')
249+
node.appendChild(innerNode)
250+
251+
fireEvent[event](innerNode)
252+
expect(spy).toHaveBeenCalledTimes(1)
253+
}),
254+
)
255+
256+
nonBubblingEvents.forEach(event =>
257+
it(`doesn't bubble ${event}`, () => {
258+
const node = document.createElement('div')
259+
const spy = jest.fn()
260+
node.addEventListener(event.toLowerCase(), spy)
261+
262+
const innerNode = document.createElement('div')
263+
node.appendChild(innerNode)
264+
265+
fireEvent[event](innerNode)
266+
expect(spy).not.toHaveBeenCalled()
267+
}),
268+
)
269+
})
270+
154271
describe(`Aliased Events`, () => {
155272
it(`fires doubleClick`, () => {
156273
const node = document.createElement('div')
@@ -215,7 +332,7 @@ test('fires shortcut events on Window', () => {
215332
window.addEventListener('click', clickSpy)
216333
fireEvent.click(window)
217334
expect(clickSpy).toHaveBeenCalledTimes(1)
218-
window.removeEventListener('message', clickSpy)
335+
window.removeEventListener('click', clickSpy)
219336
})
220337

221338
test('throws a useful error message when firing events on non-existent nodes', () => {

0 commit comments

Comments
 (0)