8
8
9
9
/*eslint-disable react/prop-types*/
10
10
11
- import React from 'react'
11
+ import React , { FunctionComponent } from 'react'
12
12
import { renderToString } from 'react-dom/server'
13
13
import { createStore } from 'redux'
14
14
import { Provider , connect } from '../../src/index'
15
+ import type { Dispatch , Store } from 'redux'
15
16
16
17
describe ( 'React' , ( ) => {
17
18
describe ( 'server rendering' , ( ) => {
18
- function greetingReducer ( state = { greeting : 'Hello' } , action ) {
19
+ interface ActionType {
20
+ type : string
21
+ payload : {
22
+ greeting : string
23
+ }
24
+ }
25
+ function greetingReducer (
26
+ state = { greeting : 'Hello' } ,
27
+ action : ActionType
28
+ ) {
19
29
return action && action . payload ? action . payload : state
20
30
}
31
+ interface GreetingProps {
32
+ greeting : string
33
+ greeted : string
34
+ }
35
+ const Greeting : FunctionComponent < GreetingProps > = ( {
36
+ greeting,
37
+ greeted,
38
+ } ) => {
39
+ return < span > { greeting + ' ' + greeted } </ span >
40
+ }
21
41
22
- const Greeting = ( { greeting, greeted } ) => greeting + ' ' + greeted
23
- const ConnectedGreeting = connect ( ( state ) => state ) ( Greeting )
42
+ interface RootType {
43
+ greeting : string
44
+ }
45
+ interface Props {
46
+ greeted : string
47
+ }
24
48
25
- const Greeter = ( props ) => (
49
+ const ConnectedGreeting = connect < RootType , unknown , Props , RootType > (
50
+ ( state ) => state
51
+ ) ( Greeting )
52
+
53
+ const Greeter = ( props : any ) => (
26
54
< div >
27
55
< ConnectedGreeting { ...props } />
28
56
</ div >
29
57
)
30
58
31
- class Dispatcher extends React . Component {
32
- constructor ( props ) {
59
+ interface DispatcherProps {
60
+ constructAction ?: ActionType
61
+ willMountAction ?: ActionType
62
+ renderAction ?: ActionType
63
+ dispatch : Dispatch
64
+ greeted : string
65
+ }
66
+
67
+ class Dispatcher extends React . Component < DispatcherProps > {
68
+ constructor ( props : DispatcherProps ) {
33
69
super ( props )
34
70
if ( props . constructAction ) {
35
71
props . dispatch ( props . constructAction )
@@ -51,23 +87,22 @@ describe('React', () => {
51
87
const ConnectedDispatcher = connect ( ) ( Dispatcher )
52
88
53
89
it ( 'should be able to render connected component with props and state from store' , ( ) => {
54
- const store = createStore ( greetingReducer )
90
+ const store : Store = createStore ( greetingReducer )
55
91
56
92
const markup = renderToString (
57
93
< Provider store = { store } >
58
94
< Greeter greeted = "world" />
59
95
</ Provider >
60
96
)
61
-
62
97
expect ( markup ) . toContain ( 'Hello world' )
63
98
} )
64
99
65
100
it ( 'should run in an SSR environment without logging warnings about useLayoutEffect' , ( ) => {
66
- const store = createStore ( greetingReducer )
101
+ const store : Store = createStore ( greetingReducer )
67
102
68
103
const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
69
104
70
- const markup = renderToString (
105
+ renderToString (
71
106
< Provider store = { store } >
72
107
< Greeter greeted = "world" />
73
108
</ Provider >
@@ -79,7 +114,7 @@ describe('React', () => {
79
114
} )
80
115
81
116
it ( 'should render with updated state if actions are dispatched before render' , ( ) => {
82
- const store = createStore ( greetingReducer )
117
+ const store : Store = createStore ( greetingReducer )
83
118
84
119
store . dispatch ( { type : 'Update' , payload : { greeting : 'Hi' } } )
85
120
@@ -106,7 +141,7 @@ describe('React', () => {
106
141
In all other versions, including v7, the store state may change as actions are dispatched
107
142
during lifecycle methods, and components will see that new state immediately as they read it.
108
143
*/
109
- const store = createStore ( greetingReducer )
144
+ const store : Store = createStore ( greetingReducer )
110
145
111
146
const constructAction = { type : 'Update' , payload : { greeting : 'Hi' } }
112
147
const willMountAction = { type : 'Update' , payload : { greeting : 'Hiya' } }
@@ -143,7 +178,7 @@ describe('React', () => {
143
178
This test works both when state is fetched directly in connected
144
179
components and when it is fetched in a Provider and placed on context
145
180
*/
146
- const store = createStore ( greetingReducer )
181
+ const store : Store = createStore ( greetingReducer )
147
182
148
183
const constructAction = { type : 'Update' , payload : { greeting : 'Hi' } }
149
184
const willMountAction = { type : 'Update' , payload : { greeting : 'Hiya' } }
0 commit comments