Skip to content

Commit 8124138

Browse files
committed
fix(fireEvent): Flush effects from discrete events immediately
BREAKING CHANGE: Importing from `@testing-library/react` will no longer wrap event dispatches of `@testing-library/dom` in `act(() => {})`
1 parent a50c9e1 commit 8124138

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

src/fire-event.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,59 @@
11
import {fireEvent as dtlFireEvent} from '@testing-library/dom'
22
import act from './act-compat'
33

4-
const discreteEvents = new Set()
4+
// https://github.com/facebook/react/blob/b48b38af68c27fd401fe4b923a8fa0b229693cd4/packages/react-dom/src/events/ReactDOMEventListener.js#L310-L366
5+
const discreteEvents = new Set([
6+
'cancel',
7+
'click',
8+
'close',
9+
'contextmenu',
10+
'copy',
11+
'cut',
12+
'auxclick',
13+
'dblclick',
14+
'dragend',
15+
'dragstart',
16+
'drop',
17+
'focusin',
18+
'focusout',
19+
'input',
20+
'invalid',
21+
'keydown',
22+
'keypress',
23+
'keyup',
24+
'mousedown',
25+
'mouseup',
26+
'paste',
27+
'pause',
28+
'play',
29+
'pointercancel',
30+
'pointerdown',
31+
'pointerup',
32+
'ratechange',
33+
'reset',
34+
'seeked',
35+
'submit',
36+
'touchcancel',
37+
'touchend',
38+
'touchstart',
39+
'volumechange',
40+
'change',
41+
'selectionchange',
42+
'textInput',
43+
'compositionstart',
44+
'compositionend',
45+
'compositionupdate',
46+
'beforeblur',
47+
'afterblur',
48+
'beforeinput',
49+
'blur',
50+
'fullscreenchange',
51+
'focus',
52+
'hashchange',
53+
'popstate',
54+
'select',
55+
'selectstart',
56+
])
557
function isDiscreteEvent(type) {
658
return discreteEvents.has(type)
759
}
@@ -15,6 +67,9 @@ function noAct(cb) {
1567
// we make this distinction however is because we have
1668
// a few extra events that work a bit differently
1769
function fireEvent(element, event, ...args) {
70+
// `act` would simulate how this event would behave if dispatched from a React event listener.
71+
// In almost all cases we want to simulate how this event behaves in response to a user interaction.
72+
// See discussion in https://github.com/facebook/react/pull/21202
1873
const eventWrapper = isDiscreteEvent(event.type) ? noAct : act
1974

2075
let fireEventReturnValue

0 commit comments

Comments
 (0)