1212let React ;
1313let ReactDOM ;
1414let ReactDOMClient ;
15- let ReactTestUtils ;
1615let act ;
1716let Scheduler ;
1817let waitForAll ;
@@ -25,7 +24,6 @@ describe('ReactUpdates', () => {
2524 React = require ( 'react' ) ;
2625 ReactDOM = require ( 'react-dom' ) ;
2726 ReactDOMClient = require ( 'react-dom/client' ) ;
28- ReactTestUtils = require ( 'react-dom/test-utils' ) ;
2927 act = require ( 'internal-test-utils' ) . act ;
3028 Scheduler = require ( 'scheduler' ) ;
3129
@@ -832,7 +830,7 @@ describe('ReactUpdates', () => {
832830 expect ( updates ) . toEqual ( [ 0 , 1 , 2 , 0 , 1 , 2 ] ) ;
833831 } ) ;
834832
835- it ( 'should queue nested updates' , ( ) => {
833+ it ( 'should queue nested updates' , async ( ) => {
836834 // See https://github.com/facebook/react/issues/1147
837835
838836 class X extends React . Component {
@@ -877,11 +875,25 @@ describe('ReactUpdates', () => {
877875 }
878876 }
879877
880- const x = ReactTestUtils . renderIntoDocument ( < X /> ) ;
881- const y = ReactTestUtils . renderIntoDocument ( < Y /> ) ;
878+ let container = document . createElement ( 'div' ) ;
879+ let root = ReactDOMClient . createRoot ( container ) ;
880+ let x ;
881+ await act ( ( ) => {
882+ root . render ( < X ref = { current => ( x = current ) } /> ) ;
883+ } ) ;
884+
885+ container = document . createElement ( 'div' ) ;
886+ root = ReactDOMClient . createRoot ( container ) ;
887+ let y ;
888+ await act ( ( ) => {
889+ root . render ( < Y ref = { current => ( y = current ) } /> ) ;
890+ } ) ;
891+
882892 expect ( ReactDOM . findDOMNode ( x ) . textContent ) . toBe ( '0' ) ;
883893
884- y . forceUpdate ( ) ;
894+ await act ( ( ) => {
895+ y . forceUpdate ( ) ;
896+ } ) ;
885897 expect ( ReactDOM . findDOMNode ( x ) . textContent ) . toBe ( '1' ) ;
886898 } ) ;
887899
@@ -1004,7 +1016,7 @@ describe('ReactUpdates', () => {
10041016 assertLog ( [ ] ) ;
10051017 } ) ;
10061018
1007- it ( 'throws in setState if the update callback is not a function' , ( ) => {
1019+ it ( 'throws in setState if the update callback is not a function' , async ( ) => {
10081020 function Foo ( ) {
10091021 this . a = 1 ;
10101022 this . b = 2 ;
@@ -1018,36 +1030,62 @@ describe('ReactUpdates', () => {
10181030 }
10191031 }
10201032
1021- let component = ReactTestUtils . renderIntoDocument ( < A /> ) ;
1033+ let container = document . createElement ( 'div' ) ;
1034+ let root = ReactDOMClient . createRoot ( container ) ;
1035+ let component ;
1036+ await act ( ( ) => {
1037+ root . render ( < A ref = { current => ( component = current ) } /> ) ;
1038+ } ) ;
10221039
1023- expect ( ( ) => {
1024- expect ( ( ) => component . setState ( { } , 'no' ) ) . toErrorDev (
1040+ await expect (
1041+ expect ( async ( ) => {
1042+ await act ( ( ) => {
1043+ component . setState ( { } , 'no' ) ;
1044+ } ) ;
1045+ } ) . toErrorDev (
10251046 'setState(...): Expected the last optional `callback` argument to be ' +
10261047 'a function. Instead received: no.' ,
1027- ) ;
1028- } ) . toThrowError (
1048+ ) ,
1049+ ) . rejects . toThrowError (
10291050 'Invalid argument passed as callback. Expected a function. Instead ' +
10301051 'received: no' ,
10311052 ) ;
1032- component = ReactTestUtils . renderIntoDocument ( < A /> ) ;
1033- expect ( ( ) => {
1034- expect ( ( ) => component . setState ( { } , { foo : 'bar' } ) ) . toErrorDev (
1053+ container = document . createElement ( 'div' ) ;
1054+ root = ReactDOMClient . createRoot ( container ) ;
1055+ await act ( ( ) => {
1056+ root . render ( < A ref = { current => ( component = current ) } /> ) ;
1057+ } ) ;
1058+
1059+ await expect (
1060+ expect ( async ( ) => {
1061+ await act ( ( ) => {
1062+ component . setState ( { } , { foo : 'bar' } ) ;
1063+ } ) ;
1064+ } ) . toErrorDev (
10351065 'setState(...): Expected the last optional `callback` argument to be ' +
10361066 'a function. Instead received: [object Object].' ,
1037- ) ;
1038- } ) . toThrowError (
1067+ ) ,
1068+ ) . rejects . toThrowError (
10391069 'Invalid argument passed as callback. Expected a function. Instead ' +
10401070 'received: [object Object]' ,
10411071 ) ;
1042- // Make sure the warning is deduplicated and doesn't fire again
1043- component = ReactTestUtils . renderIntoDocument ( < A /> ) ;
1044- expect ( ( ) => component . setState ( { } , new Foo ( ) ) ) . toThrowError (
1072+ container = document . createElement ( 'div' ) ;
1073+ root = ReactDOMClient . createRoot ( container ) ;
1074+ await act ( ( ) => {
1075+ root . render ( < A ref = { current => ( component = current ) } /> ) ;
1076+ } ) ;
1077+
1078+ await expect (
1079+ act ( ( ) => {
1080+ component . setState ( { } , new Foo ( ) ) ;
1081+ } ) ,
1082+ ) . rejects . toThrowError (
10451083 'Invalid argument passed as callback. Expected a function. Instead ' +
10461084 'received: [object Object]' ,
10471085 ) ;
10481086 } ) ;
10491087
1050- it ( 'throws in forceUpdate if the update callback is not a function' , ( ) => {
1088+ it ( 'throws in forceUpdate if the update callback is not a function' , async ( ) => {
10511089 function Foo ( ) {
10521090 this . a = 1 ;
10531091 this . b = 2 ;
@@ -1061,30 +1099,57 @@ describe('ReactUpdates', () => {
10611099 }
10621100 }
10631101
1064- let component = ReactTestUtils . renderIntoDocument ( < A /> ) ;
1102+ let container = document . createElement ( 'div' ) ;
1103+ let root = ReactDOMClient . createRoot ( container ) ;
1104+ let component ;
1105+ await act ( ( ) => {
1106+ root . render ( < A ref = { current => ( component = current ) } /> ) ;
1107+ } ) ;
10651108
1066- expect ( ( ) => {
1067- expect ( ( ) => component . forceUpdate ( 'no' ) ) . toErrorDev (
1109+ await expect (
1110+ expect ( async ( ) => {
1111+ await act ( ( ) => {
1112+ component . forceUpdate ( 'no' ) ;
1113+ } ) ;
1114+ } ) . toErrorDev (
10681115 'forceUpdate(...): Expected the last optional `callback` argument to be ' +
10691116 'a function. Instead received: no.' ,
1070- ) ;
1071- } ) . toThrowError (
1117+ ) ,
1118+ ) . rejects . toThrowError (
10721119 'Invalid argument passed as callback. Expected a function. Instead ' +
10731120 'received: no' ,
10741121 ) ;
1075- component = ReactTestUtils . renderIntoDocument ( < A /> ) ;
1076- expect ( ( ) => {
1077- expect ( ( ) => component . forceUpdate ( { foo : 'bar' } ) ) . toErrorDev (
1122+ container = document . createElement ( 'div' ) ;
1123+ root = ReactDOMClient . createRoot ( container ) ;
1124+ await act ( ( ) => {
1125+ root . render ( < A ref = { current => ( component = current ) } /> ) ;
1126+ } ) ;
1127+
1128+ await expect (
1129+ expect ( async ( ) => {
1130+ await act ( ( ) => {
1131+ component . forceUpdate ( { foo : 'bar' } ) ;
1132+ } ) ;
1133+ } ) . toErrorDev (
10781134 'forceUpdate(...): Expected the last optional `callback` argument to be ' +
10791135 'a function. Instead received: [object Object].' ,
1080- ) ;
1081- } ) . toThrowError (
1136+ ) ,
1137+ ) . rejects . toThrowError (
10821138 'Invalid argument passed as callback. Expected a function. Instead ' +
10831139 'received: [object Object]' ,
10841140 ) ;
10851141 // Make sure the warning is deduplicated and doesn't fire again
1086- component = ReactTestUtils . renderIntoDocument ( < A /> ) ;
1087- expect ( ( ) => component . forceUpdate ( new Foo ( ) ) ) . toThrowError (
1142+ container = document . createElement ( 'div' ) ;
1143+ root = ReactDOMClient . createRoot ( container ) ;
1144+ await act ( ( ) => {
1145+ root . render ( < A ref = { current => ( component = current ) } /> ) ;
1146+ } ) ;
1147+
1148+ await expect (
1149+ act ( ( ) => {
1150+ component . forceUpdate ( new Foo ( ) ) ;
1151+ } ) ,
1152+ ) . rejects . toThrowError (
10881153 'Invalid argument passed as callback. Expected a function. Instead ' +
10891154 'received: [object Object]' ,
10901155 ) ;
0 commit comments