2
2
3
3
import React , { useCallback , useReducer , useLayoutEffect } from 'react'
4
4
import { createStore } from 'redux'
5
- import { renderHook , act } from '@testing-library/react-hooks'
6
5
import * as rtl from '@testing-library/react'
7
6
import {
8
7
Provider as ProviderMock ,
@@ -15,7 +14,6 @@ import { useReduxContext } from '../../src/hooks/useReduxContext'
15
14
import type { FunctionComponent , DispatchWithoutAction , ReactNode } from 'react'
16
15
import type { Store , AnyAction } from 'redux'
17
16
import type {
18
- ProviderProps ,
19
17
TypedUseSelectorHook ,
20
18
ReactReduxContextValue ,
21
19
Subscription ,
@@ -44,41 +42,55 @@ describe('React', () => {
44
42
afterEach ( ( ) => rtl . cleanup ( ) )
45
43
46
44
describe ( 'core subscription behavior' , ( ) => {
47
- type PropsTypeDelStore = Omit < ProviderProps , 'store' >
48
-
49
45
it ( 'selects the state on initial render' , ( ) => {
50
- const { result } = renderHook (
51
- ( ) => useNormalSelector ( ( s ) => s . count ) ,
52
- {
53
- wrapper : ( props : PropsTypeDelStore ) => (
54
- < ProviderMock { ...props } store = { normalStore } />
55
- ) ,
56
- }
46
+ let result : number | undefined
47
+ const Comp = ( ) => {
48
+ const count = useNormalSelector ( ( state ) => state . count )
49
+
50
+ useLayoutEffect ( ( ) => {
51
+ result = count
52
+ } , [ ] )
53
+ return < div > { count } </ div >
54
+ }
55
+
56
+ rtl . render (
57
+ < ProviderMock store = { normalStore } >
58
+ < Comp />
59
+ </ ProviderMock >
57
60
)
58
61
59
- expect ( result . current ) . toEqual ( 0 )
62
+ expect ( result ) . toEqual ( 0 )
60
63
} )
61
64
62
65
it ( 'selects the state and renders the component when the store updates' , ( ) => {
63
66
type MockParams = [ NormalStateType ]
64
67
const selector : jest . Mock < number , MockParams > = jest . fn (
65
68
( s ) => s . count
66
69
)
70
+ let result : number | undefined
71
+ const Comp = ( ) => {
72
+ const count = useNormalSelector ( selector )
67
73
68
- const { result } = renderHook ( ( ) => useNormalSelector ( selector ) , {
69
- wrapper : ( props : PropsTypeDelStore ) => (
70
- < ProviderMock { ... props } store = { normalStore } />
71
- ) ,
72
- } )
74
+ useLayoutEffect ( ( ) => {
75
+ result = count
76
+ } )
77
+ return < div > { count } </ div >
78
+ }
73
79
74
- expect ( result . current ) . toEqual ( 0 )
80
+ rtl . render (
81
+ < ProviderMock store = { normalStore } >
82
+ < Comp />
83
+ </ ProviderMock >
84
+ )
85
+
86
+ expect ( result ) . toEqual ( 0 )
75
87
expect ( selector ) . toHaveBeenCalledTimes ( 1 )
76
88
77
- act ( ( ) => {
89
+ rtl . act ( ( ) => {
78
90
normalStore . dispatch ( { type : '' } )
79
91
} )
80
92
81
- expect ( result . current ) . toEqual ( 1 )
93
+ expect ( result ) . toEqual ( 1 )
82
94
expect ( selector ) . toHaveBeenCalledTimes ( 2 )
83
95
} )
84
96
} )
@@ -102,7 +114,7 @@ describe('React', () => {
102
114
103
115
expect ( renderedItems ) . toEqual ( [ 1 ] )
104
116
105
- act ( ( ) => {
117
+ rtl . act ( ( ) => {
106
118
store . dispatch ( { type : '' } )
107
119
} )
108
120
@@ -132,7 +144,7 @@ describe('React', () => {
132
144
// @ts -ignore ts(2454)
133
145
expect ( rootSubscription . getListeners ( ) . get ( ) . length ) . toBe ( 1 )
134
146
135
- act ( ( ) => {
147
+ rtl . act ( ( ) => {
136
148
normalStore . dispatch ( { type : '' } )
137
149
} )
138
150
@@ -163,7 +175,7 @@ describe('React', () => {
163
175
// @ts -ignore ts(2454)
164
176
expect ( rootSubscription . getListeners ( ) . get ( ) . length ) . toBe ( 2 )
165
177
166
- act ( ( ) => {
178
+ rtl . act ( ( ) => {
167
179
normalStore . dispatch ( { type : '' } )
168
180
} )
169
181
@@ -210,8 +222,7 @@ describe('React', () => {
210
222
// With `useSyncExternalStore`, we get three renders of `<Comp>`:
211
223
// 1) Initial render, count is 0
212
224
// 2) Render due to dispatch, still sync in the initial render's commit phase
213
- // TODO 3) ??
214
- expect ( renderedItems ) . toEqual ( [ 0 , 1 , 1 ] )
225
+ expect ( renderedItems ) . toEqual ( [ 0 , 1 ] )
215
226
} )
216
227
} )
217
228
@@ -274,7 +285,7 @@ describe('React', () => {
274
285
275
286
expect ( renderedItems . length ) . toBe ( 1 )
276
287
277
- act ( ( ) => {
288
+ rtl . act ( ( ) => {
278
289
store . dispatch ( { type : '' } )
279
290
} )
280
291
@@ -309,7 +320,7 @@ describe('React', () => {
309
320
310
321
expect ( renderedItems . length ) . toBe ( 1 )
311
322
312
- act ( ( ) => {
323
+ rtl . act ( ( ) => {
313
324
store . dispatch ( { type : '' } )
314
325
} )
315
326
@@ -346,7 +357,7 @@ describe('React', () => {
346
357
expect ( numCalls ) . toBe ( 1 )
347
358
expect ( renderedItems . length ) . toEqual ( 1 )
348
359
349
- act ( ( ) => {
360
+ rtl . act ( ( ) => {
350
361
store . dispatch ( { type : '' } )
351
362
} )
352
363
@@ -398,9 +409,7 @@ describe('React', () => {
398
409
399
410
// Selector first called on Comp mount, and then re-invoked after mount due to useLayoutEffect dispatching event
400
411
expect ( numCalls ) . toBe ( 2 )
401
- // TODO As with "notice store updates" above, we're now getting _3_ renders here
402
- // expect(renderedItems.length).toEqual(2)
403
- expect ( renderedItems . length ) . toEqual ( 3 )
412
+ expect ( renderedItems . length ) . toEqual ( 2 )
404
413
} )
405
414
} )
406
415
@@ -504,7 +513,7 @@ describe('React', () => {
504
513
// TODO We can no longer catch errors in selectors after dispatch ourselves, as `uSES` swallows them.
505
514
// The test selector will happen to re-throw while rendering and we do see that.
506
515
expect ( ( ) => {
507
- act ( ( ) => {
516
+ rtl . act ( ( ) => {
508
517
store . dispatch ( { type : '' } )
509
518
} )
510
519
} ) . toThrow ( / P a n i c ! / )
@@ -539,7 +548,7 @@ describe('React', () => {
539
548
)
540
549
541
550
expect ( ( ) => {
542
- act ( ( ) => {
551
+ rtl . act ( ( ) => {
543
552
normalStore . dispatch ( { type : '' } )
544
553
} )
545
554
} ) . toThrowError ( )
@@ -614,7 +623,7 @@ describe('React', () => {
614
623
615
624
expect ( renderedItems . length ) . toBe ( 1 )
616
625
617
- act ( ( ) => {
626
+ rtl . act ( ( ) => {
618
627
normalStore . dispatch ( { type : '' } )
619
628
} )
620
629
0 commit comments