Skip to content

Commit bf6bd48

Browse files
committed
Cut excessive assertions in configureStore.test-d.ts
1 parent 2a70d74 commit bf6bd48

File tree

1 file changed

+60
-59
lines changed

1 file changed

+60
-59
lines changed

packages/toolkit/src/tests/configureStore.test-d.ts

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
import type { ConfigureStoreOptions, PayloadAction } from '@reduxjs/toolkit'
2-
import { Tuple, configureStore, createSlice } from '@reduxjs/toolkit'
31
import type {
42
Action,
3+
ConfigureStoreOptions,
54
Dispatch,
65
Middleware,
6+
PayloadAction,
77
Reducer,
88
Store,
99
StoreEnhancer,
10+
ThunkAction,
11+
ThunkDispatch,
12+
ThunkMiddleware,
1013
UnknownAction,
11-
} from 'redux'
12-
import { applyMiddleware, combineReducers } from 'redux'
13-
import type { ThunkAction, ThunkDispatch, ThunkMiddleware } from 'redux-thunk'
14+
} from '@reduxjs/toolkit'
15+
import {
16+
Tuple,
17+
applyMiddleware,
18+
combineReducers,
19+
configureStore,
20+
createSlice,
21+
} from '@reduxjs/toolkit'
1422
import { thunk } from 'redux-thunk'
1523

1624
const _anyMiddleware: any = () => () => () => {}
@@ -40,11 +48,12 @@ describe('type tests', () => {
4048

4149
test('configureStore() infers the store state type.', () => {
4250
const reducer: Reducer<number> = () => 0
51+
4352
const store = configureStore({ reducer })
44-
const numberStore: Store<number, UnknownAction> = store
4553

46-
// @ts-expect-error
47-
const stringStore: Store<string, UnknownAction> = store
54+
expectTypeOf(store).toMatchTypeOf<Store<number, UnknownAction>>()
55+
56+
expectTypeOf(store).not.toMatchTypeOf<Store<string, UnknownAction>>()
4857
})
4958

5059
test('configureStore() infers the store action type.', () => {
@@ -402,7 +411,7 @@ describe('type tests', () => {
402411
test('Dispatch typings', () => {
403412
type StateA = number
404413
const reducerA = () => 0
405-
function thunkA() {
414+
const thunkA = () => {
406415
return (() => {}) as any as ThunkAction<Promise<'A'>, StateA, any, any>
407416
}
408417

@@ -599,12 +608,6 @@ describe('type tests', () => {
599608
>
600609
>()
601610

602-
expectTypeOf(concatenated).not.toEqualTypeOf<
603-
ReadonlyArray<
604-
typeof otherMiddleware | ThunkMiddleware | Middleware<{}>
605-
>
606-
>()
607-
608611
return concatenated
609612
},
610613
})
@@ -720,57 +723,55 @@ describe('type tests', () => {
720723
expectTypeOf(store.dispatch).toBeFunction()
721724
})
722725

723-
{
724-
interface CounterState {
725-
value: number
726-
}
726+
interface CounterState {
727+
value: number
728+
}
727729

728-
const counterSlice = createSlice({
729-
name: 'counter',
730-
initialState: { value: 0 } as CounterState,
731-
reducers: {
732-
increment(state) {
733-
state.value += 1
734-
},
735-
decrement(state) {
736-
state.value -= 1
737-
},
738-
// Use the PayloadAction type to declare the contents of `action.payload`
739-
incrementByAmount: (state, action: PayloadAction<number>) => {
740-
state.value += action.payload
741-
},
730+
const counterSlice = createSlice({
731+
name: 'counter',
732+
initialState: { value: 0 } as CounterState,
733+
reducers: {
734+
increment(state) {
735+
state.value += 1
742736
},
743-
})
737+
decrement(state) {
738+
state.value -= 1
739+
},
740+
// Use the PayloadAction type to declare the contents of `action.payload`
741+
incrementByAmount: (state, action: PayloadAction<number>) => {
742+
state.value += action.payload
743+
},
744+
},
745+
})
744746

745-
type Unsubscribe = () => void
747+
type Unsubscribe = () => void
746748

747-
// A fake middleware that tells TS that an unsubscribe callback is being returned for a given action
748-
// This is the same signature that the "listener" middleware uses
749-
const dummyMiddleware: Middleware<
750-
{
751-
(action: Action<'actionListenerMiddleware/add'>): Unsubscribe
752-
},
753-
CounterState
754-
> = (storeApi) => (next) => (action) => {}
749+
// A fake middleware that tells TS that an unsubscribe callback is being returned for a given action
750+
// This is the same signature that the "listener" middleware uses
751+
const dummyMiddleware: Middleware<
752+
{
753+
(action: Action<'actionListenerMiddleware/add'>): Unsubscribe
754+
},
755+
CounterState
756+
> = (storeApi) => (next) => (action) => {}
755757

756-
const store = configureStore({
757-
reducer: counterSlice.reducer,
758-
middleware: (gDM) => gDM().prepend(dummyMiddleware),
759-
})
758+
const store = configureStore({
759+
reducer: counterSlice.reducer,
760+
middleware: (gDM) => gDM().prepend(dummyMiddleware),
761+
})
760762

761-
// Order matters here! We need the listener type to come first, otherwise
762-
// the thunk middleware type kicks in and TS thinks a plain action is being returned
763-
expectTypeOf(store.dispatch).toEqualTypeOf<
764-
((action: Action<'actionListenerMiddleware/add'>) => Unsubscribe) &
765-
ThunkDispatch<CounterState, undefined, UnknownAction> &
766-
Dispatch<UnknownAction>
767-
>()
763+
// Order matters here! We need the listener type to come first, otherwise
764+
// the thunk middleware type kicks in and TS thinks a plain action is being returned
765+
expectTypeOf(store.dispatch).toEqualTypeOf<
766+
((action: Action<'actionListenerMiddleware/add'>) => Unsubscribe) &
767+
ThunkDispatch<CounterState, undefined, UnknownAction> &
768+
Dispatch<UnknownAction>
769+
>()
768770

769-
const unsubscribe = store.dispatch({
770-
type: 'actionListenerMiddleware/add',
771-
} as const)
771+
const unsubscribe = store.dispatch({
772+
type: 'actionListenerMiddleware/add',
773+
} as const)
772774

773-
expectTypeOf(unsubscribe).toEqualTypeOf<Unsubscribe>()
774-
}
775+
expectTypeOf(unsubscribe).toEqualTypeOf<Unsubscribe>()
775776
})
776777
})

0 commit comments

Comments
 (0)