import { AnyAction , Store , Unsubscribe } from 'redux' ; import { createMockDispatch , MockDispatch } from './DispatchMock' ; export interface MockStore < S , T extends AnyAction > extends Omit < Store < S , T > , 'Symbol.observable' > , MockDispatch < T > { } const isFunction = ( arg ) : boolean => typeof arg === 'function' ; export const emptyStore = ( ) : Omit < Store < { } , AnyAction > , 'Symbol.observable' > => ( { dispatch < A extends AnyAction > ( action ) { return action ; } , getState ( ) { return { } ; } , subscribe ( ) { return ( ) : void => { } ; } , replaceReducer ( ) { } , } ) ; export const createMockStore = < S , T extends AnyAction > ( state ) : MockStore < S , T > => { const dispatchMock = createMockDispatch < T > ( ) ; const subscribers = [ ] ; return { getState ( ) { return state ; } , replaceReducer ( ) { } , subscribe ( subscriber ) { if ( isFunction ( subscriber ) ) { subscribers . push ( subscriber ) ; } return subscriber ; } , dispatch : < A extends T > ( action ) : A => { for ( let i = 0 ; i < subscribers . length ; i ++ ) { subscribers [ i ] ( ) ; } return dispatchMock . dispatch ( action ) ; } , getActions : dispatchMock . getActions , getAction : dispatchMock . getAction , isActionDispatched : dispatchMock . isActionDispatched , isActionTypeDispatched : dispatchMock . isActionTypeDispatched , } ; } ;