File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
packages/react-dom/src/__tests__ Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1212let React ;
1313let ReactDOMClient ;
1414let act ;
15+ let ReactDOM ;
1516
1617describe ( 'ReactEventIndependence' , ( ) => {
1718 beforeEach ( ( ) => {
1819 jest . resetModules ( ) ;
1920
2021 React = require ( 'react' ) ;
22+ ReactDOM = require ( 'react-dom' ) ;
2123 ReactDOMClient = require ( 'react-dom/client' ) ;
2224 act = require ( 'internal-test-utils' ) . act ;
2325 } ) ;
@@ -62,4 +64,29 @@ describe('ReactEventIndependence', () => {
6264 document . body . removeChild ( outer ) ;
6365 }
6466 } ) ;
67+
68+ it ( 'does not when event fired on unmounted tree' , async ( ) => {
69+ let clicks = 0 ;
70+ const container = document . createElement ( 'div' ) ;
71+ document . body . appendChild ( container ) ;
72+ try {
73+ const root = ReactDOMClient . createRoot ( container ) ;
74+
75+ await act ( ( ) => {
76+ root . render ( < button onClick = { ( ) => clicks ++ } > click me</ button > ) ;
77+ } ) ;
78+
79+ const button = container . firstChild ;
80+
81+ // Now we unmount the component, as if caused by a non-React event handler
82+ // for the same click we're about to simulate, like closing a layer:
83+ root . unmount ( ) ;
84+ button . click ( ) ;
85+
86+ // Since the tree is unmounted, we don't dispatch the click event.
87+ expect ( clicks ) . toBe ( 0 ) ;
88+ } finally {
89+ document . body . removeChild ( container ) ;
90+ }
91+ } ) ;
6592} ) ;
You can’t perform that action at this time.
0 commit comments