diff --git a/packages/vue-redux/tests/compositions.withTypes.test.tsx b/packages/vue-redux/tests/compositions.withTypes.test.tsx new file mode 100644 index 0000000..fdd5dce --- /dev/null +++ b/packages/vue-redux/tests/compositions.withTypes.test.tsx @@ -0,0 +1,77 @@ +import {describe, test, expect} from "vitest"; +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 +type AppThunk = ThunkAction< + ThunkReturnType, + RootState, + unknown, + Action +> + +describe('useSelector.withTypes()', () => { + test('should return useSelector', () => { + const useAppSelector = useSelector.withTypes() + + expect(useAppSelector).toBe(useSelector) + }) +}) + +describe('useDispatch.withTypes()', () => { + test('should return useDispatch', () => { + const useAppDispatch = useDispatch.withTypes() + + expect(useAppDispatch).toBe(useDispatch) + }) +}) + +describe('useStore.withTypes()', () => { + test('should return useStore', () => { + const useAppStore = useStore.withTypes() + + expect(useAppStore).toBe(useStore) + }) +})