10
10
'use strict' ;
11
11
12
12
let React ;
13
- let ReactDOM ;
13
+ let ReactDOMClient ;
14
+
15
+ let act ;
14
16
15
17
describe ( 'getEventKey' , ( ) => {
16
18
let container ;
19
+ let root ;
17
20
18
21
beforeEach ( ( ) => {
19
22
React = require ( 'react' ) ;
20
- ReactDOM = require ( 'react-dom' ) ;
23
+ ReactDOMClient = require ( 'react-dom/client' ) ;
24
+
25
+ act = require ( 'internal-test-utils' ) . act ;
21
26
22
27
// The container has to be attached for events to fire.
23
28
container = document . createElement ( 'div' ) ;
29
+ root = ReactDOMClient . createRoot ( container ) ;
24
30
document . body . appendChild ( container ) ;
25
31
} ) ;
26
32
27
- afterEach ( ( ) => {
33
+ afterEach ( async ( ) => {
34
+ await act ( ( ) => {
35
+ root . unmount ( ) ;
36
+ } ) ;
28
37
document . body . removeChild ( container ) ;
29
38
container = null ;
39
+ root = null ;
30
40
} ) ;
31
41
32
42
describe ( 'when key is implemented in a browser' , ( ) => {
33
43
describe ( 'when key is not normalized' , ( ) => {
34
- it ( 'returns a normalized value' , ( ) => {
44
+ it ( 'returns a normalized value' , async ( ) => {
35
45
let key = null ;
36
46
class Comp extends React . Component {
37
47
render ( ) {
38
48
return < input onKeyDown = { e => ( key = e . key ) } /> ;
39
49
}
40
50
}
41
51
42
- ReactDOM . render ( < Comp /> , container ) ;
52
+ await act ( ( ) => {
53
+ root . render ( < Comp /> ) ;
54
+ } ) ;
43
55
44
56
const nativeEvent = new KeyboardEvent ( 'keydown' , {
45
57
key : 'Del' ,
@@ -52,15 +64,17 @@ describe('getEventKey', () => {
52
64
} ) ;
53
65
54
66
describe ( 'when key is normalized' , ( ) => {
55
- it ( 'returns a key' , ( ) => {
67
+ it ( 'returns a key' , async ( ) => {
56
68
let key = null ;
57
69
class Comp extends React . Component {
58
70
render ( ) {
59
71
return < input onKeyDown = { e => ( key = e . key ) } /> ;
60
72
}
61
73
}
62
74
63
- ReactDOM . render ( < Comp /> , container ) ;
75
+ await act ( ( ) => {
76
+ root . render ( < Comp /> ) ;
77
+ } ) ;
64
78
65
79
const nativeEvent = new KeyboardEvent ( 'keydown' , {
66
80
key : 'f' ,
@@ -76,15 +90,17 @@ describe('getEventKey', () => {
76
90
describe ( 'when key is not implemented in a browser' , ( ) => {
77
91
describe ( 'when event type is keypress' , ( ) => {
78
92
describe ( 'when charCode is 13' , ( ) => {
79
- it ( 'returns "Enter"' , ( ) => {
93
+ it ( 'returns "Enter"' , async ( ) => {
80
94
let key = null ;
81
95
class Comp extends React . Component {
82
96
render ( ) {
83
97
return < input onKeyPress = { e => ( key = e . key ) } /> ;
84
98
}
85
99
}
86
100
87
- ReactDOM . render ( < Comp /> , container ) ;
101
+ await act ( ( ) => {
102
+ root . render ( < Comp /> ) ;
103
+ } ) ;
88
104
89
105
const nativeEvent = new KeyboardEvent ( 'keypress' , {
90
106
charCode : 13 ,
@@ -97,15 +113,17 @@ describe('getEventKey', () => {
97
113
} ) ;
98
114
99
115
describe ( 'when charCode is not 13' , ( ) => {
100
- it ( 'returns a string from a charCode' , ( ) => {
116
+ it ( 'returns a string from a charCode' , async ( ) => {
101
117
let key = null ;
102
118
class Comp extends React . Component {
103
119
render ( ) {
104
120
return < input onKeyPress = { e => ( key = e . key ) } /> ;
105
121
}
106
122
}
107
123
108
- ReactDOM . render ( < Comp /> , container ) ;
124
+ await act ( ( ) => {
125
+ root . render ( < Comp /> ) ;
126
+ } ) ;
109
127
110
128
const nativeEvent = new KeyboardEvent ( 'keypress' , {
111
129
charCode : 65 ,
@@ -120,15 +138,17 @@ describe('getEventKey', () => {
120
138
121
139
describe ( 'when event type is keydown or keyup' , ( ) => {
122
140
describe ( 'when keyCode is recognized' , ( ) => {
123
- it ( 'returns a translated key' , ( ) => {
141
+ it ( 'returns a translated key' , async ( ) => {
124
142
let key = null ;
125
143
class Comp extends React . Component {
126
144
render ( ) {
127
145
return < input onKeyDown = { e => ( key = e . key ) } /> ;
128
146
}
129
147
}
130
148
131
- ReactDOM . render ( < Comp /> , container ) ;
149
+ await act ( ( ) => {
150
+ root . render ( < Comp /> ) ;
151
+ } ) ;
132
152
133
153
const nativeEvent = new KeyboardEvent ( 'keydown' , {
134
154
keyCode : 45 ,
@@ -141,15 +161,17 @@ describe('getEventKey', () => {
141
161
} ) ;
142
162
143
163
describe ( 'when keyCode is not recognized' , ( ) => {
144
- it ( 'returns Unidentified' , ( ) => {
164
+ it ( 'returns Unidentified' , async ( ) => {
145
165
let key = null ;
146
166
class Comp extends React . Component {
147
167
render ( ) {
148
168
return < input onKeyDown = { e => ( key = e . key ) } /> ;
149
169
}
150
170
}
151
171
152
- ReactDOM . render ( < Comp /> , container ) ;
172
+ await act ( ( ) => {
173
+ root . render ( < Comp /> ) ;
174
+ } ) ;
153
175
154
176
const nativeEvent = new KeyboardEvent ( 'keydown' , {
155
177
keyCode : 1337 ,
0 commit comments