Skip to content

Commit 8bb90a1

Browse files
authored
Merge pull request #4127 from aryaemami59/migrate-typetests-to-vitest
Migrate type tests to Vitest
2 parents 54b9182 + 77511c3 commit 8bb90a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+6474
-5542
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ typesversions
3030
!.yarn/versions
3131
.pnp.*
3232
*.tgz
33+
34+
tsconfig.vitest-temp.json
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import type { Action, Middleware, UnknownAction } from 'redux'
2+
import type { ThunkDispatch } from 'redux-thunk'
3+
import { configureStore } from '../../configureStore'
4+
import { createDynamicMiddleware } from '../index'
5+
6+
const untypedInstance = createDynamicMiddleware()
7+
8+
interface AppDispatch extends ThunkDispatch<number, undefined, UnknownAction> {
9+
(n: 1): 1
10+
}
11+
12+
const typedInstance = createDynamicMiddleware<number, AppDispatch>()
13+
14+
declare const staticMiddleware: Middleware<(n: 1) => 1>
15+
16+
const store = configureStore({
17+
reducer: () => 0,
18+
middleware: (gDM) =>
19+
gDM().prepend(typedInstance.middleware).concat(staticMiddleware),
20+
})
21+
22+
declare const compatibleMiddleware: Middleware<{}, number, AppDispatch>
23+
declare const incompatibleMiddleware: Middleware<{}, string, AppDispatch>
24+
25+
declare const addedMiddleware: Middleware<(n: 2) => 2>
26+
27+
describe('type tests', () => {
28+
test('instance typed at creation ensures middleware compatibility with store', () => {
29+
const store = configureStore({
30+
reducer: () => '',
31+
// @ts-expect-error
32+
middleware: (gDM) => gDM().prepend(typedInstance.middleware),
33+
})
34+
})
35+
36+
test('instance typed at creation enforces correct middleware type', () => {
37+
typedInstance.addMiddleware(
38+
compatibleMiddleware,
39+
// @ts-expect-error
40+
incompatibleMiddleware
41+
)
42+
43+
const dispatch = store.dispatch(
44+
typedInstance.withMiddleware(
45+
compatibleMiddleware,
46+
// @ts-expect-error
47+
incompatibleMiddleware
48+
)
49+
)
50+
})
51+
52+
test('withTypes() enforces correct middleware type', () => {
53+
const addMiddleware = untypedInstance.addMiddleware.withTypes<{
54+
state: number
55+
dispatch: AppDispatch
56+
}>()
57+
58+
addMiddleware(
59+
compatibleMiddleware,
60+
// @ts-expect-error
61+
incompatibleMiddleware
62+
)
63+
64+
const withMiddleware = untypedInstance.withMiddleware.withTypes<{
65+
state: number
66+
dispatch: AppDispatch
67+
}>()
68+
69+
const dispatch = store.dispatch(
70+
withMiddleware(
71+
compatibleMiddleware,
72+
// @ts-expect-error
73+
incompatibleMiddleware
74+
)
75+
)
76+
})
77+
78+
test('withMiddleware returns typed dispatch, with any applicable extensions', () => {
79+
const dispatch = store.dispatch(
80+
typedInstance.withMiddleware(addedMiddleware)
81+
)
82+
83+
// standard
84+
expectTypeOf(dispatch({ type: 'foo' })).toEqualTypeOf<Action<string>>()
85+
86+
// thunk
87+
expectTypeOf(dispatch(() => 'foo')).toBeString()
88+
89+
// static
90+
expectTypeOf(dispatch(1)).toEqualTypeOf<1>()
91+
92+
// added
93+
expectTypeOf(dispatch(2)).toEqualTypeOf<2>()
94+
})
95+
})

packages/toolkit/src/dynamicMiddleware/tests/index.typetest.ts

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import type { Context } from 'react'
2+
import type { ReactReduxContextValue } from 'react-redux'
3+
import type { Action, Middleware, UnknownAction } from 'redux'
4+
import type { ThunkDispatch } from 'redux-thunk'
5+
import { createDynamicMiddleware } from '../react'
6+
7+
interface AppDispatch extends ThunkDispatch<number, undefined, UnknownAction> {
8+
(n: 1): 1
9+
}
10+
11+
const untypedInstance = createDynamicMiddleware()
12+
13+
const typedInstance = createDynamicMiddleware<number, AppDispatch>()
14+
15+
declare const compatibleMiddleware: Middleware<{}, number, AppDispatch>
16+
declare const incompatibleMiddleware: Middleware<{}, string, AppDispatch>
17+
18+
declare const customContext: Context<ReactReduxContextValue>
19+
20+
declare const addedMiddleware: Middleware<(n: 2) => 2>
21+
22+
describe('type tests', () => {
23+
test('instance typed at creation enforces correct middleware type', () => {
24+
const useDispatch = typedInstance.createDispatchWithMiddlewareHook(
25+
compatibleMiddleware,
26+
// @ts-expect-error
27+
incompatibleMiddleware
28+
)
29+
30+
const createDispatchWithMiddlewareHook =
31+
typedInstance.createDispatchWithMiddlewareHookFactory(customContext)
32+
const useDispatchWithContext = createDispatchWithMiddlewareHook(
33+
compatibleMiddleware,
34+
// @ts-expect-error
35+
incompatibleMiddleware
36+
)
37+
})
38+
39+
test('withTypes() enforces correct middleware type', () => {
40+
const createDispatchWithMiddlewareHook =
41+
untypedInstance.createDispatchWithMiddlewareHook.withTypes<{
42+
state: number
43+
dispatch: AppDispatch
44+
}>()
45+
const useDispatch = createDispatchWithMiddlewareHook(
46+
compatibleMiddleware,
47+
// @ts-expect-error
48+
incompatibleMiddleware
49+
)
50+
51+
const createCustomDispatchWithMiddlewareHook = untypedInstance
52+
.createDispatchWithMiddlewareHookFactory(customContext)
53+
.withTypes<{
54+
state: number
55+
dispatch: AppDispatch
56+
}>()
57+
const useCustomDispatch = createCustomDispatchWithMiddlewareHook(
58+
compatibleMiddleware,
59+
// @ts-expect-error
60+
incompatibleMiddleware
61+
)
62+
})
63+
64+
test('useDispatchWithMW returns typed dispatch, with any applicable extensions', () => {
65+
const useDispatchWithMW =
66+
typedInstance.createDispatchWithMiddlewareHook(addedMiddleware)
67+
const dispatch = useDispatchWithMW()
68+
69+
// standard
70+
expectTypeOf(dispatch({ type: 'foo' })).toEqualTypeOf<Action<string>>()
71+
72+
// thunk
73+
expectTypeOf(dispatch(() => 'foo')).toBeString()
74+
75+
// static
76+
expectTypeOf(dispatch(1)).toEqualTypeOf<1>()
77+
78+
// added
79+
expectTypeOf(dispatch(2)).toEqualTypeOf<2>()
80+
})
81+
})

packages/toolkit/src/dynamicMiddleware/tests/react.typetest.ts

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)