-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
hooks.withTypes.test.tsx
to test runtime behavior
- Loading branch information
1 parent
f3af0b9
commit e747c63
Showing
3 changed files
with
78 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import type { Action, ThunkAction } from '@reduxjs/toolkit' | ||
import { configureStore, createAsyncThunk, createSlice } from '@reduxjs/toolkit' | ||
import { useDispatch, useSelector, useStore } from '../../src' | ||
|
||
export interface CounterState { | ||
counter: number | ||
} | ||
|
||
const initialState: CounterState = { | ||
counter: 0, | ||
} | ||
|
||
export const counterSlice = createSlice({ | ||
name: 'counter', | ||
initialState, | ||
reducers: { | ||
increment(state) { | ||
state.counter++ | ||
}, | ||
}, | ||
}) | ||
|
||
export function fetchCount(amount = 1) { | ||
return new Promise<{ data: number }>((resolve) => | ||
setTimeout(() => resolve({ data: amount }), 500) | ||
) | ||
} | ||
|
||
export const incrementAsync = createAsyncThunk( | ||
'counter/fetchCount', | ||
async (amount: number) => { | ||
const response = await fetchCount(amount) | ||
// The value we return becomes the `fulfilled` action payload | ||
return response.data | ||
} | ||
) | ||
|
||
const { increment } = counterSlice.actions | ||
|
||
const counterStore = configureStore({ | ||
reducer: counterSlice.reducer, | ||
}) | ||
|
||
type AppStore = typeof counterStore | ||
type AppDispatch = typeof counterStore.dispatch | ||
type RootState = ReturnType<typeof counterStore.getState> | ||
type AppThunk<ThunkReturnType = void> = ThunkAction< | ||
ThunkReturnType, | ||
RootState, | ||
unknown, | ||
Action | ||
> | ||
|
||
describe('useSelector.withTypes<RootState>()', () => { | ||
test('should return useSelector', () => { | ||
const useAppSelector = useSelector.withTypes<RootState>() | ||
|
||
expect(useAppSelector).toBe(useSelector) | ||
}) | ||
}) | ||
|
||
describe('useDispatch.withTypes<AppDispatch>()', () => { | ||
test('should return useDispatch', () => { | ||
const useAppDispatch = useDispatch.withTypes<AppDispatch>() | ||
|
||
expect(useAppDispatch).toBe(useDispatch) | ||
}) | ||
}) | ||
|
||
describe('useStore.withTypes<AppStore>()', () => { | ||
test('should return useStore', () => { | ||
const useAppStore = useStore.withTypes<AppStore>() | ||
|
||
expect(useAppStore).toBe(useStore) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters