|
11 | 11 |
|
12 | 12 | describe('SimpleEventPlugin', function () { |
13 | 13 | let React; |
14 | | - let ReactDOM; |
15 | 14 | let ReactDOMClient; |
16 | 15 | let Scheduler; |
17 | 16 | let act; |
@@ -21,8 +20,10 @@ describe('SimpleEventPlugin', function () { |
21 | 20 | let assertLog; |
22 | 21 | let waitForAll; |
23 | 22 |
|
24 | | - function expectClickThru(element) { |
25 | | - element.click(); |
| 23 | + async function expectClickThru(element) { |
| 24 | + await act(() => { |
| 25 | + element.click(); |
| 26 | + }); |
26 | 27 | expect(onClick).toHaveBeenCalledTimes(1); |
27 | 28 | } |
28 | 29 |
|
@@ -31,23 +32,27 @@ describe('SimpleEventPlugin', function () { |
31 | 32 | expect(onClick).toHaveBeenCalledTimes(0); |
32 | 33 | } |
33 | 34 |
|
34 | | - function mounted(element) { |
| 35 | + async function mounted(element) { |
35 | 36 | container = document.createElement('div'); |
36 | 37 | document.body.appendChild(container); |
37 | | - element = ReactDOM.render(element, container); |
| 38 | + const root = ReactDOMClient.createRoot(container); |
| 39 | + await act(() => { |
| 40 | + root.render(element); |
| 41 | + }); |
| 42 | + element = container.firstChild; |
38 | 43 | return element; |
39 | 44 | } |
40 | 45 |
|
41 | 46 | beforeEach(function () { |
42 | 47 | jest.resetModules(); |
43 | 48 | React = require('react'); |
44 | | - ReactDOM = require('react-dom'); |
45 | 49 | ReactDOMClient = require('react-dom/client'); |
46 | 50 | Scheduler = require('scheduler'); |
47 | 51 |
|
48 | 52 | const InternalTestUtils = require('internal-test-utils'); |
49 | 53 | assertLog = InternalTestUtils.assertLog; |
50 | 54 | waitForAll = InternalTestUtils.waitForAll; |
| 55 | + act = InternalTestUtils.act; |
51 | 56 |
|
52 | 57 | onClick = jest.fn(); |
53 | 58 | }); |
@@ -59,13 +64,13 @@ describe('SimpleEventPlugin', function () { |
59 | 64 | } |
60 | 65 | }); |
61 | 66 |
|
62 | | - it('A non-interactive tags click when disabled', function () { |
| 67 | + it('A non-interactive tags click when disabled', async function () { |
63 | 68 | const element = <div onClick={onClick} />; |
64 | | - expectClickThru(mounted(element)); |
| 69 | + await expectClickThru(await mounted(element)); |
65 | 70 | }); |
66 | 71 |
|
67 | | - it('A non-interactive tags clicks bubble when disabled', function () { |
68 | | - const element = mounted( |
| 72 | + it('A non-interactive tags clicks bubble when disabled', async function () { |
| 73 | + const element = await mounted( |
69 | 74 | <div onClick={onClick}> |
70 | 75 | <div /> |
71 | 76 | </div>, |
@@ -75,8 +80,8 @@ describe('SimpleEventPlugin', function () { |
75 | 80 | expect(onClick).toHaveBeenCalledTimes(1); |
76 | 81 | }); |
77 | 82 |
|
78 | | - it('does not register a click when clicking a child of a disabled element', function () { |
79 | | - const element = mounted( |
| 83 | + it('does not register a click when clicking a child of a disabled element', async function () { |
| 84 | + const element = await mounted( |
80 | 85 | <button onClick={onClick} disabled={true}> |
81 | 86 | <span /> |
82 | 87 | </button>, |
@@ -87,8 +92,8 @@ describe('SimpleEventPlugin', function () { |
87 | 92 | expect(onClick).toHaveBeenCalledTimes(0); |
88 | 93 | }); |
89 | 94 |
|
90 | | - it('triggers click events for children of disabled elements', function () { |
91 | | - const element = mounted( |
| 95 | + it('triggers click events for children of disabled elements', async function () { |
| 96 | + const element = await mounted( |
92 | 97 | <button disabled={true}> |
93 | 98 | <span onClick={onClick} /> |
94 | 99 | </button>, |
@@ -99,8 +104,8 @@ describe('SimpleEventPlugin', function () { |
99 | 104 | expect(onClick).toHaveBeenCalledTimes(1); |
100 | 105 | }); |
101 | 106 |
|
102 | | - it('triggers parent captured click events when target is a child of a disabled elements', function () { |
103 | | - const element = mounted( |
| 107 | + it('triggers parent captured click events when target is a child of a disabled elements', async function () { |
| 108 | + const element = await mounted( |
104 | 109 | <div onClickCapture={onClick}> |
105 | 110 | <button disabled={true}> |
106 | 111 | <span /> |
@@ -113,8 +118,8 @@ describe('SimpleEventPlugin', function () { |
113 | 118 | expect(onClick).toHaveBeenCalledTimes(1); |
114 | 119 | }); |
115 | 120 |
|
116 | | - it('triggers captured click events for children of disabled elements', function () { |
117 | | - const element = mounted( |
| 121 | + it('triggers captured click events for children of disabled elements', async function () { |
| 122 | + const element = await mounted( |
118 | 123 | <button disabled={true}> |
119 | 124 | <span onClickCapture={onClick} /> |
120 | 125 | </button>, |
@@ -127,68 +132,76 @@ describe('SimpleEventPlugin', function () { |
127 | 132 |
|
128 | 133 | ['button', 'input', 'select', 'textarea'].forEach(function (tagName) { |
129 | 134 | describe(tagName, function () { |
130 | | - it('should forward clicks when it starts out not disabled', () => { |
| 135 | + it('should forward clicks when it starts out not disabled', async () => { |
131 | 136 | const element = React.createElement(tagName, { |
132 | 137 | onClick: onClick, |
133 | 138 | }); |
134 | 139 |
|
135 | | - expectClickThru(mounted(element)); |
| 140 | + await expectClickThru(await mounted(element)); |
136 | 141 | }); |
137 | 142 |
|
138 | | - it('should not forward clicks when it starts out disabled', () => { |
| 143 | + it('should not forward clicks when it starts out disabled', async () => { |
139 | 144 | const element = React.createElement(tagName, { |
140 | 145 | onClick: onClick, |
141 | 146 | disabled: true, |
142 | 147 | }); |
143 | 148 |
|
144 | | - expectNoClickThru(mounted(element)); |
| 149 | + await expectNoClickThru(await mounted(element)); |
145 | 150 | }); |
146 | 151 |
|
147 | | - it('should forward clicks when it becomes not disabled', () => { |
| 152 | + it('should forward clicks when it becomes not disabled', async () => { |
148 | 153 | container = document.createElement('div'); |
149 | 154 | document.body.appendChild(container); |
150 | | - let element = ReactDOM.render( |
151 | | - React.createElement(tagName, {onClick: onClick, disabled: true}), |
152 | | - container, |
153 | | - ); |
154 | | - element = ReactDOM.render( |
155 | | - React.createElement(tagName, {onClick: onClick}), |
156 | | - container, |
157 | | - ); |
158 | | - expectClickThru(element); |
| 155 | + const root = ReactDOMClient.createRoot(container); |
| 156 | + await act(() => { |
| 157 | + root.render( |
| 158 | + React.createElement(tagName, {onClick: onClick, disabled: true}), |
| 159 | + ); |
| 160 | + }); |
| 161 | + await act(() => { |
| 162 | + root.render(React.createElement(tagName, {onClick: onClick})); |
| 163 | + }); |
| 164 | + const element = container.firstChild; |
| 165 | + await expectClickThru(element); |
159 | 166 | }); |
160 | 167 |
|
161 | | - it('should not forward clicks when it becomes disabled', () => { |
| 168 | + it('should not forward clicks when it becomes disabled', async () => { |
162 | 169 | container = document.createElement('div'); |
163 | 170 | document.body.appendChild(container); |
164 | | - let element = ReactDOM.render( |
165 | | - React.createElement(tagName, {onClick: onClick}), |
166 | | - container, |
167 | | - ); |
168 | | - element = ReactDOM.render( |
169 | | - React.createElement(tagName, {onClick: onClick, disabled: true}), |
170 | | - container, |
171 | | - ); |
| 171 | + const root = ReactDOMClient.createRoot(container); |
| 172 | + await act(() => { |
| 173 | + root.render(React.createElement(tagName, {onClick: onClick})); |
| 174 | + }); |
| 175 | + await act(() => { |
| 176 | + root.render( |
| 177 | + React.createElement(tagName, {onClick: onClick, disabled: true}), |
| 178 | + ); |
| 179 | + }); |
| 180 | + const element = container.firstChild; |
172 | 181 | expectNoClickThru(element); |
173 | 182 | }); |
174 | 183 |
|
175 | | - it('should work correctly if the listener is changed', () => { |
| 184 | + it('should work correctly if the listener is changed', async () => { |
176 | 185 | container = document.createElement('div'); |
177 | 186 | document.body.appendChild(container); |
178 | | - let element = ReactDOM.render( |
179 | | - React.createElement(tagName, {onClick: onClick, disabled: true}), |
180 | | - container, |
181 | | - ); |
182 | | - element = ReactDOM.render( |
183 | | - React.createElement(tagName, {onClick: onClick, disabled: false}), |
184 | | - container, |
185 | | - ); |
186 | | - expectClickThru(element); |
| 187 | + const root = ReactDOMClient.createRoot(container); |
| 188 | + await act(() => { |
| 189 | + root.render( |
| 190 | + React.createElement(tagName, {onClick: onClick, disabled: true}), |
| 191 | + ); |
| 192 | + }); |
| 193 | + await act(() => { |
| 194 | + root.render( |
| 195 | + React.createElement(tagName, {onClick: onClick, disabled: false}), |
| 196 | + ); |
| 197 | + }); |
| 198 | + const element = container.firstChild; |
| 199 | + await expectClickThru(element); |
187 | 200 | }); |
188 | 201 | }); |
189 | 202 | }); |
190 | 203 |
|
191 | | - it('batches updates that occur as a result of a nested event dispatch', () => { |
| 204 | + it('batches updates that occur as a result of a nested event dispatch', async () => { |
192 | 205 | container = document.createElement('div'); |
193 | 206 | document.body.appendChild(container); |
194 | 207 |
|
@@ -226,12 +239,17 @@ describe('SimpleEventPlugin', function () { |
226 | 239 | ); |
227 | 240 | } |
228 | 241 |
|
229 | | - ReactDOM.render(<Button />, container); |
| 242 | + const root = ReactDOMClient.createRoot(container); |
| 243 | + await act(() => { |
| 244 | + root.render(<Button />); |
| 245 | + }); |
| 246 | + |
230 | 247 | expect(button.textContent).toEqual('Count: 0'); |
231 | 248 | assertLog([]); |
232 | 249 |
|
233 | | - click(); |
234 | | - |
| 250 | + await act(() => { |
| 251 | + click(); |
| 252 | + }); |
235 | 253 | // There should be exactly one update. |
236 | 254 | assertLog(['didUpdate - Count: 3']); |
237 | 255 | expect(button.textContent).toEqual('Count: 3'); |
@@ -242,7 +260,6 @@ describe('SimpleEventPlugin', function () { |
242 | 260 | jest.resetModules(); |
243 | 261 |
|
244 | 262 | React = require('react'); |
245 | | - ReactDOM = require('react-dom'); |
246 | 263 | ReactDOMClient = require('react-dom/client'); |
247 | 264 | Scheduler = require('scheduler'); |
248 | 265 |
|
@@ -392,10 +409,13 @@ describe('SimpleEventPlugin', function () { |
392 | 409 | describe('iOS bubbling click fix', function () { |
393 | 410 | // See http://www.quirksmode.org/blog/archives/2010/09/click_event_del.html |
394 | 411 |
|
395 | | - it('does not add a local click to interactive elements', function () { |
| 412 | + it('does not add a local click to interactive elements', async function () { |
396 | 413 | container = document.createElement('div'); |
397 | 414 |
|
398 | | - ReactDOM.render(<button onClick={onClick} />, container); |
| 415 | + const root = ReactDOMClient.createRoot(container); |
| 416 | + await act(() => { |
| 417 | + root.render(<button onClick={onClick} />); |
| 418 | + }); |
399 | 419 |
|
400 | 420 | const node = container.firstChild; |
401 | 421 |
|
@@ -404,19 +424,24 @@ describe('SimpleEventPlugin', function () { |
404 | 424 | expect(onClick).toHaveBeenCalledTimes(0); |
405 | 425 | }); |
406 | 426 |
|
407 | | - it('adds a local click listener to non-interactive elements', function () { |
| 427 | + it('adds a local click listener to non-interactive elements', async function () { |
408 | 428 | container = document.createElement('div'); |
409 | 429 |
|
410 | | - ReactDOM.render(<div onClick={onClick} />, container); |
| 430 | + const root = ReactDOMClient.createRoot(container); |
| 431 | + await act(() => { |
| 432 | + root.render(<div onClick={onClick} />); |
| 433 | + }); |
411 | 434 |
|
412 | 435 | const node = container.firstChild; |
413 | 436 |
|
414 | | - node.dispatchEvent(new MouseEvent('click')); |
| 437 | + await act(() => { |
| 438 | + node.dispatchEvent(new MouseEvent('click')); |
| 439 | + }); |
415 | 440 |
|
416 | 441 | expect(onClick).toHaveBeenCalledTimes(0); |
417 | 442 | }); |
418 | 443 |
|
419 | | - it('registers passive handlers for events affected by the intervention', () => { |
| 444 | + it('registers passive handlers for events affected by the intervention', async () => { |
420 | 445 | container = document.createElement('div'); |
421 | 446 |
|
422 | 447 | const passiveEvents = []; |
@@ -430,7 +455,10 @@ describe('SimpleEventPlugin', function () { |
430 | 455 | return nativeAddEventListener.apply(this, arguments); |
431 | 456 | }; |
432 | 457 |
|
433 | | - ReactDOM.render(<div />, container); |
| 458 | + const root = ReactDOMClient.createRoot(container); |
| 459 | + await act(() => { |
| 460 | + root.render(<div />); |
| 461 | + }); |
434 | 462 |
|
435 | 463 | expect(passiveEvents).toEqual([ |
436 | 464 | 'touchstart', |
|