GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Convert ChangeEventPlugin to createRoot · react/react@808ac42

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/react-dom/src/events/plugins/__tests__
    • ChangeEventPlugin-test.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/react-dom/src/events/plugins/__tests__/ChangeEventPlugin-test.js‎

Copy file name to clipboardExpand all lines: packages/react-dom/src/events/plugins/__tests__/ChangeEventPlugin-test.js
+118-75Lines changed: 118 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -83,92 +83,106 @@ describe('ChangeEventPlugin', () => {
8383
// keep track of the "current" value and only fire events when it changes.
8484
// See https://github.com/facebook/react/pull/5746.
8585

86-
it('should consider initial text value to be current', () => {
86+
it('should consider initial text value to be current', async () => {
8787
let called = 0;
8888

8989
function cb(e) {
9090
called++;
9191
expect(e.type).toBe('change');
9292
}
9393

94-
const node = ReactDOM.render(
95-
<input type="text" onChange={cb} defaultValue="foo" />,
96-
container,
97-
);
94+
const root = ReactDOMClient.createRoot(container);
95+
await act(() => {
96+
root.render(<input type="text" onChange={cb} defaultValue="foo" />);
97+
});
98+
99+
const node = container.firstChild;
98100
node.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));
99101
node.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));
100102

101103
// There should be no React change events because the value stayed the same.
102104
expect(called).toBe(0);
103105
});
104106

105-
it('should consider initial text value to be current (capture)', () => {
107+
it('should consider initial text value to be current (capture)', async () => {
106108
let called = 0;
107109

108110
function cb(e) {
109111
called++;
110112
expect(e.type).toBe('change');
111113
}
112114

113-
const node = ReactDOM.render(
114-
<input type="text" onChangeCapture={cb} defaultValue="foo" />,
115-
container,
116-
);
115+
const root = ReactDOMClient.createRoot(container);
116+
await act(() => {
117+
root.render(
118+
<input type="text" onChangeCapture={cb} defaultValue="foo" />,
119+
);
120+
});
121+
122+
const node = container.firstChild;
117123
node.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));
118124
node.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));
119125

120126
// There should be no React change events because the value stayed the same.
121127
expect(called).toBe(0);
122128
});
123129

124-
it('should not invoke a change event for textarea same value', () => {
130+
it('should not invoke a change event for textarea same value', async () => {
125131
let called = 0;
126132

127133
function cb(e) {
128134
called++;
129135
expect(e.type).toBe('change');
130136
}
131137

132-
const node = ReactDOM.render(
133-
<textarea onChange={cb} defaultValue="initial" />,
134-
container,
135-
);
138+
const root = ReactDOMClient.createRoot(container);
139+
await act(() => {
140+
root.render(<textarea onChange={cb} defaultValue="initial" />);
141+
});
142+
143+
const node = container.firstChild;
136144
node.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));
137145
node.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));
138146
// There should be no React change events because the value stayed the same.
139147
expect(called).toBe(0);
140148
});
141149

142-
it('should not invoke a change event for textarea same value (capture)', () => {
150+
it('should not invoke a change event for textarea same value (capture)', async () => {
143151
let called = 0;
144152

145153
function cb(e) {
146154
called++;
147155
expect(e.type).toBe('change');
148156
}
149157

150-
const node = ReactDOM.render(
151-
<textarea onChangeCapture={cb} defaultValue="initial" />,
152-
container,
153-
);
158+
const root = ReactDOMClient.createRoot(container);
159+
await act(() => {
160+
root.render(<textarea onChangeCapture={cb} defaultValue="initial" />);
161+
});
162+
163+
const node = container.firstChild;
154164
node.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));
155165
node.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));
156166
// There should be no React change events because the value stayed the same.
157167
expect(called).toBe(0);
158168
});
159169

