3
3
import React , { Component , MouseEvent } from 'react'
4
4
import createClass from 'create-react-class'
5
5
import PropTypes from 'prop-types'
6
- import ReactDOM from 'react-dom'
7
6
import { createStore , applyMiddleware } from 'redux'
8
7
import { Provider as ProviderMock , connect } from '../../src/index'
9
8
import * as rtl from '@testing-library/react'
@@ -402,7 +401,9 @@ describe('React', () => {
402
401
expect ( tester . getByTestId ( 'x' ) ) . toHaveTextContent ( 'true' )
403
402
404
403
props = { }
405
- container . current ! . forceUpdate ( )
404
+ rtl . act ( ( ) => {
405
+ container . current ! . forceUpdate ( )
406
+ } )
406
407
407
408
expect ( tester . queryByTestId ( 'x' ) ) . toBe ( null )
408
409
} )
@@ -440,7 +441,9 @@ describe('React', () => {
440
441
expect ( tester . getByTestId ( 'x' ) ) . toHaveTextContent ( 'true' )
441
442
442
443
props = { }
443
- container . current ! . forceUpdate ( )
444
+ rtl . act ( ( ) => {
445
+ container . current ! . forceUpdate ( )
446
+ } )
444
447
445
448
expect ( tester . getAllByTitle ( 'prop' ) . length ) . toBe ( 1 )
446
449
expect ( tester . getByTestId ( 'dispatch' ) ) . toHaveTextContent (
@@ -888,8 +891,14 @@ describe('React', () => {
888
891
< OuterComponent ref = { outerComponent } />
889
892
</ ProviderMock >
890
893
)
891
- outerComponent . current ! . setFoo ( 'BAR' )
892
- outerComponent . current ! . setFoo ( 'DID' )
894
+ expect ( invocationCount ) . toEqual ( 1 )
895
+ rtl . act ( ( ) => {
896
+ outerComponent . current ! . setFoo ( 'BAR' )
897
+ } )
898
+ rtl . act ( ( ) => {
899
+ outerComponent . current ! . setFoo ( 'BAZ' )
900
+ outerComponent . current ! . setFoo ( 'DID' )
901
+ } )
893
902
894
903
expect ( invocationCount ) . toEqual ( 3 )
895
904
} )
@@ -937,8 +946,16 @@ describe('React', () => {
937
946
</ ProviderMock >
938
947
)
939
948
940
- outerComponent . current ! . setFoo ( 'BAR' )
941
- outerComponent . current ! . setFoo ( 'BAZ' )
949
+ expect ( invocationCount ) . toEqual ( 1 )
950
+ rtl . act ( ( ) => {
951
+ outerComponent . current ! . setFoo ( 'QUUX' )
952
+ } )
953
+
954
+ expect ( invocationCount ) . toEqual ( 2 )
955
+ rtl . act ( ( ) => {
956
+ outerComponent . current ! . setFoo ( 'BAR' )
957
+ outerComponent . current ! . setFoo ( 'BAZ' )
958
+ } )
942
959
943
960
expect ( invocationCount ) . toEqual ( 3 )
944
961
expect ( propsPassedIn ) . toEqual ( {
@@ -988,8 +1005,12 @@ describe('React', () => {
988
1005
</ ProviderMock >
989
1006
)
990
1007
991
- outerComponent . current ! . setFoo ( 'BAR' )
992
- outerComponent . current ! . setFoo ( 'DID' )
1008
+ rtl . act ( ( ) => {
1009
+ outerComponent . current ! . setFoo ( 'BAR' )
1010
+ } )
1011
+ rtl . act ( ( ) => {
1012
+ outerComponent . current ! . setFoo ( 'DID' )
1013
+ } )
993
1014
994
1015
expect ( invocationCount ) . toEqual ( 1 )
995
1016
} )
@@ -1034,9 +1055,17 @@ describe('React', () => {
1034
1055
< OuterComponent ref = { outerComponent } />
1035
1056
</ ProviderMock >
1036
1057
)
1058
+ expect ( invocationCount ) . toEqual ( 1 )
1059
+ rtl . act ( ( ) => {
1060
+ outerComponent . current ! . setFoo ( 'BAR' )
1061
+ } )
1037
1062
1038
- outerComponent . current ! . setFoo ( 'BAR' )
1039
- outerComponent . current ! . setFoo ( 'DID' )
1063
+ expect ( invocationCount ) . toEqual ( 2 )
1064
+
1065
+ rtl . act ( ( ) => {
1066
+ outerComponent . current ! . setFoo ( 'DID' )
1067
+ outerComponent . current ! . setFoo ( 'QUUX' )
1068
+ } )
1040
1069
1041
1070
expect ( invocationCount ) . toEqual ( 3 )
1042
1071
} )
@@ -1084,12 +1113,22 @@ describe('React', () => {
1084
1113
</ ProviderMock >
1085
1114
)
1086
1115
1087
- outerComponent . current ! . setFoo ( 'BAR' )
1088
- outerComponent . current ! . setFoo ( 'BAZ' )
1116
+ expect ( invocationCount ) . toEqual ( 1 )
1117
+ rtl . act ( ( ) => {
1118
+ outerComponent . current ! . setFoo ( 'BAR' )
1119
+ } )
1120
+
1121
+ expect ( invocationCount ) . toEqual ( 2 )
1122
+
1123
+ rtl . act ( ( ) => {
1124
+ outerComponent . current ! . setFoo ( 'DID' )
1125
+ outerComponent . current ! . setFoo ( 'QUUX' )
1126
+ } )
1089
1127
1090
1128
expect ( invocationCount ) . toEqual ( 3 )
1129
+
1091
1130
expect ( propsPassedIn ) . toEqual ( {
1092
- foo : 'BAZ ' ,
1131
+ foo : 'QUUX ' ,
1093
1132
} )
1094
1133
} )
1095
1134
} )
@@ -1160,20 +1199,18 @@ describe('React', () => {
1160
1199
string
1161
1200
> ( ( state ) => ( { state } ) ) ( Child )
1162
1201
1163
- const div = document . createElement ( 'div' )
1164
- ReactDOM . render (
1202
+ const { unmount } = rtl . render (
1165
1203
< ProviderMock store = { store } >
1166
1204
< ConnectedApp />
1167
- </ ProviderMock > ,
1168
- div
1205
+ </ ProviderMock >
1169
1206
)
1170
1207
1171
1208
try {
1172
1209
rtl . act ( ( ) => {
1173
1210
store . dispatch ( { type : 'APPEND' , body : 'A' } )
1174
1211
} )
1175
1212
} finally {
1176
- ReactDOM . unmountComponentAtNode ( div )
1213
+ unmount ( )
1177
1214
}
1178
1215
} )
1179
1216
@@ -1212,6 +1249,7 @@ describe('React', () => {
1212
1249
const A = ( ) => < h1 > A</ h1 >
1213
1250
function mapState ( state : { } ) {
1214
1251
const calls = ++ mapStateToPropsCalls
1252
+ console . trace ( 'Call: ' , calls )
1215
1253
return { calls, state }
1216
1254
}
1217
1255
const ConnectedA = connect ( mapState ) ( A )
@@ -1252,28 +1290,27 @@ describe('React', () => {
1252
1290
}
1253
1291
}
1254
1292
1255
- const div = document . createElement ( 'div' )
1256
- document . body . appendChild ( div )
1257
- ReactDOM . render (
1293
+ const { unmount } = rtl . render (
1258
1294
< ProviderMock store = { store } >
1259
1295
< RouterMock />
1260
- </ ProviderMock > ,
1261
- div
1296
+ </ ProviderMock >
1262
1297
)
1263
1298
1264
- const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
1265
- linkA . current ! . click ( )
1266
- linkB . current ! . click ( )
1267
- linkB . current ! . click ( )
1268
- document . body . removeChild ( div )
1299
+ // const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
1300
+ rtl . act ( ( ) => {
1301
+ linkA . current ! . click ( )
1302
+ linkB . current ! . click ( )
1303
+ linkB . current ! . click ( )
1304
+ unmount ( )
1305
+ } )
1269
1306
1270
1307
// Called 3 times:
1271
1308
// - Initial mount
1272
- // - After first link click, stil mounted
1309
+ // - After first link click, still mounted
1273
1310
// - After second link click, but the queued state update is discarded due to batching as it's unmounted
1274
1311
expect ( mapStateToPropsCalls ) . toBe ( 3 )
1275
- expect ( spy ) . toHaveBeenCalledTimes ( 0 )
1276
- spy . mockRestore ( )
1312
+ // expect(spy).toHaveBeenCalledTimes(0)
1313
+ // spy.mockRestore()
1277
1314
} )
1278
1315
1279
1316
it ( 'should not attempt to set state when dispatching in componentWillUnmount' , ( ) => {
@@ -1297,17 +1334,16 @@ describe('React', () => {
1297
1334
( state ) => ( { calls : mapStateToPropsCalls ++ } ) ,
1298
1335
( dispatch ) => ( { dispatch } )
1299
1336
) ( Container )
1300
- const div = document . createElement ( 'div' )
1301
- ReactDOM . render (
1337
+
1338
+ const { unmount } = rtl . render (
1302
1339
< ProviderMock store = { store } >
1303
1340
< Connected />
1304
- </ ProviderMock > ,
1305
- div
1341
+ </ ProviderMock >
1306
1342
)
1307
1343
expect ( mapStateToPropsCalls ) . toBe ( 1 )
1308
1344
1309
1345
const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
1310
- ReactDOM . unmountComponentAtNode ( div )
1346
+ unmount ( )
1311
1347
expect ( spy ) . toHaveBeenCalledTimes ( 0 )
1312
1348
expect ( mapStateToPropsCalls ) . toBe ( 1 )
1313
1349
spy . mockRestore ( )
@@ -1327,18 +1363,17 @@ describe('React', () => {
1327
1363
( dispatch ) => ( { dispatch } )
1328
1364
) ( Inner )
1329
1365
1330
- const div = document . createElement ( 'div' )
1366
+ let unmount : ReturnType < typeof rtl . render > [ 'unmount' ]
1331
1367
store . subscribe ( ( ) => {
1332
- ReactDOM . unmountComponentAtNode ( div )
1368
+ unmount ( )
1333
1369
} )
1334
1370
1335
1371
rtl . act ( ( ) => {
1336
- ReactDOM . render (
1372
+ unmount = rtl . render (
1337
1373
< ProviderMock store = { store } >
1338
1374
< ConnectedInner />
1339
- </ ProviderMock > ,
1340
- div
1341
- )
1375
+ </ ProviderMock >
1376
+ ) . unmount
1342
1377
} )
1343
1378
1344
1379
expect ( mapStateToPropsCalls ) . toBe ( 1 )
@@ -1405,15 +1440,13 @@ describe('React', () => {
1405
1440
store . dispatch ( { type : 'fetch' } )
1406
1441
} )
1407
1442
1408
- const div = document . createElement ( 'div' )
1409
- ReactDOM . render (
1443
+ const { unmount } = rtl . render (
1410
1444
< ProviderMock store = { store } >
1411
1445
< ConnectedParent />
1412
- </ ProviderMock > ,
1413
- div
1446
+ </ ProviderMock >
1414
1447
)
1415
1448
1416
- ReactDOM . unmountComponentAtNode ( div )
1449
+ unmount ( )
1417
1450
} )
1418
1451
} )
1419
1452
@@ -2114,9 +2147,13 @@ describe('React', () => {
2114
2147
)
2115
2148
2116
2149
expect ( mapStateToProps ) . toHaveBeenCalledTimes ( 0 )
2117
- store . dispatch ( { type : 'INC' } )
2150
+ rtl . act ( ( ) => {
2151
+ store . dispatch ( { type : 'INC' } )
2152
+ } )
2118
2153
expect ( mapStateToProps ) . toHaveBeenCalledTimes ( 1 )
2119
- store . dispatch ( { type : 'INC' } )
2154
+ rtl . act ( ( ) => {
2155
+ store . dispatch ( { type : 'INC' } )
2156
+ } )
2120
2157
expect ( mapStateToProps ) . toHaveBeenCalledTimes ( 1 )
2121
2158
} )
2122
2159
} )
@@ -2516,7 +2553,7 @@ describe('React', () => {
2516
2553
} )
2517
2554
2518
2555
describe ( 'Refs' , ( ) => {
2519
- it ( 'should return the instance of the wrapped component for use in calling child methods' , async ( done ) => {
2556
+ it ( 'should return the instance of the wrapped component for use in calling child methods' , async ( ) => {
2520
2557
const store = createStore ( ( ) => ( { } ) )
2521
2558
2522
2559
const someData = {
@@ -2555,7 +2592,6 @@ describe('React', () => {
2555
2592
await tester . findByTestId ( 'loaded' )
2556
2593
2557
2594
expect ( ref . current ! . someInstanceMethod ( ) ) . toBe ( someData )
2558
- done ( )
2559
2595
} )
2560
2596
2561
2597
it ( 'should correctly separate and pass through props to the wrapped component with a forwarded ref' , ( ) => {
@@ -2601,7 +2637,7 @@ describe('React', () => {
2601
2637
} )
2602
2638
2603
2639
describe ( 'Impure behavior' , ( ) => {
2604
- it ( 'should return the instance of the wrapped component for use in calling child methods, impure component' , async ( done ) => {
2640
+ it ( 'should return the instance of the wrapped component for use in calling child methods, impure component' , async ( ) => {
2605
2641
const store = createStore ( ( ) => ( { } ) )
2606
2642
2607
2643
const someData = {
@@ -2641,7 +2677,6 @@ describe('React', () => {
2641
2677
await tester . findByTestId ( 'loaded' )
2642
2678
2643
2679
expect ( ref . current ! . someInstanceMethod ( ) ) . toBe ( someData )
2644
- done ( )
2645
2680
} )
2646
2681
2647
2682
it ( 'should wrap impure components without supressing updates' , ( ) => {
@@ -2695,8 +2730,10 @@ describe('React', () => {
2695
2730
)
2696
2731
2697
2732
expect ( tester . getByTestId ( 'statefulValue' ) ) . toHaveTextContent ( '0' )
2698
- //@ts -ignore
2699
- externalSetState ( { value : 1 } )
2733
+ rtl . act ( ( ) => {
2734
+ //@ts -ignore
2735
+ externalSetState ( { value : 1 } )
2736
+ } )
2700
2737
expect ( tester . getByTestId ( 'statefulValue' ) ) . toHaveTextContent ( '1' )
2701
2738
} )
2702
2739
@@ -2744,7 +2781,7 @@ describe('React', () => {
2744
2781
)
2745
2782
const Decorated = decorator ( ImpureComponent )
2746
2783
2747
- let externalSetState
2784
+ let externalSetState : any
2748
2785
let storeGetter = { storeKey : 'foo' }
2749
2786
type StatefulWrapperStateType = {
2750
2787
storeGetter : typeof storeGetter
@@ -2785,8 +2822,10 @@ describe('React', () => {
2785
2822
2786
2823
// Impure update
2787
2824
storeGetter . storeKey = 'bar'
2788
- //@ts -ignore
2789
- externalSetState ( { storeGetter } )
2825
+ rtl . act ( ( ) => {
2826
+ //@ts -ignore
2827
+ externalSetState ( { storeGetter } )
2828
+ } )
2790
2829
2791
2830
// 4) After the the impure update
2792
2831
expect ( mapStateSpy ) . toHaveBeenCalledTimes ( 3 )
@@ -3317,8 +3356,14 @@ describe('React', () => {
3317
3356
< OuterComponent ref = { outerComponent } />
3318
3357
</ ProviderMock >
3319
3358
)
3320
- outerComponent . current ! . setState ( ( { count } ) => ( { count : count + 1 } ) )
3321
- store . dispatch ( { type : '' } )
3359
+ rtl . act ( ( ) => {
3360
+ outerComponent . current ! . setState ( ( { count } ) => ( {
3361
+ count : count + 1 ,
3362
+ } ) )
3363
+
3364
+ store . dispatch ( { type : '' } )
3365
+ } )
3366
+
3322
3367
//@ts -ignore
3323
3368
expect ( propsPassedIn . count ) . toEqual ( 1 )
3324
3369
//@ts -ignore
@@ -3429,7 +3474,9 @@ describe('React', () => {
3429
3474
expect ( rendered . getByTestId ( 'child' ) . dataset . prop ) . toEqual ( 'a' )
3430
3475
3431
3476
// Force the multi-update sequence by running this bound action creator
3432
- parent . current ! . inc1 ( )
3477
+ rtl . act ( ( ) => {
3478
+ parent . current ! . inc1 ( )
3479
+ } )
3433
3480
3434
3481
// The connected child component _should_ have rendered with the latest Redux
3435
3482
// store value (3) _and_ the latest wrapper prop ('b').
0 commit comments