Skip to content

Commit e747c63

Browse files
committed
Add hooks.withTypes.test.tsx to test runtime behavior
1 parent f3af0b9 commit e747c63

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

test/hooks/hooks.withTypes.test.tsx

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import type { Action, ThunkAction } from '@reduxjs/toolkit'
2+
import { configureStore, createAsyncThunk, createSlice } from '@reduxjs/toolkit'
3+
import { useDispatch, useSelector, useStore } from '../../src'
4+
5+
export interface CounterState {
6+
counter: number
7+
}
8+
9+
const initialState: CounterState = {
10+
counter: 0,
11+
}
12+
13+
export const counterSlice = createSlice({
14+
name: 'counter',
15+
initialState,
16+
reducers: {
17+
increment(state) {
18+
state.counter++
19+
},
20+
},
21+
})
22+
23+
export function fetchCount(amount = 1) {
24+
return new Promise<{ data: number }>((resolve) =>
25+
setTimeout(() => resolve({ data: amount }), 500)
26+
)
27+
}
28+
29+
export const incrementAsync = createAsyncThunk(
30+
'counter/fetchCount',
31+
async (amount: number) => {
32+
const response = await fetchCount(amount)
33+
// The value we return becomes the `fulfilled` action payload
34+
return response.data
35+
}
36+
)
37+
38+
const { increment } = counterSlice.actions
39+
40+
const counterStore = configureStore({
41+
reducer: counterSlice.reducer,
42+
})
43+
44+
type AppStore = typeof counterStore
45+
type AppDispatch = typeof counterStore.dispatch
46+
type RootState = ReturnType<typeof counterStore.getState>
47+
type AppThunk<ThunkReturnType = void> = ThunkAction<
48+
ThunkReturnType,
49+
RootState,
50+
unknown,
51+
Action
52+
>
53+
54+
describe('useSelector.withTypes<RootState>()', () => {
55+
test('should return useSelector', () => {
56+
const useAppSelector = useSelector.withTypes<RootState>()
57+
58+
expect(useAppSelector).toBe(useSelector)
59+
})
60+
})
61+
62+
describe('useDispatch.withTypes<AppDispatch>()', () => {
63+
test('should return useDispatch', () => {
64+
const useAppDispatch = useDispatch.withTypes<AppDispatch>()
65+
66+
expect(useAppDispatch).toBe(useDispatch)
67+
})
68+
})
69+
70+
describe('useStore.withTypes<AppStore>()', () => {
71+
test('should return useStore', () => {
72+
const useAppStore = useStore.withTypes<AppStore>()
73+
74+
expect(useAppStore).toBe(useStore)
75+
})
76+
})

test/hooks/useSelector.spec.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,12 +1055,6 @@ describe('React', () => {
10551055
})
10561056
})
10571057
})
1058-
1059-
test('useSelector.withTypes', () => {
1060-
const useAppSelector = useSelector.withTypes<RootState>()
1061-
1062-
expect(useAppSelector).toBe(useSelector)
1063-
})
10641058
})
10651059

10661060
describe('createSelectorHook', () => {

test/typetests/hooks.withTypes.test-d.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import type { AppDispatch, AppStore, RootState } from './counterApp'
55
import { incrementAsync } from './counterApp'
66

77
function preTypedHooksSetupWithTypes() {
8-
const useAppDispatch = useDispatch.withTypes<AppDispatch>()
9-
108
const useAppSelector = useSelector.withTypes<RootState>()
119

10+
const useAppDispatch = useDispatch.withTypes<AppDispatch>()
11+
1212
const useAppStore = useStore.withTypes<AppStore>()
1313

1414
function CounterComponent() {

0 commit comments

Comments
 (0)