160-
it('should consider initial checkbox checked=true to be current', () => {
170+
it('should consider initial checkbox checked=true to be current', async () => {
161171
let called = 0;
162172

163173
function cb(e) {
164174
called++;
165175
expect(e.type).toBe('change');
166176
}
167177

168-
const node = ReactDOM.render(
169-
<input type="checkbox" onChange={cb} defaultChecked={true} />,
170-
container,
171-
);
178+
const root = ReactDOMClient.createRoot(container);
179+
await act(() => {
180+
root.render(
181+
<input type="checkbox" onChange={cb} defaultChecked={true} />,
182+
);
183+
});
184+
185+
const node = container.firstChild;
172186

173187
// Secretly, set `checked` to false, so that dispatching the `click` will
174188
// make it `true` again. Thus, at the time of the event, React should not
@@ -181,18 +195,22 @@ describe('ChangeEventPlugin', () => {
181195
expect(called).toBe(0);
182196
});
183197

184-
it('should consider initial checkbox checked=false to be current', () => {
198+
it('should consider initial checkbox checked=false to be current', async () => {
185199
let called = 0;
186200

187201
function cb(e) {
188202
called++;
189203
expect(e.type).toBe('change');
190204
}
191205

192-
const node = ReactDOM.render(
193-
<input type="checkbox" onChange={cb} defaultChecked={false} />,
194-
container,
195-
);
206+
const root = ReactDOMClient.createRoot(container);
207+
await act(() => {
208+
root.render(
209+
<input type="checkbox" onChange={cb} defaultChecked={false} />,
210+
);
211+
});
212+
213+
const node = container.firstChild;
196214

197215
// Secretly, set `checked` to true, so that dispatching the `click` will
198216
// make it `false` again. Thus, at the time of the event, React should not
@@ -205,18 +223,20 @@ describe('ChangeEventPlugin', () => {
205223
expect(called).toBe(0);
206224
});
207225

208-
it('should fire change for checkbox input', () => {
226+
it('should fire change for checkbox input', async () => {
209227
let called = 0;
210228

211229
function cb(e) {
212230
called++;
213231
expect(e.type).toBe('change');
214232
}
215233

216-
const node = ReactDOM.render(
217-
<input type="checkbox" onChange={cb} />,
218-
container,
219-
);
234+
const root = ReactDOMClient.createRoot(container);
235+
await act(() => {
236+
root.render(<input type="checkbox" onChange={cb} />);
237+
});
238+
239+
const node = container.firstChild;
220240

221241
expect(node.checked).toBe(false);
222242
node.dispatchEvent(
@@ -235,18 +255,20 @@ describe('ChangeEventPlugin', () => {
235255
expect(called).toBe(2);
236256
});
237257

238-
it('should not fire change setting the value programmatically', () => {
258+
it('should not fire change setting the value programmatically', async () => {
239259
let called = 0;
240260

241261
function cb(e) {
242262
called++;
243263
expect(e.type).toBe('change');
244264
}
245265

246-
const input = ReactDOM.render(
247-
<input type="text" defaultValue="foo" onChange={cb} />,
248-
container,
249-
);
266+
const root = ReactDOMClient.createRoot(container);
267+
await act(() => {
268+
root.render(<input type="text" defaultValue="foo" onChange={cb} />);
269+
});
270+
271+
const input = container.firstChild;
250272

251273
// Set it programmatically.
252274
input.value = 'bar';
@@ -271,18 +293,20 @@ describe('ChangeEventPlugin', () => {
271293
expect(called).toBe(1);
272294
});
273295

274-
it('should not distinguish equal string and number values', () => {
296+
it('should not distinguish equal string and number values', async () => {
275297
let called = 0;
276298

277299
function cb(e) {
278300
called++;
279301
expect(e.type).toBe('change');
280302
}
281303

282-
const input = ReactDOM.render(
283-
<input type="text" defaultValue="42" onChange={cb} />,
284-
container,
285-
);
304+
const root = ReactDOMClient.createRoot(container);
305+
await act(() => {
306+
root.render(<input type="text" defaultValue="42" onChange={cb} />);
307+
});
308+
309+
const input = container.firstChild;
286310

287311
// When we set `value` as a property, React updates the "current" value
288312
// that it tracks internally. The "current" value is later used to determine
@@ -296,18 +320,22 @@ describe('ChangeEventPlugin', () => {
296320
});
297321

298322
// See a similar input test above for a detailed description of why.
299-
it('should not fire change when setting checked programmatically', () => {
323+
it('should not fire change when setting checked programmatically', async () => {
300324
let called = 0;
301325

302326
function cb(e) {
303327
called++;
304328
expect(e.type).toBe('change');
305329
}
306330

307-
const input = ReactDOM.render(
308-
<input type="checkbox" onChange={cb} defaultChecked={false} />,
309-
container,
310-
);
331+
const root = ReactDOMClient.createRoot(container);
332+
await act(() => {
333+
root.render(
334+
<input type="checkbox" onChange={cb} defaultChecked={false} />,
335+
);
336+
});
337+
338+
const input = container.firstChild;
311339

312340
// Set the value, updating the "current" value that React tracks to true.
313341
input.checked = true;
@@ -326,32 +354,39 @@ describe('ChangeEventPlugin', () => {
326354
expect(called).toBe(1);
327355
});
328356

329-
it('should unmount', () => {
330-
const input = ReactDOM.render(<input />, container);
357+
it('should unmount', async () => {
358+
const root = ReactDOMClient.createRoot(container);
359+
await act(() => {
360+
root.render(<input />);
361+
});
362+
363+
const input = container.firstChild;
331364

332365
ReactDOM.unmountComponentAtNode(container);
333366
});
334367

335-
it('should only fire change for checked radio button once', () => {
368+
it('should only fire change for checked radio button once', async () => {
336369
let called = 0;
337370

338371
function cb(e) {
339372
called++;
340373
expect(e.type).toBe('change');
341374
}
342375

343-
const input = ReactDOM.render(
344-
<input type="radio" onChange={cb} />,
345-
container,
346-
);
376+
const root = ReactDOMClient.createRoot(container);
377+
await act(() => {
378+
root.render(<input type="radio" onChange={cb} />);
379+
});
380+
381+
const input = container.firstChild;
347382

348383
setUntrackedChecked.call(input, true);
349384
input.dispatchEvent(new Event('click', {bubbles: true, cancelable: true}));
350385
input.dispatchEvent(new Event('click', {bubbles: true, cancelable: true}));
351386
expect(called).toBe(1);
352387
});
353388

354-
it('should track radio button cousins in a group', () => {
389+
it('should track radio button cousins in a group', async () => {
355390
let called1 = 0;
356391
let called2 = 0;
357392

@@ -365,13 +400,17 @@ describe('ChangeEventPlugin', () => {
365400
expect(e.type).toBe('change');
366401
}
367402

368-
const div = ReactDOM.render(
369-
<div>
370-
<input type="radio" name="group" onChange={cb1} />
371-
<input type="radio" name="group" onChange={cb2} />
372-
</div>,
373-
container,
374-
);
403+
const root = ReactDOMClient.createRoot(container);
404+
await act(() => {
405+
root.render(
406+
<div>
407+
<input type="radio" name="group" onChange={cb1} />
408+
<input type="radio" name="group" onChange={cb2} />
409+
</div>,
410+
);
411+
});
412+
413+
const div = container.firstChild;
375414
const option1 = div.childNodes[0];
376415
const option2 = div.childNodes[1];
377416

@@ -455,18 +494,20 @@ describe('ChangeEventPlugin', () => {
455494
});
456495
});
457496

458-
it('should listen for both change and input events when supported', () => {
497+
it('should listen for both change and input events when supported', async () => {
459498
let called = 0;
460499

461500
function cb(e) {
462501
called++;
463502
expect(e.type).toBe('change');
464503
}
465504

466-
const input = ReactDOM.render(
467-
<input type="range" onChange={cb} />,
468-
container,
469-
);
505+
const root = ReactDOMClient.createRoot(container);
506+
await act(() => {
507+
root.render(<input type="range" onChange={cb} />);
508+
});
509+
510+
const input = container.firstChild;
470511

471512
setUntrackedValue.call(input, 10);
472513
input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));
@@ -477,18 +518,20 @@ describe('ChangeEventPlugin', () => {
477518
expect(called).toBe(2);
478519
});
479520

480-
it('should only fire events when the value changes for range inputs', () => {
521+
it('should only fire events when the value changes for range inputs', async () => {
481522
let called = 0;
482523

483524
function cb(e) {
484525
called++;
485526
expect(e.type).toBe('change');
486527
}
487528

488-
const input = ReactDOM.render(
489-
<input type="range" onChange={cb} />,
490-
container,
491-
);
529+
const root = ReactDOMClient.createRoot(container);
530+
await act(() => {
531+
root.render(<input type="range" onChange={cb} />);
532+
});
533+
534+
const input = container.firstChild;
492535
setUntrackedValue.call(input, '40');
493536
input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));
494537
input.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.