Skip to content

Commit f1201de

Browse files
authored
Refactor/test hooks (#1767)
1 parent cb93374 commit f1201de

File tree

7 files changed

+241
-116
lines changed

7 files changed

+241
-116
lines changed

jest.config.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
const { default: tsJestPreset } = require('ts-jest')
2+
13
const defaults = {
4+
...tsJestPreset,
25
coverageDirectory: './coverage/',
36
collectCoverage: true,
4-
testURL: 'http://localhost'
7+
testURL: 'http://localhost',
58
}
69

7-
const testFolderPath = folderName => `<rootDir>/test/${folderName}/**/*.js`
10+
const testFolderPath = (folderName) =>
11+
`<rootDir>/test/${folderName}/**/*.{js,ts,tsx}`
812

913
const NORMAL_TEST_FOLDERS = ['components', 'hooks', 'integration', 'utils']
1014

1115
const standardConfig = {
1216
...defaults,
1317
displayName: 'ReactDOM',
14-
testMatch: NORMAL_TEST_FOLDERS.map(testFolderPath)
18+
testMatch: NORMAL_TEST_FOLDERS.map(testFolderPath),
1519
}
1620

1721
const rnConfig = {
@@ -20,10 +24,10 @@ const rnConfig = {
2024
testMatch: [testFolderPath('react-native')],
2125
preset: 'react-native',
2226
transform: {
23-
'^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js'
24-
}
27+
'^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js',
28+
},
2529
}
2630

2731
module.exports = {
28-
projects: [standardConfig, rnConfig]
32+
projects: [standardConfig, rnConfig],
2933
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"rimraf": "^3.0.2",
110110
"rollup": "^2.32.1",
111111
"rollup-plugin-terser": "^7.0.2",
112+
"ts-jest": "^27.0.3",
112113
"typescript": "^4.3.4"
113114
},
114115
"browserify": {

src/hooks/useReduxContext.ts

+2-1
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

+9-5
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}

0 commit comments

Comments
 (0)