source: node_modules/@reduxjs/toolkit/src/dynamicMiddleware/tests/react.test-d.ts@ a762898

Last change on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 2.5 KB
Line 
1import type { Context } from 'react'
2import type { ReactReduxContextValue } from 'react-redux'
3import type { Action, Middleware, UnknownAction } from 'redux'
4import type { ThunkDispatch } from 'redux-thunk'
5import { createDynamicMiddleware } from '../react'
6
7interface AppDispatch extends ThunkDispatch<number, undefined, UnknownAction> {
8 (n: 1): 1
9}
10
11const untypedInstance = createDynamicMiddleware()
12
13const typedInstance = createDynamicMiddleware<number, AppDispatch>()
14
15declare const compatibleMiddleware: Middleware<{}, number, AppDispatch>
16declare const incompatibleMiddleware: Middleware<{}, string, AppDispatch>
17
18declare const customContext: Context<ReactReduxContextValue | null>
19
20declare const addedMiddleware: Middleware<(n: 2) => 2>
21
22describe('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})
Note: See TracBrowser for help on using the repository browser.