Skip to content

Commit d45b449

Browse files
jomaxxKent C. Dodds
authored and
Kent C. Dodds
committed
feat(fireEvent): add fireEvent util (#13)
* added fireEvent util * remove react logic * 💯 coverage * Update README.md
1 parent 2289371 commit d45b449

File tree

6 files changed

+530
-4
lines changed

6 files changed

+530
-4
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

+31-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

@@ -78,6 +78,7 @@ when a real user uses it.
7878
* [`getByAltText(container: HTMLElement, text: TextMatch): HTMLElement`](#getbyalttextcontainer-htmlelement-text-textmatch-htmlelement)
7979
* [`wait`](#wait)
8080
* [`waitForElement`](#waitforelement)
81+
* [`fireEvent(node: HTMLElement, event: Event)`](#fireeventnode-htmlelement-event-event)
8182
* [Custom Jest Matchers](#custom-jest-matchers)
8283
* [`toBeInTheDOM`](#tobeinthedom)
8384
* [`toHaveTextContent`](#tohavetextcontent)
@@ -368,6 +369,34 @@ The default `timeout` is `4500ms` which will keep you under
368369
additions and removals of child elements (including text nodes) in the `container` and any of its descendants.
369370
It won't detect attribute changes unless you add `attributes: true` to the options.
370371

372+
### `fireEvent(node: HTMLElement, event: Event)`
373+
374+
Fire DOM events.
375+
376+
```javascript
377+
// <button>Submit</button>
378+
fireEvent(
379+
getElementByText('Submit'),
380+
new MouseEvent('click', {
381+
bubbles: true,
382+
cancelable: true,
383+
}),
384+
)
385+
```
386+
387+
#### `fireEvent[eventName](node: HTMLElement, eventProperties: Object)`
388+
389+
Convenience methods for firing DOM events. Check out
390+
[src/events.js](https://github.com/kentcdodds/dom-testing-library/blob/master/src/events.js)
391+
for a full list as well as default `eventProperties`.
392+
393+
```javascript
394+
// <button>Submit</button>
395+
const rightClick = {button: 2}
396+
fireEvent.click(getElementByText('Submit'), rightClick)
397+
// default `button` property for click events is set to `0` which is a left click.
398+
```
399+
371400
## Custom Jest Matchers
372401

373402
There are two simple API which extend the `expect` API of jest for making assertions easier.
@@ -662,7 +691,7 @@ Thanks goes to these people ([emoji key][emojis]):
662691
<!-- prettier-ignore -->
663692
| [<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") |
664693
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
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") |
694+
| [<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") |
666695

667696
<!-- ALL-CONTRIBUTORS-LIST:END -->
668697

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
],
3636
"dependencies": {
3737
"jest-matcher-utils": "^22.4.3",
38-
"wait-for-expect": "^0.4.0",
39-
"mutationobserver-shim": "^0.3.2"
38+
"mutationobserver-shim": "^0.3.2",
39+
"wait-for-expect": "^0.4.0"
4040
},
4141
"devDependencies": {
4242
"jest-in-case": "^1.0.2",

src/__tests__/events.js

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import {fireEvent} from '../'
2+
3+
const eventTypes = [
4+
{
5+
type: 'Clipboard',
6+
events: ['copy', 'paste'],
7+
elementType: 'input',
8+
},
9+
{
10+
type: 'Composition',
11+
events: ['compositionEnd', 'compositionStart', 'compositionUpdate'],
12+
elementType: 'input',
13+
},
14+
{
15+
type: 'Keyboard',
16+
events: ['keyDown', 'keyPress', 'keyUp'],
17+
elementType: 'input',
18+
},
19+
{
20+
type: 'Focus',
21+
events: ['focus', 'blur'],
22+
elementType: 'input',
23+
},
24+
{
25+
type: 'Form',
26+
events: ['focus', 'blur'],
27+
elementType: 'input',
28+
},
29+
{
30+
type: 'Focus',
31+
events: ['change', 'input', 'invalid'],
32+
elementType: 'input',
33+
},
34+
{
35+
type: 'Focus',
36+
events: ['submit'],
37+
elementType: 'form',
38+
},
39+
{
40+
type: 'Mouse',
41+
events: [
42+
'click',
43+
'contextMenu',
44+
'dblClick',
45+
'drag',
46+
'dragEnd',
47+
'dragEnter',
48+
'dragExit',
49+
'dragLeave',
50+
'dragOver',
51+
'dragStart',
52+
'drop',
53+
'mouseDown',
54+
'mouseEnter',
55+
'mouseLeave',
56+
'mouseMove',
57+
'mouseOut',
58+
'mouseOver',
59+
'mouseUp',
60+
],
61+
elementType: 'button',
62+
},
63+
{
64+
type: 'Selection',
65+
events: ['select'],
66+
elementType: 'input',
67+
},
68+
{
69+
type: 'Touch',
70+
events: ['touchCancel', 'touchEnd', 'touchMove', 'touchStart'],
71+
elementType: 'button',
72+
},
73+
{
74+
type: 'UI',
75+
events: ['scroll'],
76+
elementType: 'div',
77+
},
78+
{
79+
type: 'Wheel',
80+
events: ['wheel'],
81+
elementType: 'div',
82+
},
83+
{
84+
type: 'Media',
85+
events: [
86+
'abort',
87+
'canPlay',
88+
'canPlayThrough',
89+
'durationChange',
90+
'emptied',
91+
'encrypted',
92+
'ended',
93+
'error',
94+
'loadedData',
95+
'loadedMetadata',
96+
'loadStart',
97+
'pause',
98+
'play',
99+
'playing',
100+
'progress',
101+
'rateChange',
102+
'seeked',
103+
'seeking',
104+
'stalled',
105+
'suspend',
106+
'timeUpdate',
107+
'volumeChange',
108+
'waiting',
109+
],
110+
elementType: 'video',
111+
},
112+
{
113+
type: 'Image',
114+
events: ['load', 'error'],
115+
elementType: 'img',
116+
},
117+
{
118+
type: 'Animation',
119+
events: ['animationStart', 'animationEnd', 'animationIteration'],
120+
elementType: 'div',
121+
},
122+
{
123+
type: 'Transition',
124+
events: ['transitionEnd'],
125+
elementType: 'div',
126+
},
127+
]
128+
129+
eventTypes.forEach(({type, events, elementType}) => {
130+
describe(`${type} Events`, () => {
131+
events.forEach(eventName => {
132+
it(`fires ${eventName}`, () => {
133+
const node = document.createElement(elementType)
134+
const spy = jest.fn()
135+
node.addEventListener(eventName.toLowerCase(), spy)
136+
fireEvent[eventName](node)
137+
expect(spy).toHaveBeenCalledTimes(1)
138+
})
139+
})
140+
})
141+
})
142+
143+
describe(`Aliased Events`, () => {
144+
it(`fires doubleClick`, () => {
145+
const node = document.createElement('div')
146+
const spy = jest.fn()
147+
node.addEventListener('dblclick', spy)
148+
fireEvent.doubleClick(node)
149+
expect(spy).toHaveBeenCalledTimes(1)
150+
})
151+
})

0 commit comments

Comments
 (0)