1111
1212describe ( 'when Trusted Types are available in global object' , ( ) => {
1313 let React ;
14- let ReactDOM ;
14+ let ReactDOMClient ;
1515 let ReactFeatureFlags ;
16+ let act ;
1617 let container ;
1718 let ttObject1 ;
1819 let ttObject2 ;
@@ -34,7 +35,8 @@ describe('when Trusted Types are available in global object', () => {
3435 ReactFeatureFlags = require ( 'shared/ReactFeatureFlags' ) ;
3536 ReactFeatureFlags . enableTrustedTypesIntegration = true ;
3637 React = require ( 'react' ) ;
37- ReactDOM = require ( 'react-dom' ) ;
38+ ReactDOMClient = require ( 'react-dom/client' ) ;
39+ act = require ( 'internal-test-utils' ) . act ;
3840 ttObject1 = {
3941 toString ( ) {
4042 return '<b>Hi</b>' ;
@@ -53,7 +55,7 @@ describe('when Trusted Types are available in global object', () => {
5355 delete window . trustedTypes ;
5456 } ) ;
5557
56- it ( 'should not stringify trusted values for dangerouslySetInnerHTML' , ( ) => {
58+ it ( 'should not stringify trusted values for dangerouslySetInnerHTML' , async ( ) => {
5759 const innerHTMLDescriptor = Object . getOwnPropertyDescriptor (
5860 Element . prototype ,
5961 'innerHTML' ,
@@ -69,20 +71,21 @@ describe('when Trusted Types are available in global object', () => {
6971 return innerHTMLDescriptor . set . apply ( this , arguments ) ;
7072 } ,
7173 } ) ;
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+
7679 expect ( container . innerHTML ) . toBe ( '<div><b>Hi</b></div>' ) ;
7780 expect ( innerHTMLCalls . length ) . toBe ( 1 ) ;
7881 // Ensure it didn't get stringified when passed to a DOM sink:
7982 expect ( innerHTMLCalls [ 0 ] ) . toBe ( ttObject1 ) ;
8083
8184 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+
8689 expect ( container . innerHTML ) . toBe ( '<div><b>Bye</b></div>' ) ;
8790 expect ( innerHTMLCalls . length ) . toBe ( 1 ) ;
8891 // 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', () => {
9699 }
97100 } ) ;
98101
99- it ( 'should not stringify trusted values for setAttribute (unknown attribute)' , ( ) => {
102+ it ( 'should not stringify trusted values for setAttribute (unknown attribute)' , async ( ) => {
100103 const setAttribute = Element . prototype . setAttribute ;
101104 try {
102105 const setAttributeCalls = [ ] ;
103106 Element . prototype . setAttribute = function ( name , value ) {
104107 setAttributeCalls . push ( [ this , name . toLowerCase ( ) , value ] ) ;
105108 return setAttribute . apply ( this , arguments ) ;
106109 } ;
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+
108115 expect ( container . innerHTML ) . toBe ( '<div data-foo="<b>Hi</b>"></div>' ) ;
109116 expect ( setAttributeCalls . length ) . toBe ( 1 ) ;
110117 expect ( setAttributeCalls [ 0 ] [ 0 ] ) . toBe ( container . firstChild ) ;
@@ -113,7 +120,10 @@ describe('when Trusted Types are available in global object', () => {
113120 expect ( setAttributeCalls [ 0 ] [ 2 ] ) . toBe ( ttObject1 ) ;
114121
115122 setAttributeCalls . length = 0 ;
116- ReactDOM . render ( < div data-foo = { ttObject2 } /> , container ) ;
123+ await act ( ( ) => {
124+ root . render ( < div data-foo = { ttObject2 } /> ) ;
125+ } ) ;
126+
117127 expect ( setAttributeCalls . length ) . toBe ( 1 ) ;
118128 expect ( setAttributeCalls [ 0 ] [ 0 ] ) . toBe ( container . firstChild ) ;
119129 expect ( setAttributeCalls [ 0 ] [ 1 ] ) . toBe ( 'data-foo' ) ;
@@ -124,15 +134,19 @@ describe('when Trusted Types are available in global object', () => {
124134 }
125135 } ) ;
126136
127- it ( 'should not stringify trusted values for setAttribute (known attribute)' , ( ) => {
137+ it ( 'should not stringify trusted values for setAttribute (known attribute)' , async ( ) => {
128138 const setAttribute = Element . prototype . setAttribute ;
129139 try {
130140 const setAttributeCalls = [ ] ;
131141 Element . prototype . setAttribute = function ( name , value ) {
132142 setAttributeCalls . push ( [ this , name . toLowerCase ( ) , value ] ) ;
133143 return setAttribute . apply ( this , arguments ) ;
134144 } ;
135- ReactDOM . render ( < div className = { ttObject1 } /> , container ) ;
145+ const root = ReactDOMClient . createRoot ( container ) ;
146+ await act ( ( ) => {
147+ root . render ( < div className = { ttObject1 } /> ) ;
148+ } ) ;
149+
136150 expect ( container . innerHTML ) . toBe ( '<div class="<b>Hi</b>"></div>' ) ;
137151 expect ( setAttributeCalls . length ) . toBe ( 1 ) ;
138152 expect ( setAttributeCalls [ 0 ] [ 0 ] ) . toBe ( container . firstChild ) ;
@@ -141,7 +155,10 @@ describe('when Trusted Types are available in global object', () => {
141155 expect ( setAttributeCalls [ 0 ] [ 2 ] ) . toBe ( ttObject1 ) ;
142156
143157 setAttributeCalls . length = 0 ;
144- ReactDOM . render ( < div className = { ttObject2 } /> , container ) ;
158+ await act ( ( ) => {
159+ root . render ( < div className = { ttObject2 } /> ) ;
160+ } ) ;
161+
145162 expect ( setAttributeCalls . length ) . toBe ( 1 ) ;
146163 expect ( setAttributeCalls [ 0 ] [ 0 ] ) . toBe ( container . firstChild ) ;
147164 expect ( setAttributeCalls [ 0 ] [ 1 ] ) . toBe ( 'class' ) ;
@@ -152,15 +169,19 @@ describe('when Trusted Types are available in global object', () => {
152169 }
153170 } ) ;
154171
155- it ( 'should not stringify trusted values for setAttributeNS' , ( ) => {
172+ it ( 'should not stringify trusted values for setAttributeNS' , async ( ) => {
156173 const setAttributeNS = Element . prototype . setAttributeNS ;
157174 try {
158175 const setAttributeNSCalls = [ ] ;
159176 Element . prototype . setAttributeNS = function ( ns , name , value ) {
160177 setAttributeNSCalls . push ( [ this , ns , name , value ] ) ;
161178 return setAttributeNS . apply ( this , arguments ) ;
162179 } ;
163- ReactDOM . render ( < svg xlinkHref = { ttObject1 } /> , container ) ;
180+ const root = ReactDOMClient . createRoot ( container ) ;
181+ await act ( ( ) => {
182+ root . render ( < svg xlinkHref = { ttObject1 } /> ) ;
183+ } ) ;
184+
164185 expect ( container . innerHTML ) . toBe ( '<svg xlink:href="<b>Hi</b>"></svg>' ) ;
165186 expect ( setAttributeNSCalls . length ) . toBe ( 1 ) ;
166187 expect ( setAttributeNSCalls [ 0 ] [ 0 ] ) . toBe ( container . firstChild ) ;
@@ -170,7 +191,10 @@ describe('when Trusted Types are available in global object', () => {
170191 expect ( setAttributeNSCalls [ 0 ] [ 3 ] ) . toBe ( ttObject1 ) ;
171192
172193 setAttributeNSCalls . length = 0 ;
173- ReactDOM . render ( < svg xlinkHref = { ttObject2 } /> , container ) ;
194+ await act ( ( ) => {
195+ root . render ( < svg xlinkHref = { ttObject2 } /> ) ;
196+ } ) ;
197+
174198 expect ( setAttributeNSCalls . length ) . toBe ( 1 ) ;
175199 expect ( setAttributeNSCalls [ 0 ] [ 0 ] ) . toBe ( container . firstChild ) ;
176200 expect ( setAttributeNSCalls [ 0 ] [ 1 ] ) . toBe ( 'http://www.w3.org/1999/xlink' ) ;
@@ -209,14 +233,17 @@ describe('when Trusted Types are available in global object', () => {
209233 } ) ;
210234
211235 // @gate !disableIEWorkarounds
212- it ( 'should log a warning' , ( ) => {
236+ it ( 'should log a warning' , async ( ) => {
213237 class Component extends React . Component {
214238 render ( ) {
215239 return < svg dangerouslySetInnerHTML = { { __html : 'unsafe html' } } /> ;
216240 }
217241 }
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+ } ) ;
220247 } ) . toErrorDev (
221248 "Warning: Using 'dangerouslySetInnerHTML' in an svg element with " +
222249 'Trusted Types enabled in an Internet Explorer will cause ' +
@@ -229,9 +256,12 @@ describe('when Trusted Types are available in global object', () => {
229256 } ) ;
230257 } ) ;
231258
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+ } ) ;
235265 } ) . toErrorDev (
236266 'Warning: Encountered a script tag while rendering React component. ' +
237267 'Scripts inside React components are never executed when rendering ' +
@@ -241,6 +271,8 @@ describe('when Trusted Types are available in global object', () => {
241271 ) ;
242272
243273 // 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+ } ) ;
245277 } ) ;
246278} ) ;
0 commit comments