You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add simulateEventDispatch to test ReactDOMEventListener (#28079)
## Overview
For events, the browser will yield to microtasks between calling event
handers, allowing time to flush work inbetween. For example, in the
browser, this code will log the flushes between events:
```js
<body onclick="console.log('body'); Promise.resolve().then(() => console.log('flush body'));">
<div onclick="console.log('div'); Promise.resolve().then(() => console.log('flush div'));">
hi
</div>
</body>
// Logs
div
flush div
body
flush body
```
[Sandbox](https://codesandbox.io/s/eloquent-noether-mw2cjg?file=/index.html)
The problem is, `dispatchEvent` (either in the browser, or JSDOM) does
not yield to microtasks. Which means, this code will log the flushes
after the events:
```js
const target = document.getElementsByTagName("div")[0];
const nativeEvent = document.createEvent("Event");
nativeEvent.initEvent("click", true, true);
target.dispatchEvent(nativeEvent);
// Logs
div
body
flush div
flush body
```
## The problem
This mostly isn't a problem because React attaches event handler at the
root, and calls the event handlers on components via the synthetic event
system. We handle flushing between calling event handlers as needed.
However, if you're mixing capture and bubbling events, or using multiple
roots, then the problem of not flushing microtasks between events can
come into play. This was found when converting a test to `createRoot` in
#28050 (comment), and
that test is an example of where this is an issue with nested roots.
Here's a sandox for
[discrete](https://codesandbox.io/p/sandbox/red-http-2wg8k5) and
[continuous](https://codesandbox.io/p/sandbox/gracious-voice-6r7tsc?file=%2Fsrc%2Findex.js%3A25%2C28)
events, showing how the test should behave. The existing test, when
switched to `createRoot` matches the browser behavior for continuous
events, but not discrete. Continuous events should be batched, and
discrete should flush individually.
## The fix
This PR implements the fix suggested by @sebmarkbage, to manually
traverse the path up from the element and dispatch events, yielding
between each call.
DiffTrain build for commit cd63ef7.
Copy file name to clipboardExpand all lines: compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react-test-renderer/cjs/ReactTestRenderer-dev.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -25670,7 +25670,7 @@ if (__DEV__) {
25670
25670
return root;
25671
25671
}
25672
25672
25673
-
var ReactVersion = "18.3.0-canary-04b59928d-20240208";
25673
+
var ReactVersion = "18.3.0-canary-cd63ef792-20240208";
Copy file name to clipboardExpand all lines: compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react-test-renderer/cjs/ReactTestRenderer-prod.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -9152,7 +9152,7 @@ var devToolsConfig$jscomp$inline_1011 = {
9152
9152
throwError("TestRenderer does not support findFiberByHostInstance()");
9153
9153
},
9154
9154
bundleType: 0,
9155
-
version: "18.3.0-canary-04b59928d-20240208",
9155
+
version: "18.3.0-canary-cd63ef792-20240208",
9156
9156
rendererPackageName: "react-test-renderer"
9157
9157
};
9158
9158
varinternals$jscomp$inline_1189={
@@ -9183,7 +9183,7 @@ var internals$jscomp$inline_1189 = {
Copy file name to clipboardExpand all lines: compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react-test-renderer/cjs/ReactTestRenderer-profiling.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -9580,7 +9580,7 @@ var devToolsConfig$jscomp$inline_1053 = {
9580
9580
throwError("TestRenderer does not support findFiberByHostInstance()");
9581
9581
},
9582
9582
bundleType: 0,
9583
-
version: "18.3.0-canary-04b59928d-20240208",
9583
+
version: "18.3.0-canary-cd63ef792-20240208",
9584
9584
rendererPackageName: "react-test-renderer"
9585
9585
};
9586
9586
varinternals$jscomp$inline_1230={
@@ -9611,7 +9611,7 @@ var internals$jscomp$inline_1230 = {
0 commit comments