Skip to content

Commit c5863d5

Browse files
committed
Compose enhancers test
1 parent e9e8a23 commit c5863d5

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

test/typescript/enhancers.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StoreEnhancer, Action, AnyAction, Reducer, createStore, StoreEnhancerStoreCreator, Store } from '../..'
1+
import { StoreEnhancer, Action, AnyAction, Reducer, createStore } from '../..'
22

33
interface State {
44
someField: 'string'
@@ -327,3 +327,51 @@ function finalHelmersonExample() {
327327
// typings:expect-error
328328
newStore.getState().wrongField
329329
}
330+
331+
function composedEnhancers() {
332+
interface State {
333+
someState: string;
334+
}
335+
const reducer: Reducer<State> = null as any
336+
337+
interface Ext1 {
338+
enhancer1: string;
339+
}
340+
interface Ext2 {
341+
enhancer2: number;
342+
}
343+
344+
const enhancer1: StoreEnhancer<Ext1> = (createStore) => (reducer, preloadedState) => {
345+
function replaceReducer<NewState, NewActions extends Action>(nextReducer: Reducer<NewState, NewActions>) {
346+
store.replaceReducer(nextReducer)
347+
return newStore
348+
}
349+
const store = createStore(reducer, preloadedState)
350+
const newStore = {
351+
...store,
352+
replaceReducer,
353+
enhancer1: 'foo'
354+
}
355+
return newStore
356+
}
357+
358+
const enhancer2: StoreEnhancer<Ext2> = (createStore) => (reducer, preloadedState) => {
359+
function replaceReducer<NewState, NewActions extends Action>(nextReducer: Reducer<NewState, NewActions>) {
360+
store.replaceReducer(nextReducer)
361+
return newStore
362+
}
363+
const store = createStore(reducer, preloadedState)
364+
const newStore = {
365+
...store,
366+
replaceReducer,
367+
enhancer2: 5
368+
}
369+
return newStore
370+
}
371+
372+
const enhancedStore = createStore(reducer, (createStore) => enhancer2(enhancer1(createStore)));
373+
enhancedStore.enhancer1
374+
enhancedStore.enhancer2
375+
// typings:expect-error
376+
enhancedStore.enhancer3
377+
}

0 commit comments

Comments
 (0)