diff --git a/src/app/store/config/alienStore/manager.test.ts b/src/app/store/config/alienStore/manager.test.ts index b880f1ce..16a1a6fe 100644 --- a/src/app/store/config/alienStore/manager.test.ts +++ b/src/app/store/config/alienStore/manager.test.ts @@ -1,4 +1,4 @@ -import { AnyAction } from 'redux'; +import { createStore, AnyAction } from 'redux'; import manager from './manager'; @@ -10,12 +10,6 @@ type AlienState = { cart: CartType; }; -const preloadedState: AlienState = { - cart: { - quantity: 30, - }, -}; - describe('manager', () => { it('should return an alienManager', () => { const alienManager = manager(); @@ -23,6 +17,7 @@ describe('manager', () => { expect(alienManager).toHaveProperty('injectReducers'); expect(alienManager).toHaveProperty('removeReducers'); expect(alienManager).toHaveProperty('rootReducer'); + expect(alienManager).toHaveProperty('setDispatch'); }); describe('getReducerMap', () => { @@ -88,4 +83,15 @@ describe('manager', () => { }); }); }); + + describe('setDispatch', () => { + it('should set a Redux dispatcher for internal use', () => { + const store = createStore(() => {}); + const alienManager = manager(); + const setDispatchSpy = jest.spyOn(alienManager, 'setDispatch'); + + alienManager.setDispatch(store.dispatch); + expect(setDispatchSpy).toHaveBeenCalledWith(store.dispatch); + }); + }); }); diff --git a/src/app/store/config/alienStore/manager.ts b/src/app/store/config/alienStore/manager.ts index f49153d6..c99280ba 100644 --- a/src/app/store/config/alienStore/manager.ts +++ b/src/app/store/config/alienStore/manager.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/ban-ts-ignore */ -import { combineReducers, Reducer, AnyAction } from 'redux'; +import { combineReducers, Reducer, AnyAction, Dispatch } from 'redux'; // get the return value if T is a function // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -12,13 +12,16 @@ type FullStoreShape = { type ReducerMapper = Partial<{ [K in keyof Partial]: Reducer }>; -export interface AlienManager { +export interface AlienManager { getReducerMap: () => ReducerMapper>; injectReducers: (key: string, reducer: Reducer) => Reducer | void; removeReducers: (key: string) => void; rootReducer: Reducer; + setDispatch: (storeDispatch: Dispatch) => void; } +type AlienDispatch = Dispatch | null; + export default function manager(initialReducers?: State): AlienManager { type StoreShape = FullStoreShape; @@ -26,12 +29,18 @@ export default function manager(initialReducers?: State): AlienManager ({}); + let dispatch: AlienDispatch = null; + const reducers: ReducerMap = initialReducers ? { ...initialReducers } : {}; // @ts-ignore "combineReducers" doesn't have that overload match let combinedReducer = initialReducers ? combineReducers(reducers) : fallback; let keysToRemove: Array = []; + function setDispatch(storeDispatch: Dispatch): void { + dispatch = storeDispatch; + } + function getReducerMap(): ReducerMap { return reducers; } @@ -44,6 +53,9 @@ export default function manager(initialReducers?: State): AlienManager(initialReducers?: State): AlienManager