Skip to content

Commit 6c6332f

Browse files
author
Josef Blake
committed
added fireEvent util
1 parent 2289371 commit 6c6332f

File tree

7 files changed

+669
-3
lines changed

7 files changed

+669
-3
lines changed

.all-contributorsrc

+11
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@
139139
"code",
140140
"doc"
141141
]
142+
},
143+
{
144+
"login": "jomaxx",
145+
"name": "Josef Maxx Blake",
146+
"avatar_url": "https://avatars2.githubusercontent.com/u/2747424?v=4",
147+
"profile": "http://jomaxx.com",
148+
"contributions": [
149+
"code",
150+
"doc",
151+
"test"
152+
]
142153
}
143154
]
144155
}

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[![downloads][downloads-badge]][npmtrends]
1717
[![MIT License][license-badge]][license]
1818

19-
[![All Contributors](https://img.shields.io/badge/all_contributors-13-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-14-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Code of Conduct][coc-badge]][coc]
2222

@@ -662,7 +662,7 @@ Thanks goes to these people ([emoji key][emojis]):
662662
<!-- prettier-ignore -->
663663
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") |
664664
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
665-
| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Documentation") |
665+
| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/2747424?v=4" width="100px;"/><br /><sub><b>Josef Maxx Blake</b></sub>](http://jomaxx.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Tests") |
666666

667667
<!-- ALL-CONTRIBUTORS-LIST:END -->
668668

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
},
4141
"devDependencies": {
4242
"jest-in-case": "^1.0.2",
43-
"kcd-scripts": "^0.37.0"
43+
"kcd-scripts": "^0.37.0",
44+
"react": "^16.3.1",
45+
"react-dom": "^16.3.1"
4446
},
4547
"eslintConfig": {
4648
"extends": "./node_modules/kcd-scripts/eslint.js",

src/__tests__/events.js

+287
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
import React from 'react'
2+
import {mount, unmount} from './helpers/react'
3+
import {fireEvent} from '../'
4+
5+
describe('Clipboard Events', () => {
6+
;['copy', 'paste'].forEach(eventName => {
7+
it(`fires ${eventName}`, () => {
8+
const node = document.createElement('input')
9+
const spy = jest.fn()
10+
node.addEventListener(eventName.toLowerCase(), spy)
11+
fireEvent[eventName](node)
12+
expect(spy).toHaveBeenCalledTimes(1)
13+
})
14+
})
15+
})
16+
17+
describe('Composition Events', () => {
18+
;['compositionEnd', 'compositionStart', 'compositionUpdate'].forEach(
19+
eventName => {
20+
it(`fires ${eventName}`, () => {
21+
const node = document.createElement('input')
22+
const spy = jest.fn()
23+
node.addEventListener(eventName.toLowerCase(), spy)
24+
fireEvent[eventName](node)
25+
expect(spy).toHaveBeenCalledTimes(1)
26+
})
27+
},
28+
)
29+
})
30+
31+
describe('Keyboard Events', () => {
32+
;['keyDown', 'keyPress', 'keyUp'].forEach(eventName => {
33+
it(`fires ${eventName}`, () => {
34+
const node = document.createElement('input')
35+
const spy = jest.fn()
36+
node.addEventListener(eventName.toLowerCase(), spy)
37+
fireEvent[eventName](node)
38+
expect(spy).toHaveBeenCalledTimes(1)
39+
})
40+
})
41+
})
42+
43+
describe('Focus Events', () => {
44+
;['focus', 'blur'].forEach(eventName => {
45+
it(`fires ${eventName}`, () => {
46+
const node = document.createElement('input')
47+
const spy = jest.fn()
48+
node.addEventListener(eventName.toLowerCase(), spy)
49+
fireEvent[eventName](node)
50+
expect(spy).toHaveBeenCalledTimes(1)
51+
})
52+
})
53+
})
54+
55+
describe('Form Events', () => {
56+
;['change', 'input', 'invalid'].forEach(eventName => {
57+
it(`fires ${eventName}`, () => {
58+
const node = document.createElement('input')
59+
const spy = jest.fn()
60+
node.addEventListener(eventName.toLowerCase(), spy)
61+
fireEvent[eventName](node)
62+
expect(spy).toHaveBeenCalledTimes(1)
63+
})
64+
})
65+
;['submit'].forEach(eventName => {
66+
it(`fires ${eventName}`, () => {
67+
const node = document.createElement('form')
68+
const spy = jest.fn()
69+
node.addEventListener(eventName.toLowerCase(), spy)
70+
fireEvent[eventName](node)
71+
expect(spy).toHaveBeenCalledTimes(1)
72+
})
73+
})
74+
})
75+
76+
describe('Mouse Events', () => {
77+
;[
78+
'click',
79+
'contextMenu',
80+
'dblClick',
81+
'drag',
82+
'dragEnd',
83+
'dragEnter',
84+
'dragExit',
85+
'dragLeave',
86+
'dragOver',
87+
'dragStart',
88+
'drop',
89+
'mouseDown',
90+
'mouseEnter',
91+
'mouseLeave',
92+
'mouseMove',
93+
'mouseOut',
94+
'mouseOver',
95+
'mouseUp',
96+
].forEach(eventName => {
97+
it(`fires ${eventName}`, () => {
98+
const node = document.createElement('button')
99+
const spy = jest.fn()
100+
node.addEventListener(eventName.toLowerCase(), spy)
101+
fireEvent[eventName](node)
102+
expect(spy).toHaveBeenCalledTimes(1)
103+
})
104+
})
105+
})
106+
107+
describe('Selection Events', () => {
108+
;['select'].forEach(eventName => {
109+
it(`fires ${eventName}`, () => {
110+
const node = document.createElement('input')
111+
const spy = jest.fn()
112+
node.addEventListener(eventName.toLowerCase(), spy)
113+
fireEvent[eventName](node)
114+
expect(spy).toHaveBeenCalledTimes(1)
115+
})
116+
})
117+
})
118+
119+
describe('Touch Events', () => {
120+
;['touchCancel', 'touchEnd', 'touchMove', 'touchStart'].forEach(eventName => {
121+
it(`fires ${eventName}`, () => {
122+
const node = document.createElement('button')
123+
const spy = jest.fn()
124+
node.addEventListener(eventName.toLowerCase(), spy)
125+
fireEvent[eventName](node)
126+
expect(spy).toHaveBeenCalledTimes(1)
127+
})
128+
})
129+
})
130+
131+
describe('UI Events', () => {
132+
;['scroll'].forEach(eventName => {
133+
it(`fires ${eventName}`, () => {
134+
const node = document.createElement('div')
135+
const spy = jest.fn()
136+
node.addEventListener(eventName.toLowerCase(), spy)
137+
fireEvent[eventName](node)
138+
expect(spy).toHaveBeenCalledTimes(1)
139+
})
140+
})
141+
})
142+
143+
describe('Wheel Events', () => {
144+
;['wheel'].forEach(eventName => {
145+
it(`fires ${eventName}`, () => {
146+
const node = document.createElement('div')
147+
const spy = jest.fn()
148+
node.addEventListener(eventName.toLowerCase(), spy)
149+
fireEvent[eventName](node)
150+
expect(spy).toHaveBeenCalledTimes(1)
151+
})
152+
})
153+
})
154+
155+
describe('Media Events', () => {
156+
;[
157+
'abort',
158+
'canPlay',
159+
'canPlayThrough',
160+
'durationChange',
161+
'emptied',
162+
'encrypted',
163+
'ended',
164+
'error',
165+
'loadedData',
166+
'loadedMetadata',
167+
'loadStart',
168+
'pause',
169+
'play',
170+
'playing',
171+
'progress',
172+
'rateChange',
173+
'seeked',
174+
'seeking',
175+
'stalled',
176+
'suspend',
177+
'timeUpdate',
178+
'volumeChange',
179+
'waiting',
180+
].forEach(eventName => {
181+
it(`fires ${eventName}`, () => {
182+
const node = document.createElement('video')
183+
const spy = jest.fn()
184+
node.addEventListener(eventName.toLowerCase(), spy)
185+
fireEvent[eventName](node)
186+
expect(spy).toHaveBeenCalledTimes(1)
187+
})
188+
})
189+
})
190+
191+
describe('Image Events', () => {
192+
;['load', 'error'].forEach(eventName => {
193+
it(`fires ${eventName}`, () => {
194+
const node = document.createElement('img')
195+
const spy = jest.fn()
196+
node.addEventListener(eventName.toLowerCase(), spy)
197+
fireEvent[eventName](node)
198+
expect(spy).toHaveBeenCalledTimes(1)
199+
})
200+
})
201+
})
202+
203+
describe('Animation Events', () => {
204+
;['animationStart', 'animationEnd', 'animationIteration'].forEach(
205+
eventName => {
206+
it(`fires ${eventName}`, () => {
207+
const node = document.createElement('div')
208+
const spy = jest.fn()
209+
node.addEventListener(eventName.toLowerCase(), spy)
210+
fireEvent[eventName](node)
211+
expect(spy).toHaveBeenCalledTimes(1)
212+
})
213+
},
214+
)
215+
})
216+
217+
describe('Transition Events', () => {
218+
;['transitionEnd'].forEach(eventName => {
219+
it(`fires ${eventName}`, () => {
220+
const node = document.createElement('div')
221+
const spy = jest.fn()
222+
node.addEventListener(eventName.toLowerCase(), spy)
223+
fireEvent[eventName](node)
224+
expect(spy).toHaveBeenCalledTimes(1)
225+
})
226+
})
227+
})
228+
229+
describe('Alias Events', () => {
230+
it(`fires doubleClick`, () => {
231+
const node = document.createElement('button')
232+
const spy = jest.fn()
233+
node.addEventListener('dblclick', spy)
234+
fireEvent.doubleClick(node)
235+
expect(spy).toHaveBeenCalledTimes(1)
236+
})
237+
})
238+
239+
describe('React Events', () => {
240+
afterEach(unmount)
241+
242+
// todo figure out why this test is broken
243+
it.skip(`triggers onChange`, () => {
244+
let node
245+
const spy = jest.fn()
246+
247+
mount(
248+
React.createElement('input', {
249+
onChange: spy,
250+
ref: el => (node = el),
251+
}),
252+
)
253+
254+
fireEvent.change(node)
255+
expect(spy).toHaveBeenCalledTimes(1)
256+
})
257+
258+
it(`triggers onMouseEnter`, () => {
259+
let node
260+
const spy = jest.fn()
261+
262+
mount(
263+
React.createElement('div', {
264+
onMouseEnter: spy,
265+
ref: el => (node = el),
266+
}),
267+
)
268+
269+
fireEvent.mouseEnter(node)
270+
expect(spy).toHaveBeenCalledTimes(1)
271+
})
272+
273+
it(`triggers onMouseLeave`, () => {
274+
let node
275+
const spy = jest.fn()
276+
277+
mount(
278+
React.createElement('div', {
279+
onMouseLeave: spy,
280+
ref: el => (node = el),
281+
}),
282+
)
283+
284+
fireEvent.mouseLeave(node)
285+
expect(spy).toHaveBeenCalledTimes(1)
286+
})
287+
})

src/__tests__/helpers/react.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import ReactDOM from 'react-dom'
2+
3+
const node = document.body.appendChild(document.createElement('div'))
4+
5+
function mount(element) {
6+
ReactDOM.render(element, node)
7+
}
8+
9+
function unmount() {
10+
ReactDOM.unmountComponentAtNode(node)
11+
}
12+
13+
export {mount, unmount}

0 commit comments

Comments
 (0)