Skip to content

Commit 5145a9e

Browse files
author
Jack Pope
committed
Add unmounted tree test case back in
1 parent afe3351 commit 5145a9e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

packages/react-dom/src/__tests__/ReactEventIndependence-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
let React;
1313
let ReactDOMClient;
1414
let act;
15+
let ReactDOM;
1516

1617
describe('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
});

0 commit comments

Comments
 (0)