Skip to content

Commit 09d8675

Browse files
committed
Refactor: transform test\/hooks js files to tsx
1 parent 27c9cc5 commit 09d8675

File tree

4 files changed

+105
-53
lines changed

4 files changed

+105
-53
lines changed

src/hooks/useReduxContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useContext } from 'react'
22
import { ReactReduxContext } from '../components/Context'
3+
import type { ReactReduxContextValue } from '../components/Context'
34

45
/**
56
* A hook to access the value of the `ReactReduxContext`. This is a low-level
@@ -17,7 +18,7 @@ import { ReactReduxContext } from '../components/Context'
1718
* return <div>{store.getState()}</div>
1819
* }
1920
*/
20-
export function useReduxContext() {
21+
export function useReduxContext(): ReactReduxContextValue | null {
2122
const contextValue = useContext(ReactReduxContext)
2223

2324
if (process.env.NODE_ENV !== 'production' && !contextValue) {

test/hooks/useDispatch.spec.js renamed to test/hooks/useDispatch.spec.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ import {
66
useDispatch,
77
createDispatchHook,
88
} from '../../src/index'
9+
import type { ProviderProps } from '../../src/'
910

10-
const store = createStore((c) => c + 1)
11-
const store2 = createStore((c) => c + 2)
11+
const store = createStore((c: number): number => c + 1)
12+
const store2 = createStore((c: number): number => c + 2)
1213

1314
describe('React', () => {
1415
describe('hooks', () => {
1516
describe('useDispatch', () => {
1617
it("returns the store's dispatch function", () => {
18+
type PropsType = Omit<ProviderProps, 'store'>
1719
const { result } = renderHook(() => useDispatch(), {
18-
wrapper: (props) => <ProviderMock {...props} store={store} />,
20+
wrapper: (props: PropsType) => (
21+
<ProviderMock {...props} store={store} />
22+
),
1923
})
2024

2125
expect(result.current).toBe(store.dispatch)
@@ -27,7 +31,7 @@ describe('React', () => {
2731
const useCustomDispatch = createDispatchHook(nestedContext)
2832
const { result } = renderHook(() => useDispatch(), {
2933
// eslint-disable-next-line react/prop-types
30-
wrapper: ({ children, ...props }) => (
34+
wrapper: ({ children, ...props }: ProviderProps) => (
3135
<ProviderMock {...props} store={store}>
3236
<ProviderMock context={nestedContext} store={store2}>
3337
{children}
@@ -40,7 +44,7 @@ describe('React', () => {
4044

4145
const { result: result2 } = renderHook(() => useCustomDispatch(), {
4246
// eslint-disable-next-line react/prop-types
43-
wrapper: ({ children, ...props }) => (
47+
wrapper: ({ children, ...props }: ProviderProps) => (
4448
<ProviderMock {...props} store={store}>
4549
<ProviderMock context={nestedContext} store={store2}>
4650
{children}

test/hooks/useSelector.spec.js renamed to test/hooks/useSelector.spec.tsx

Lines changed: 94 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,46 @@ import {
1111
connect,
1212
createSelectorHook,
1313
} from '../../src/index'
14+
import type { FunctionComponent } from 'react'
1415
import { useReduxContext } from '../../src/hooks/useReduxContext'
15-
16+
import type { Store } from 'redux'
17+
import type { ProviderProps } from '../../src/'
18+
import type { Subscription } from '../../src/utils/Subscription'
19+
20+
type PropsTypeDelStore = Omit<ProviderProps, 'store'>
21+
type DefaultRootState = {
22+
count: number
23+
}
1624
describe('React', () => {
1725
describe('hooks', () => {
1826
describe('useSelector', () => {
19-
let store
27+
let store: Store
2028
let renderedItems = []
2129

2230
beforeEach(() => {
23-
store = createStore(({ count } = { count: -1 }) => ({
24-
count: count + 1,
25-
}))
31+
interface ReducerState {
32+
count: number
33+
}
34+
store = createStore(
35+
({ count }: ReducerState = { count: -1 }): ReducerState => ({
36+
count: count + 1,
37+
})
38+
)
2639
renderedItems = []
2740
})
2841

2942
afterEach(() => rtl.cleanup())
3043

3144
describe('core subscription behavior', () => {
3245
it('selects the state on initial render', () => {
33-
const { result } = renderHook(() => useSelector((s) => s.count), {
34-
wrapper: (props) => <ProviderMock {...props} store={store} />,
35-
})
46+
const { result } = renderHook(
47+
() => useSelector<DefaultRootState>((s) => s.count),
48+
{
49+
wrapper: (props: PropsTypeDelStore) => (
50+
<ProviderMock {...props} store={store} />
51+
),
52+
}
53+
)
3654

3755
expect(result.current).toEqual(0)
3856
})
@@ -41,7 +59,9 @@ describe('React', () => {
4159
const selector = jest.fn((s) => s.count)
4260

4361
const { result } = renderHook(() => useSelector(selector), {
44-
wrapper: (props) => <ProviderMock {...props} store={store} />,
62+
wrapper: (props: PropsTypeDelStore) => (
63+
<ProviderMock {...props} store={store} />
64+
),
4565
})
4666

4767
expect(result.current).toEqual(0)
@@ -58,7 +78,7 @@ describe('React', () => {
5878

5979
describe('lifecycle interactions', () => {
6080
it('always uses the latest state', () => {
61-
store = createStore((c) => c + 1, -1)
81+
store = createStore((c: number): number => c + 1, -1)
6282

6383
const Comp = () => {
6484
const selector = useCallback((c) => c + 1, [])
@@ -81,17 +101,17 @@ describe('React', () => {
81101
})
82102

83103
it('subscribes to the store synchronously', () => {
84-
let rootSubscription
104+
let rootSubscription: Subscription
85105

86106
const Parent = () => {
87107
const { subscription } = useReduxContext()
88108
rootSubscription = subscription
89-
const count = useSelector((s) => s.count)
109+
const count = useSelector<DefaultRootState>((s) => s.count)
90110
return count === 1 ? <Child /> : null
91111
}
92112

93113
const Child = () => {
94-
const count = useSelector((s) => s.count)
114+
const count = useSelector<DefaultRootState>((s) => s.count)
95115
return <div>{count}</div>
96116
}
97117

@@ -109,17 +129,17 @@ describe('React', () => {
109129
})
110130

111131
it('unsubscribes when the component is unmounted', () => {
112-
let rootSubscription
132+
let rootSubscription: Subscription
113133

114134
const Parent = () => {
115135
const { subscription } = useReduxContext()
116136
rootSubscription = subscription
117-
const count = useSelector((s) => s.count)
137+
const count = useSelector<DefaultRootState>((s) => s.count)
118138
return count === 0 ? <Child /> : null
119139
}
120140

121141
const Child = () => {
122-
const count = useSelector((s) => s.count)
142+
const count = useSelector<DefaultRootState>((s) => s.count)
123143
return <div>{count}</div>
124144
}
125145

@@ -138,7 +158,7 @@ describe('React', () => {
138158

139159
it('notices store updates between render and store subscription effect', () => {
140160
const Comp = () => {
141-
const count = useSelector((s) => s.count)
161+
const count = useSelector<DefaultRootState>((s) => s.count)
142162
renderedItems.push(count)
143163

144164
// I don't know a better way to trigger a store update before the
@@ -161,7 +181,7 @@ describe('React', () => {
161181
})
162182

163183
it('works properly with memoized selector with dispatch in Child useLayoutEffect', () => {
164-
store = createStore((c) => c + 1, -1)
184+
store = createStore((c: number) => c + 1, -1)
165185

166186
const Comp = () => {
167187
const selector = useCallback((c) => c, [])
@@ -221,8 +241,14 @@ describe('React', () => {
221241
})
222242

223243
it('allows other equality functions to prevent unnecessary updates', () => {
244+
interface ReducerParamsType {
245+
count: number
246+
stable: any
247+
}
224248
store = createStore(
225-
({ count, stable } = { count: -1, stable: {} }) => ({
249+
(
250+
{ count, stable }: ReducerParamsType = { count: -1, stable: {} }
251+
) => ({
226252
count: count + 1,
227253
stable,
228254
})
@@ -286,12 +312,12 @@ describe('React', () => {
286312
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
287313

288314
const Parent = () => {
289-
const count = useSelector((s) => s.count)
315+
const count = useSelector<DefaultRootState>((s) => s.count)
290316
return <Child parentCount={count} />
291317
}
292318

293319
const Child = ({ parentCount }) => {
294-
const result = useSelector(({ count }) => {
320+
const result = useSelector<DefaultRootState>(({ count }) => {
295321
if (count !== parentCount) {
296322
throw new Error()
297323
}
@@ -328,7 +354,7 @@ describe('React', () => {
328354
return <div>{result}</div>
329355
}
330356

331-
const store = createStore((count = -1) => count + 1)
357+
const store = createStore((count: number = -1) => count + 1)
332358

333359
const App = () => (
334360
<ProviderMock store={store}>
@@ -349,12 +375,12 @@ describe('React', () => {
349375
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
350376

351377
const Parent = () => {
352-
const count = useSelector((s) => s.count)
378+
const count = useSelector<DefaultRootState>((s) => s.count)
353379
return <Child parentCount={count} />
354380
}
355381

356382
const Child = ({ parentCount }) => {
357-
const result = useSelector(({ count }) => {
383+
const result = useSelector<DefaultRootState>(({ count }) => {
358384
if (parentCount > 0) {
359385
throw new Error()
360386
}
@@ -380,20 +406,30 @@ describe('React', () => {
380406
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
381407

382408
const Parent = () => {
383-
const count = useSelector((s) => s.count)
409+
const count = useSelector<DefaultRootState, number>((s) => s.count)
384410
return <ConnectedWrapper parentCount={count} />
385411
}
386-
387-
const ConnectedWrapper = connect(({ count }) => ({ count }))(
388-
({ parentCount }) => {
389-
return <Child parentCount={parentCount} />
390-
}
391-
)
412+
interface ParentContentProps {
413+
parentCount: number
414+
}
415+
const ConnectedWrapper = connect<
416+
DefaultRootState,
417+
undefined,
418+
ParentContentProps,
419+
DefaultRootState
420+
>(({ count }) => ({
421+
count,
422+
}))<FunctionComponent<ParentContentProps>>(({ parentCount }) => {
423+
return <Child parentCount={parentCount} />
424+
})
392425

393426
let sawInconsistentState = false
394427

395-
const Child = ({ parentCount }) => {
396-
const result = useSelector(({ count }) => {
428+
interface ChildPropsType {
429+
parentCount: number
430+
}
431+
const Child = ({ parentCount }: ChildPropsType) => {
432+
const result = useSelector<DefaultRootState>(({ count }) => {
397433
if (count !== parentCount) {
398434
sawInconsistentState = true
399435
}
@@ -418,15 +454,17 @@ describe('React', () => {
418454
})
419455

420456
it('reuse latest selected state on selector re-run', () => {
421-
store = createStore(({ count } = { count: -1 }) => ({
422-
count: count + 1,
423-
}))
457+
store = createStore(
458+
({ count }: DefaultRootState = { count: -1 }) => ({
459+
count: count + 1,
460+
})
461+
)
424462

425463
const alwaysEqual = () => true
426464

427465
const Comp = () => {
428466
// triggers render on store change
429-
useSelector((s) => s.count)
467+
useSelector<DefaultRootState>((s) => s.count)
430468
const array = useSelector(() => [1, 2, 3], alwaysEqual)
431469
renderedItems.push(array)
432470
return <div />
@@ -449,15 +487,20 @@ describe('React', () => {
449487

450488
describe('error handling for invalid arguments', () => {
451489
it('throws if no selector is passed', () => {
490+
//@ts-expect-error
452491
expect(() => useSelector()).toThrow()
453492
})
454493

455494
it('throws if selector is not a function', () => {
495+
//@ts-expect-error
456496
expect(() => useSelector(1)).toThrow()
457497
})
458498

459499
it('throws if equality function is not a function', () => {
460-
expect(() => useSelector((s) => s.count, 1)).toThrow()
500+
expect(() =>
501+
//@ts-expect-error
502+
useSelector<DefaultRootState>((s) => s.count, 1)
503+
).toThrow()
461504
})
462505
})
463506
})
@@ -467,12 +510,16 @@ describe('React', () => {
467510
let customStore
468511

469512
beforeEach(() => {
470-
defaultStore = createStore(({ count } = { count: -1 }) => ({
471-
count: count + 1,
472-
}))
473-
customStore = createStore(({ count } = { count: 10 }) => ({
474-
count: count + 2,
475-
}))
513+
defaultStore = createStore(
514+
({ count }: DefaultRootState = { count: -1 }) => ({
515+
count: count + 1,
516+
})
517+
)
518+
customStore = createStore(
519+
({ count }: DefaultRootState = { count: 10 }) => ({
520+
count: count + 2,
521+
})
522+
)
476523
})
477524

478525
it('subscribes to the correct store', () => {
@@ -481,15 +528,15 @@ describe('React', () => {
481528
let defaultCount = null
482529
let customCount = null
483530

484-
const getCount = (s) => s.count
531+
const getCount = (s: DefaultRootState) => s.count
485532

486533
const DisplayDefaultCount = ({ children = null }) => {
487-
const count = useSelector(getCount)
534+
const count = useSelector<DefaultRootState>(getCount)
488535
defaultCount = count
489536
return <>{children}</>
490537
}
491538
const DisplayCustomCount = ({ children = null }) => {
492-
const count = useCustomSelector(getCount)
539+
const count = useCustomSelector<DefaultRootState>(getCount)
493540
customCount = count
494541
return <>{children}</>
495542
}

0 commit comments

Comments
 (0)