@@ -5,6 +5,9 @@ import ReactDOMServer from 'react-dom/server'
5
5
import { createStore , combineReducers } from 'redux'
6
6
import { connect , Provider , ReactReduxContext } from '../../src/index'
7
7
import * as rtl from '@testing-library/react'
8
+ import type { Store } from 'redux'
9
+ import type { ReactNode } from 'react'
10
+ import type { ReactReduxContextValue } from '../../src/index'
8
11
9
12
describe ( 'React' , ( ) => {
10
13
/*
@@ -26,16 +29,37 @@ describe('React', () => {
26
29
because it's the popular approach, this test targets nr 3.
27
30
*/
28
31
describe ( 'dynamic reducers' , ( ) => {
29
- const InjectReducersContext = React . createContext ( null )
32
+ const InjectReducersContext = React . createContext <
33
+ ( ( r : any ) => void ) | null
34
+ > ( null )
30
35
31
- function ExtraReducersProvider ( { children, reducers } ) {
36
+ type Reducer = ( s : any ) => any
37
+
38
+ interface ReducersType {
39
+ [ x : string ] : Reducer
40
+ }
41
+
42
+ interface ExtraReducersProviderPropsType {
43
+ children : ReactNode
44
+ reducers : ReducersType
45
+ }
46
+
47
+ function ExtraReducersProvider ( {
48
+ children,
49
+ reducers,
50
+ } : ExtraReducersProviderPropsType ) {
32
51
return (
33
52
< InjectReducersContext . Consumer >
34
53
{ ( injectReducers ) => (
35
54
< ReactReduxContext . Consumer >
36
55
{ ( reduxContext ) => {
37
- const latestState = reduxContext . store . getState ( )
38
- const contextState = reduxContext . storeState
56
+ interface ReduxContextType extends ReactReduxContextValue {
57
+ storeState ?: any
58
+ }
59
+ const latestState =
60
+ reduxContext && reduxContext . store . getState ( )
61
+ const contextState =
62
+ reduxContext && ( reduxContext as ReduxContextType ) . storeState
39
63
40
64
let shouldInject = false
41
65
let shouldPatch = false
@@ -56,16 +80,16 @@ describe('React', () => {
56
80
}
57
81
58
82
if ( shouldInject ) {
59
- injectReducers ( reducers )
83
+ injectReducers && injectReducers ( reducers )
60
84
}
61
85
62
- if ( shouldPatch ) {
86
+ if ( shouldPatch && reduxContext ) {
63
87
// A safer way to do this would be to patch the storeState
64
88
// manually with the state from the new reducers, since
65
89
// this would better avoid tearing in a future concurrent world
66
90
const patchedReduxContext = {
67
91
...reduxContext ,
68
- storeState : reduxContext . store . getState ( ) ,
92
+ storeState : reduxContext && reduxContext . store . getState ( ) ,
69
93
}
70
94
return (
71
95
< ReactReduxContext . Provider value = { patchedReduxContext } >
@@ -88,28 +112,46 @@ describe('React', () => {
88
112
const dynamicReducer = {
89
113
dynamic : ( state = { greeting : 'Hello dynamic world' } ) => state ,
90
114
}
115
+ interface StateType {
116
+ initial : GreeterTStateProps
117
+ dynamic : GreeterTStateProps
118
+ }
119
+ interface GreeterTStateProps {
120
+ greeting : string
121
+ }
91
122
92
- function Greeter ( { greeting } ) {
123
+ function Greeter ( { greeting } : GreeterTStateProps ) {
93
124
return < div > { greeting } </ div >
94
125
}
95
126
96
- const InitialGreeting = connect ( ( state ) => ( {
127
+ const InitialGreeting = connect <
128
+ GreeterTStateProps ,
129
+ unknown ,
130
+ unknown ,
131
+ StateType
132
+ > ( ( state ) => ( {
97
133
greeting : state . initial . greeting ,
98
134
} ) ) ( Greeter )
99
- const DynamicGreeting = connect ( ( state ) => ( {
135
+
136
+ const DynamicGreeting = connect <
137
+ GreeterTStateProps ,
138
+ unknown ,
139
+ unknown ,
140
+ StateType
141
+ > ( ( state ) => ( {
100
142
greeting : state . dynamic . greeting ,
101
143
} ) ) ( Greeter )
102
144
103
- function createInjectReducers ( store , initialReducer ) {
145
+ function createInjectReducers ( store : Store , initialReducer : ReducersType ) {
104
146
let reducers = initialReducer
105
- return function injectReducers ( newReducers ) {
147
+ return function injectReducers ( newReducers : ReducersType ) {
106
148
reducers = { ...reducers , ...newReducers }
107
149
store . replaceReducer ( combineReducers ( reducers ) )
108
150
}
109
151
}
110
152
111
- let store
112
- let injectReducers
153
+ let store : Store
154
+ let injectReducers : ( r : any ) => void
113
155
114
156
beforeEach ( ( ) => {
115
157
// These could be singletons on the client, but
@@ -142,6 +184,7 @@ describe('React', () => {
142
184
143
185
jest . spyOn ( console , 'error' )
144
186
// eslint-disable-next-line no-console
187
+ // @ts -ignore
145
188
console . error . mockImplementation ( ( ) => { } )
146
189
147
190
const markup = ReactDOMServer . renderToString (
@@ -159,6 +202,7 @@ describe('React', () => {
159
202
expect ( markup ) . toContain ( 'Hello dynamic world' )
160
203
161
204
// eslint-disable-next-line no-console
205
+ // @ts -ignore
162
206
console . error . mockRestore ( )
163
207
} )
164
208
} )
0 commit comments