MCPcopy
hub / github.com/reduxjs/redux / apply

Function apply

test/typescript/middleware.ts:120–207  ·  view source on GitHub ↗

* Test the type of store.dispatch after applying different middleware.

()

Source from the content-addressed store, hash-verified

118 * Test the type of store.dispatch after applying different middleware.
119 */
120function apply() {
121 interface State {
122 someField: 'string'
123 }
124 const reducer: Reducer<State> = null as any
125
126 /**
127 * logger
128 */
129 const storeWithLogger = createStore(reducer, applyMiddleware(logger()))
130 // can only dispatch actions
131 storeWithLogger.dispatch({ type: 'INCREMENT' })
132 // @ts-expect-error
133 storeWithLogger.dispatch(Promise.resolve({ type: 'INCREMENT' }))
134 // @ts-expect-error
135 storeWithLogger.dispatch('not-an-action')
136
137 /**
138 * promise
139 */
140 const storeWithPromise = createStore(reducer, applyMiddleware(promise()))
141 // can dispatch actions and promises
142 storeWithPromise.dispatch({ type: 'INCREMENT' })
143 storeWithPromise.dispatch(Promise.resolve({ type: 'INCREMENT' }))
144 // @ts-expect-error
145 storeWithPromise.dispatch('not-an-action')
146 // @ts-expect-error
147 storeWithPromise.dispatch(Promise.resolve('not-an-action'))
148
149 /**
150 * promise + logger
151 */
152 const storeWithPromiseAndLogger = createStore(
153 reducer,
154 applyMiddleware(promise(), logger())
155 )
156 // can dispatch actions and promises
157 storeWithPromiseAndLogger.dispatch({ type: 'INCREMENT' })
158 storeWithPromiseAndLogger.dispatch(Promise.resolve({ type: 'INCREMENT' }))
159 // @ts-expect-error
160 storeWithPromiseAndLogger.dispatch('not-an-action')
161 // @ts-expect-error
162 storeWithPromiseAndLogger.dispatch(Promise.resolve('not-an-action'))
163
164 /**
165 * promise + thunk
166 */
167 const storeWithPromiseAndThunk = createStore(
168 reducer,
169 applyMiddleware(promise(), thunk<State, PromiseDispatch>(), logger())
170 )
171 // can dispatch actions, promises and thunks
172 storeWithPromiseAndThunk.dispatch({ type: 'INCREMENT' })
173 storeWithPromiseAndThunk.dispatch(Promise.resolve({ type: 'INCREMENT' }))
174 storeWithPromiseAndThunk.dispatch((dispatch, getState) => {
175 getState().someField
176 // @ts-expect-error
177 getState().wrongField

Callers

nothing calls this directly

Calls 7

createStoreFunction · 0.85
applyMiddlewareFunction · 0.85
loggerFunction · 0.85
getStateFunction · 0.85
promiseFunction · 0.70
thunkFunction · 0.70
dispatchFunction · 0.50

Tested by

no test coverage detected