Skip to content

Commit 64a6e3a

Browse files
authored
refactor: transform test/integration/dynamic-reducers.spec => ts (#1774)
1 parent 19ccf75 commit 64a6e3a

File tree

1 file changed

+58
-14
lines changed

1 file changed

+58
-14
lines changed

test/integration/dynamic-reducers.spec.js renamed to test/integration/dynamic-reducers.spec.tsx

+58-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import ReactDOMServer from 'react-dom/server'
55
import { createStore, combineReducers } from 'redux'
66
import { connect, Provider, ReactReduxContext } from '../../src/index'
77
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'
811

912
describe('React', () => {
1013
/*
@@ -26,16 +29,37 @@ describe('React', () => {
2629
because it's the popular approach, this test targets nr 3.
2730
*/
2831
describe('dynamic reducers', () => {
29-
const InjectReducersContext = React.createContext(null)
32+
const InjectReducersContext = React.createContext<
33+
((r: any) => void) | null
34+
>(null)
3035

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) {
3251
return (
3352
<InjectReducersContext.Consumer>
3453
{(injectReducers) => (
3554
<ReactReduxContext.Consumer>
3655
{(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
3963

4064
let shouldInject = false
4165
let shouldPatch = false
@@ -56,16 +80,16 @@ describe('React', () => {
5680
}
5781

5882
if (shouldInject) {
59-
injectReducers(reducers)
83+
injectReducers && injectReducers(reducers)
6084
}
6185

62-
if (shouldPatch) {
86+
if (shouldPatch && reduxContext) {
6387
// A safer way to do this would be to patch the storeState
6488
// manually with the state from the new reducers, since
6589
// this would better avoid tearing in a future concurrent world
6690
const patchedReduxContext = {
6791
...reduxContext,
68-
storeState: reduxContext.store.getState(),
92+
storeState: reduxContext && reduxContext.store.getState(),
6993
}
7094
return (
7195
<ReactReduxContext.Provider value={patchedReduxContext}>
@@ -88,28 +112,46 @@ describe('React', () => {
88112
const dynamicReducer = {
89113
dynamic: (state = { greeting: 'Hello dynamic world' }) => state,
90114
}
115+
interface StateType {
116+
initial: GreeterTStateProps
117+
dynamic: GreeterTStateProps
118+
}
119+
interface GreeterTStateProps {
120+
greeting: string
121+
}
91122

92-
function Greeter({ greeting }) {
123+
function Greeter({ greeting }: GreeterTStateProps) {
93124
return <div>{greeting}</div>
94125
}
95126

96-
const InitialGreeting = connect((state) => ({
127+
const InitialGreeting = connect<
128+
GreeterTStateProps,
129+
unknown,
130+
unknown,
131+
StateType
132+
>((state) => ({
97133
greeting: state.initial.greeting,
98134
}))(Greeter)
99-
const DynamicGreeting = connect((state) => ({
135+
136+
const DynamicGreeting = connect<
137+
GreeterTStateProps,
138+
unknown,
139+
unknown,
140+
StateType
141+
>((state) => ({
100142
greeting: state.dynamic.greeting,
101143
}))(Greeter)
102144

103-
function createInjectReducers(store, initialReducer) {
145+
function createInjectReducers(store: Store, initialReducer: ReducersType) {
104146
let reducers = initialReducer
105-
return function injectReducers(newReducers) {
147+
return function injectReducers(newReducers: ReducersType) {
106148
reducers = { ...reducers, ...newReducers }
107149
store.replaceReducer(combineReducers(reducers))
108150
}
109151
}
110152

111-
let store
112-
let injectReducers
153+
let store: Store
154+
let injectReducers: (r: any) => void
113155

114156
beforeEach(() => {
115157
// These could be singletons on the client, but
@@ -142,6 +184,7 @@ describe('React', () => {
142184

143185
jest.spyOn(console, 'error')
144186
// eslint-disable-next-line no-console
187+
// @ts-ignore
145188
console.error.mockImplementation(() => {})
146189

147190
const markup = ReactDOMServer.renderToString(
@@ -159,6 +202,7 @@ describe('React', () => {
159202
expect(markup).toContain('Hello dynamic world')
160203

161204
// eslint-disable-next-line no-console
205+
// @ts-ignore
162206
console.error.mockRestore()
163207
})
164208
})

0 commit comments

Comments
 (0)