Skip to content

Commit

Permalink
Add hooks.withTypes.test.tsx to test runtime behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Jan 5, 2024
1 parent f3af0b9 commit e747c63
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
76 changes: 76 additions & 0 deletions test/hooks/hooks.withTypes.test.tsx
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)
})
})
6 changes: 0 additions & 6 deletions test/hooks/useSelector.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1055,12 +1055,6 @@ describe('React', () => {
})
})
})

test('useSelector.withTypes', () => {
const useAppSelector = useSelector.withTypes<RootState>()

expect(useAppSelector).toBe(useSelector)
})
})

describe('createSelectorHook', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/typetests/hooks.withTypes.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type { AppDispatch, AppStore, RootState } from './counterApp'
import { incrementAsync } from './counterApp'

function preTypedHooksSetupWithTypes() {
const useAppDispatch = useDispatch.withTypes<AppDispatch>()

const useAppSelector = useSelector.withTypes<RootState>()

const useAppDispatch = useDispatch.withTypes<AppDispatch>()

const useAppStore = useStore.withTypes<AppStore>()

function CounterComponent() {
Expand Down

0 comments on commit e747c63

Please sign in to comment.