12
12
let React ;
13
13
let ReactDOM ;
14
14
let ReactDOMClient ;
15
- let ReactTestUtils ;
16
15
let act ;
17
16
let Scheduler ;
18
17
let waitForAll ;
@@ -25,7 +24,6 @@ describe('ReactUpdates', () => {
25
24
React = require ( 'react' ) ;
26
25
ReactDOM = require ( 'react-dom' ) ;
27
26
ReactDOMClient = require ( 'react-dom/client' ) ;
28
- ReactTestUtils = require ( 'react-dom/test-utils' ) ;
29
27
act = require ( 'internal-test-utils' ) . act ;
30
28
Scheduler = require ( 'scheduler' ) ;
31
29
@@ -832,7 +830,7 @@ describe('ReactUpdates', () => {
832
830
expect ( updates ) . toEqual ( [ 0 , 1 , 2 , 0 , 1 , 2 ] ) ;
833
831
} ) ;
834
832
835
- it ( 'should queue nested updates' , ( ) => {
833
+ it ( 'should queue nested updates' , async ( ) => {
836
834
// See https://github.com/facebook/react/issues/1147
837
835
838
836
class X extends React . Component {
@@ -877,11 +875,25 @@ describe('ReactUpdates', () => {
877
875
}
878
876
}
879
877
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
+
882
892
expect ( ReactDOM . findDOMNode ( x ) . textContent ) . toBe ( '0' ) ;
883
893
884
- y . forceUpdate ( ) ;
894
+ await act ( ( ) => {
895
+ y . forceUpdate ( ) ;
896
+ } ) ;
885
897
expect ( ReactDOM . findDOMNode ( x ) . textContent ) . toBe ( '1' ) ;
886
898
} ) ;
887
899
@@ -1004,7 +1016,7 @@ describe('ReactUpdates', () => {
1004
1016
assertLog ( [ ] ) ;
1005
1017
} ) ;
1006
1018
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 ( ) => {
1008
1020
function Foo ( ) {
1009
1021
this . a = 1 ;
1010
1022
this . b = 2 ;
@@ -1018,36 +1030,62 @@ describe('ReactUpdates', () => {
1018
1030
}
1019
1031
}
1020
1032
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
+ } ) ;
1022
1039
1023
- expect ( ( ) => {
1024
- expect ( ( ) => component . setState ( { } , 'no' ) ) . toErrorDev (
1040
+ await expect (
1041
+ expect ( async ( ) => {
1042
+ await act ( ( ) => {
1043
+ component . setState ( { } , 'no' ) ;
1044
+ } ) ;
1045
+ } ) . toErrorDev (
1025
1046
'setState(...): Expected the last optional `callback` argument to be ' +
1026
1047
'a function. Instead received: no.' ,
1027
- ) ;
1028
- } ) . toThrowError (
1048
+ ) ,
1049
+ ) . rejects . toThrowError (
1029
1050
'Invalid argument passed as callback. Expected a function. Instead ' +
1030
1051
'received: no' ,
1031
1052
) ;
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 (
1035
1065
'setState(...): Expected the last optional `callback` argument to be ' +
1036
1066
'a function. Instead received: [object Object].' ,
1037
- ) ;
1038
- } ) . toThrowError (
1067
+ ) ,
1068
+ ) . rejects . toThrowError (
1039
1069
'Invalid argument passed as callback. Expected a function. Instead ' +
1040
1070
'received: [object Object]' ,
1041
1071
) ;
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 (
1045
1083
'Invalid argument passed as callback. Expected a function. Instead ' +
1046
1084
'received: [object Object]' ,
1047
1085
) ;
1048
1086
} ) ;
1049
1087
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 ( ) => {
1051
1089
function Foo ( ) {
1052
1090
this . a = 1 ;
1053
1091
this . b = 2 ;
@@ -1061,30 +1099,57 @@ describe('ReactUpdates', () => {
1061
1099
}
1062
1100
}
1063
1101
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
+ } ) ;
1065
1108
1066
- expect ( ( ) => {
1067
- expect ( ( ) => component . forceUpdate ( 'no' ) ) . toErrorDev (
1109
+ await expect (
1110
+ expect ( async ( ) => {
1111
+ await act ( ( ) => {
1112
+ component . forceUpdate ( 'no' ) ;
1113
+ } ) ;
1114
+ } ) . toErrorDev (
1068
1115
'forceUpdate(...): Expected the last optional `callback` argument to be ' +
1069
1116
'a function. Instead received: no.' ,
1070
- ) ;
1071
- } ) . toThrowError (
1117
+ ) ,
1118
+ ) . rejects . toThrowError (
1072
1119
'Invalid argument passed as callback. Expected a function. Instead ' +
1073
1120
'received: no' ,
1074
1121
) ;
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 (
1078
1134
'forceUpdate(...): Expected the last optional `callback` argument to be ' +
1079
1135
'a function. Instead received: [object Object].' ,
1080
- ) ;
1081
- } ) . toThrowError (
1136
+ ) ,
1137
+ ) . rejects . toThrowError (
1082
1138
'Invalid argument passed as callback. Expected a function. Instead ' +
1083
1139
'received: [object Object]' ,
1084
1140
) ;
1085
1141
// 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 (
1088
1153
'Invalid argument passed as callback. Expected a function. Instead ' +
1089
1154
'received: [object Object]' ,
1090
1155
) ;
0 commit comments