11
11
12
12
describe ( 'when Trusted Types are available in global object' , ( ) => {
13
13
let React ;
14
- let ReactDOM ;
14
+ let ReactDOMClient ;
15
15
let ReactFeatureFlags ;
16
+ let act ;
16
17
let container ;
17
18
let ttObject1 ;
18
19
let ttObject2 ;
@@ -34,7 +35,8 @@ describe('when Trusted Types are available in global object', () => {
34
35
ReactFeatureFlags = require ( 'shared/ReactFeatureFlags' ) ;
35
36
ReactFeatureFlags . enableTrustedTypesIntegration = true ;
36
37
React = require ( 'react' ) ;
37
- ReactDOM = require ( 'react-dom' ) ;
38
+ ReactDOMClient = require ( 'react-dom/client' ) ;
39
+ act = require ( 'internal-test-utils' ) . act ;
38
40
ttObject1 = {
39
41
toString ( ) {
40
42
return '<b>Hi</b>' ;
@@ -53,7 +55,7 @@ describe('when Trusted Types are available in global object', () => {
53
55
delete window . trustedTypes ;
54
56
} ) ;
55
57
56
- it ( 'should not stringify trusted values for dangerouslySetInnerHTML' , ( ) => {
58
+ it ( 'should not stringify trusted values for dangerouslySetInnerHTML' , async ( ) => {
57
59
const innerHTMLDescriptor = Object . getOwnPropertyDescriptor (
58
60
Element . prototype ,
59
61
'innerHTML' ,
@@ -69,20 +71,21 @@ describe('when Trusted Types are available in global object', () => {
69
71
return innerHTMLDescriptor . set . apply ( this , arguments ) ;
70
72
} ,
71
73
} ) ;
72
- ReactDOM . render (
73
- < div dangerouslySetInnerHTML = { { __html : ttObject1 } } /> ,
74
- container ,
75
- ) ;
74
+ const root = ReactDOMClient . createRoot ( container ) ;
75
+ await act ( ( ) => {
76
+ root . render ( < div dangerouslySetInnerHTML = { { __html : ttObject1 } } /> ) ;
77
+ } ) ;
78
+
76
79
expect ( container . innerHTML ) . toBe ( '<div><b>Hi</b></div>' ) ;
77
80
expect ( innerHTMLCalls . length ) . toBe ( 1 ) ;
78
81
// Ensure it didn't get stringified when passed to a DOM sink:
79
82
expect ( innerHTMLCalls [ 0 ] ) . toBe ( ttObject1 ) ;
80
83
81
84
innerHTMLCalls . length = 0 ;
82
- ReactDOM . render (
83
- < div dangerouslySetInnerHTML = { { __html : ttObject2 } } /> ,
84
- container ,
85
- ) ;
85
+ await act ( ( ) => {
86
+ root . render ( < div dangerouslySetInnerHTML = { { __html : ttObject2 } } /> ) ;
87
+ } ) ;
88
+
86
89
expect ( container . innerHTML ) . toBe ( '<div><b>Bye</b></div>' ) ;
87
90
expect ( innerHTMLCalls . length ) . toBe ( 1 ) ;
88
91
// Ensure it didn't get stringified when passed to a DOM sink:
@@ -96,15 +99,19 @@ describe('when Trusted Types are available in global object', () => {
96
99
}
97
100
} ) ;
98
101
99
- it ( 'should not stringify trusted values for setAttribute (unknown attribute)' , ( ) => {
102
+ it ( 'should not stringify trusted values for setAttribute (unknown attribute)' , async ( ) => {
100
103
const setAttribute = Element . prototype . setAttribute ;
101
104
try {
102
105
const setAttributeCalls = [ ] ;
103
106
Element . prototype . setAttribute = function ( name , value ) {
104
107
setAttributeCalls . push ( [ this , name . toLowerCase ( ) , value ] ) ;
105
108
return setAttribute . apply ( this , arguments ) ;
106
109
} ;
107
- ReactDOM . render ( < div data-foo = { ttObject1 } /> , container ) ;
110
+ const root = ReactDOMClient . createRoot ( container ) ;
111
+ await act ( ( ) => {
112
+ root . render ( < div data-foo = { ttObject1 } /> ) ;
113
+ } ) ;
114
+
108
115
expect ( container . innerHTML ) . toBe ( '<div data-foo="<b>Hi</b>"></div>' ) ;
109
116
expect ( setAttributeCalls . length ) . toBe ( 1 ) ;
110
117
expect ( setAttributeCalls [ 0 ] [ 0 ] ) . toBe ( container . firstChild ) ;
@@ -113,7 +120,10 @@ describe('when Trusted Types are available in global object', () => {
113
120
expect ( setAttributeCalls [ 0 ] [ 2 ] ) . toBe ( ttObject1 ) ;
114
121
115
122
setAttributeCalls . length = 0 ;
116
- ReactDOM . render ( < div data-foo = { ttObject2 } /> , container ) ;
123
+ await act ( ( ) => {
124
+ root . render ( < div data-foo = { ttObject2 } /> ) ;
125
+ } ) ;
126
+
117
127
expect ( setAttributeCalls . length ) . toBe ( 1 ) ;
118
128
expect ( setAttributeCalls [ 0 ] [ 0 ] ) . toBe ( container . firstChild ) ;
119
129
expect ( setAttributeCalls [ 0 ] [ 1 ] ) . toBe ( 'data-foo' ) ;
@@ -124,15 +134,19 @@ describe('when Trusted Types are available in global object', () => {
124
134
}
125
135
} ) ;
126
136
127
- it ( 'should not stringify trusted values for setAttribute (known attribute)' , ( ) => {
137
+ it ( 'should not stringify trusted values for setAttribute (known attribute)' , async ( ) => {
128
138
const setAttribute = Element . prototype . setAttribute ;
129
139
try {
130
140
const setAttributeCalls = [ ] ;
131
141
Element . prototype . setAttribute = function ( name , value ) {
132
142
setAttributeCalls . push ( [ this , name . toLowerCase ( ) , value ] ) ;
133
143
return setAttribute . apply ( this , arguments ) ;
134
144
} ;
135
- ReactDOM . render ( < div className = { ttObject1 } /> , container ) ;
145
+ const root = ReactDOMClient . createRoot ( container ) ;
146
+ await act ( ( ) => {
147
+ root . render ( < div className = { ttObject1 } /> ) ;
148
+ } ) ;
149
+
136
150
expect ( container . innerHTML ) . toBe ( '<div class="<b>Hi</b>"></div>' ) ;
137
151
expect ( setAttributeCalls . length ) . toBe ( 1 ) ;
138
152
expect ( setAttributeCalls [ 0 ] [ 0 ] ) . toBe ( container . firstChild ) ;
@@ -141,7 +155,10 @@ describe('when Trusted Types are available in global object', () => {
141
155
expect ( setAttributeCalls [ 0 ] [ 2 ] ) . toBe ( ttObject1 ) ;
142
156
143
157
setAttributeCalls . length = 0 ;
144
- ReactDOM . render ( < div className = { ttObject2 } /> , container ) ;
158
+ await act ( ( ) => {
159
+ root . render ( < div className = { ttObject2 } /> ) ;
160
+ } ) ;
161
+
145
162
expect ( setAttributeCalls . length ) . toBe ( 1 ) ;
146
163
expect ( setAttributeCalls [ 0 ] [ 0 ] ) . toBe ( container . firstChild ) ;
147
164
expect ( setAttributeCalls [ 0 ] [ 1 ] ) . toBe ( 'class' ) ;
@@ -152,15 +169,19 @@ describe('when Trusted Types are available in global object', () => {
152
169
}
153
170
} ) ;
154
171
155
- it ( 'should not stringify trusted values for setAttributeNS' , ( ) => {
172
+ it ( 'should not stringify trusted values for setAttributeNS' , async ( ) => {
156
173
const setAttributeNS = Element . prototype . setAttributeNS ;
157
174
try {
158
175
const setAttributeNSCalls = [ ] ;
159
176
Element . prototype . setAttributeNS = function ( ns , name , value ) {
160
177
setAttributeNSCalls . push ( [ this , ns , name , value ] ) ;
161
178
return setAttributeNS . apply ( this , arguments ) ;
162
179
} ;
163
- ReactDOM . render ( < svg xlinkHref = { ttObject1 } /> , container ) ;
180
+ const root = ReactDOMClient . createRoot ( container ) ;
181
+ await act ( ( ) => {
182
+ root . render ( < svg xlinkHref = { ttObject1 } /> ) ;
183
+ } ) ;
184
+
164
185
expect ( container . innerHTML ) . toBe ( '<svg xlink:href="<b>Hi</b>"></svg>' ) ;
165
186
expect ( setAttributeNSCalls . length ) . toBe ( 1 ) ;
166
187
expect ( setAttributeNSCalls [ 0 ] [ 0 ] ) . toBe ( container . firstChild ) ;
@@ -170,7 +191,10 @@ describe('when Trusted Types are available in global object', () => {
170
191
expect ( setAttributeNSCalls [ 0 ] [ 3 ] ) . toBe ( ttObject1 ) ;
171
192
172
193
setAttributeNSCalls . length = 0 ;
173
- ReactDOM . render ( < svg xlinkHref = { ttObject2 } /> , container ) ;
194
+ await act ( ( ) => {
195
+ root . render ( < svg xlinkHref = { ttObject2 } /> ) ;
196
+ } ) ;
197
+
174
198
expect ( setAttributeNSCalls . length ) . toBe ( 1 ) ;
175
199
expect ( setAttributeNSCalls [ 0 ] [ 0 ] ) . toBe ( container . firstChild ) ;
176
200
expect ( setAttributeNSCalls [ 0 ] [ 1 ] ) . toBe ( 'http://www.w3.org/1999/xlink' ) ;
@@ -209,14 +233,17 @@ describe('when Trusted Types are available in global object', () => {
209
233
} ) ;
210
234
211
235
// @gate !disableIEWorkarounds
212
- it ( 'should log a warning' , ( ) => {
236
+ it ( 'should log a warning' , async ( ) => {
213
237
class Component extends React . Component {
214
238
render ( ) {
215
239
return < svg dangerouslySetInnerHTML = { { __html : 'unsafe html' } } /> ;
216
240
}
217
241
}
218
- expect ( ( ) => {
219
- ReactDOM . render ( < Component /> , container ) ;
242
+ const root = ReactDOMClient . createRoot ( container ) ;
243
+ await expect ( async ( ) => {
244
+ await act ( ( ) => {
245
+ root . render ( < Component /> ) ;
246
+ } ) ;
220
247
} ) . toErrorDev (
221
248
"Warning: Using 'dangerouslySetInnerHTML' in an svg element with " +
222
249
'Trusted Types enabled in an Internet Explorer will cause ' +
@@ -229,9 +256,12 @@ describe('when Trusted Types are available in global object', () => {
229
256
} ) ;
230
257
} ) ;
231
258
232
- it ( 'should warn once when rendering script tag in jsx on client' , ( ) => {
233
- expect ( ( ) => {
234
- ReactDOM . render ( < script > alert("I am not executed")</ script > , container ) ;
259
+ it ( 'should warn once when rendering script tag in jsx on client' , async ( ) => {
260
+ const root = ReactDOMClient . createRoot ( container ) ;
261
+ await expect ( async ( ) => {
262
+ await act ( ( ) => {
263
+ root . render ( < script > alert("I am not executed")</ script > ) ;
264
+ } ) ;
235
265
} ) . toErrorDev (
236
266
'Warning: Encountered a script tag while rendering React component. ' +
237
267
'Scripts inside React components are never executed when rendering ' +
@@ -241,6 +271,8 @@ describe('when Trusted Types are available in global object', () => {
241
271
) ;
242
272
243
273
// check that the warning is printed only once
244
- ReactDOM . render ( < script > alert("I am not executed")</ script > , container ) ;
274
+ await act ( ( ) => {
275
+ root . render ( < script > alert("I am not executed")</ script > ) ;
276
+ } ) ;
245
277
} ) ;
246
278
} ) ;
0 commit comments