Skip to content

Commit b3f526a

Browse files
committed
Add runtime tests for listenerMiddleware.withTypes
1 parent 785484a commit b3f526a

File tree

2 files changed

+115
-3
lines changed

2 files changed

+115
-3
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import type { Action } from 'redux'
2+
import type { ThunkAction } from 'redux-thunk'
3+
import { describe, expect, test } from 'vitest'
4+
import { configureStore } from '../../configureStore'
5+
import { createAsyncThunk } from '../../createAsyncThunk'
6+
import { createSlice } from '../../createSlice'
7+
import { addListener, createListenerMiddleware, removeListener } from '../index'
8+
9+
export interface CounterState {
10+
counter: number
11+
}
12+
13+
const initialState: CounterState = {
14+
counter: 0,
15+
}
16+
17+
export const counterSlice = createSlice({
18+
name: 'counter',
19+
initialState,
20+
reducers: {
21+
increment(state) {
22+
state.counter++
23+
},
24+
},
25+
})
26+
27+
export function fetchCount(amount = 1) {
28+
return new Promise<{ data: number }>((resolve) =>
29+
setTimeout(() => resolve({ data: amount }), 500)
30+
)
31+
}
32+
33+
export const incrementAsync = createAsyncThunk(
34+
'counter/fetchCount',
35+
async (amount: number) => {
36+
const response = await fetchCount(amount)
37+
// The value we return becomes the `fulfilled` action payload
38+
return response.data
39+
}
40+
)
41+
42+
const { increment } = counterSlice.actions
43+
44+
const store = configureStore({
45+
reducer: counterSlice.reducer,
46+
})
47+
48+
type AppStore = typeof store
49+
type AppDispatch = typeof store.dispatch
50+
type RootState = ReturnType<typeof store.getState>
51+
type AppThunk<ThunkReturnType = void> = ThunkAction<
52+
ThunkReturnType,
53+
RootState,
54+
unknown,
55+
Action
56+
>
57+
58+
const listenerMiddleware = createListenerMiddleware()
59+
60+
const startAppListening = listenerMiddleware.startListening.withTypes<
61+
RootState,
62+
AppDispatch
63+
>()
64+
65+
const stopAppListening = listenerMiddleware.stopListening.withTypes<
66+
RootState,
67+
AppDispatch
68+
>()
69+
70+
const addAppListener = addListener.withTypes<RootState, AppDispatch>()
71+
72+
const removeAppListener = removeListener.withTypes<RootState, AppDispatch>()
73+
74+
describe(startAppListening.withTypes, () => {
75+
test('should return startListening', () => {
76+
expect(startAppListening.withTypes).to.be.a('function')
77+
78+
expect(startAppListening.withTypes().withTypes).to.be.a('function')
79+
80+
expect(startAppListening).toBe(listenerMiddleware.startListening)
81+
})
82+
})
83+
84+
describe(stopAppListening.withTypes, () => {
85+
test('should return stopListening', () => {
86+
expect(stopAppListening.withTypes).to.be.a('function')
87+
88+
expect(stopAppListening.withTypes().withTypes).to.be.a('function')
89+
90+
expect(stopAppListening).toBe(listenerMiddleware.stopListening)
91+
})
92+
})
93+
94+
describe(addAppListener.withTypes, () => {
95+
test('should return addListener', () => {
96+
expect(addAppListener.withTypes).to.be.a('function')
97+
98+
expect(addAppListener.withTypes().withTypes).to.be.a('function')
99+
100+
expect(addAppListener).toBe(addListener)
101+
})
102+
})
103+
104+
describe(removeAppListener.withTypes, () => {
105+
test('should return removeListener', () => {
106+
expect(removeAppListener.withTypes).to.be.a('function')
107+
108+
expect(removeAppListener.withTypes().withTypes).to.be.a('function')
109+
110+
expect(removeAppListener).toBe(removeListener)
111+
})
112+
})

packages/toolkit/src/tests/tsconfig.typetests.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"extends": "../../tsconfig.test.json",
33
"compilerOptions": {
44
"skipLibCheck": true,
5-
"rootDir": "../../src"
6-
},
7-
"include": ["../../src/**/*.ts*"]
5+
// "rootDir": "../../src"
6+
}
7+
// "include": ["../../src/**/*.ts*"]
88
}

0 commit comments

Comments
 (0)