1010'use strict' ;
1111
1212let React ;
13- let ReactDOM ;
13+ let ReactDOMClient ;
14+
15+ let act ;
1416
1517describe ( 'getEventKey' , ( ) => {
1618 let container ;
19+ let root ;
1720
1821 beforeEach ( ( ) => {
1922 React = require ( 'react' ) ;
20- ReactDOM = require ( 'react-dom' ) ;
23+ ReactDOMClient = require ( 'react-dom/client' ) ;
24+
25+ act = require ( 'internal-test-utils' ) . act ;
2126
2227 // The container has to be attached for events to fire.
2328 container = document . createElement ( 'div' ) ;
29+ root = ReactDOMClient . createRoot ( container ) ;
2430 document . body . appendChild ( container ) ;
2531 } ) ;
2632
27- afterEach ( ( ) => {
33+ afterEach ( async ( ) => {
34+ await act ( ( ) => {
35+ root . unmount ( ) ;
36+ } ) ;
2837 document . body . removeChild ( container ) ;
2938 container = null ;
39+ root = null ;
3040 } ) ;
3141
3242 describe ( 'when key is implemented in a browser' , ( ) => {
3343 describe ( 'when key is not normalized' , ( ) => {
34- it ( 'returns a normalized value' , ( ) => {
44+ it ( 'returns a normalized value' , async ( ) => {
3545 let key = null ;
3646 class Comp extends React . Component {
3747 render ( ) {
3848 return < input onKeyDown = { e => ( key = e . key ) } /> ;
3949 }
4050 }
4151
42- ReactDOM . render ( < Comp /> , container ) ;
52+ await act ( ( ) => {
53+ root . render ( < Comp /> ) ;
54+ } ) ;
4355
4456 const nativeEvent = new KeyboardEvent ( 'keydown' , {
4557 key : 'Del' ,
@@ -52,15 +64,17 @@ describe('getEventKey', () => {
5264 } ) ;
5365
5466 describe ( 'when key is normalized' , ( ) => {
55- it ( 'returns a key' , ( ) => {
67+ it ( 'returns a key' , async ( ) => {
5668 let key = null ;
5769 class Comp extends React . Component {
5870 render ( ) {
5971 return < input onKeyDown = { e => ( key = e . key ) } /> ;
6072 }
6173 }
6274
63- ReactDOM . render ( < Comp /> , container ) ;
75+ await act ( ( ) => {
76+ root . render ( < Comp /> ) ;
77+ } ) ;
6478
6579 const nativeEvent = new KeyboardEvent ( 'keydown' , {
6680 key : 'f' ,
@@ -76,15 +90,17 @@ describe('getEventKey', () => {
7690 describe ( 'when key is not implemented in a browser' , ( ) => {
7791 describe ( 'when event type is keypress' , ( ) => {
7892 describe ( 'when charCode is 13' , ( ) => {
79- it ( 'returns "Enter"' , ( ) => {
93+ it ( 'returns "Enter"' , async ( ) => {
8094 let key = null ;
8195 class Comp extends React . Component {
8296 render ( ) {
8397 return < input onKeyPress = { e => ( key = e . key ) } /> ;
8498 }
8599 }
86100
87- ReactDOM . render ( < Comp /> , container ) ;
101+ await act ( ( ) => {
102+ root . render ( < Comp /> ) ;
103+ } ) ;
88104
89105 const nativeEvent = new KeyboardEvent ( 'keypress' , {
90106 charCode : 13 ,
@@ -97,15 +113,17 @@ describe('getEventKey', () => {
97113 } ) ;
98114
99115 describe ( 'when charCode is not 13' , ( ) => {
100- it ( 'returns a string from a charCode' , ( ) => {
116+ it ( 'returns a string from a charCode' , async ( ) => {
101117 let key = null ;
102118 class Comp extends React . Component {
103119 render ( ) {
104120 return < input onKeyPress = { e => ( key = e . key ) } /> ;
105121 }
106122 }
107123
108- ReactDOM . render ( < Comp /> , container ) ;
124+ await act ( ( ) => {
125+ root . render ( < Comp /> ) ;
126+ } ) ;
109127
110128 const nativeEvent = new KeyboardEvent ( 'keypress' , {
111129 charCode : 65 ,
@@ -120,15 +138,17 @@ describe('getEventKey', () => {
120138
121139 describe ( 'when event type is keydown or keyup' , ( ) => {
122140 describe ( 'when keyCode is recognized' , ( ) => {
123- it ( 'returns a translated key' , ( ) => {
141+ it ( 'returns a translated key' , async ( ) => {
124142 let key = null ;
125143 class Comp extends React . Component {
126144 render ( ) {
127145 return < input onKeyDown = { e => ( key = e . key ) } /> ;
128146 }
129147 }
130148
131- ReactDOM . render ( < Comp /> , container ) ;
149+ await act ( ( ) => {
150+ root . render ( < Comp /> ) ;
151+ } ) ;
132152
133153 const nativeEvent = new KeyboardEvent ( 'keydown' , {
134154 keyCode : 45 ,
@@ -141,15 +161,17 @@ describe('getEventKey', () => {
141161 } ) ;
142162
143163 describe ( 'when keyCode is not recognized' , ( ) => {
144- it ( 'returns Unidentified' , ( ) => {
164+ it ( 'returns Unidentified' , async ( ) => {
145165 let key = null ;
146166 class Comp extends React . Component {
147167 render ( ) {
148168 return < input onKeyDown = { e => ( key = e . key ) } /> ;
149169 }
150170 }
151171
152- ReactDOM . render ( < Comp /> , container ) ;
172+ await act ( ( ) => {
173+ root . render ( < Comp /> ) ;
174+ } ) ;
153175
154176 const nativeEvent = new KeyboardEvent ( 'keydown' , {
155177 keyCode : 1337 ,
0 commit comments