Skip to content

Commit

Permalink
feat(alienstore): add getReducerMap method
Browse files Browse the repository at this point in the history
returns the current register that are registered on the store

improves #43
  • Loading branch information
aneurysmjs committed Nov 4, 2019
1 parent 77a435a commit e924018
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
47 changes: 42 additions & 5 deletions src/app/store/config/alienStore/alienStore.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import { renderHook } from '@testing-library/react-hooks';
import { AnyAction } from 'redux';
import alienStore, {
createStore,
reloadStore,
getReducerMap,
injectReducers,
withStoreModule,
reloadStore,
useAlienModule,
withStoreModule,
} from './alienStore';

const WRONG_COMPONENT_PATH = './some/wrong/component/path';

describe('Dyno Store', () => {
describe('alienStore', () => {
it('should have main methods', () => {
expect(alienStore).toHaveProperty('createStore');
expect(alienStore).toHaveProperty('reloadStore');
expect(alienStore).toHaveProperty('getReducerMap');
expect(alienStore).toHaveProperty('injectReducers');
expect(alienStore).toHaveProperty('withStoreModule');
expect(alienStore).toHaveProperty('reloadStore');
expect(alienStore).toHaveProperty('useAlienModule');
expect(alienStore).toHaveProperty('withStoreModule');
});

describe('create store', () => {
Expand Down Expand Up @@ -52,6 +54,41 @@ describe('Dyno Store', () => {
});
});

describe('getReducerMap', () => {
it('should return the current reducers which is just the default', () => {
const initialReducer = {
defaultState: (): string => 'default state value',
};
createStore(undefined);
const currentReducers = getReducerMap();
/**
* @desc since Object equality fails when object contains a function
*
* this spec fails:
* it("can't compare functions", () => {
* expect(() => {}).toEqual(() => {});
* });
*
* Because functions are compared via reference equality
* it('does reference equality', () => {
* const fn = function() {};
* expect(fn).toEqual(fn);
* });
*
*/
// so just compare by their keys at least
expect(Object.keys(currentReducers)).toEqual(Object.keys(initialReducer));
});
it('should return the current reducers also when they are given', () => {
const initialReducers = {
reducer1: (): string => 'reducer1 value',
reducer2: (): string => 'reducer1 value',
};
createStore(initialReducers);
const currentReducers = getReducerMap();
expect(Object.keys(currentReducers)).toEqual(Object.keys(initialReducers));
});
});
describe('injectReducers', () => {
it('should add reducer and reload the store', () => {
const store = createStore(undefined);
Expand Down
9 changes: 6 additions & 3 deletions src/app/store/config/alienStore/alienStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const alienReducer = {
defaultState: (): string => 'default state value',
};

let reducerMap: ReducerMap = {};
let reducerMap = {};

const createRootReducer = (): Reducer => {
return combineReducers(reducerMap);
Expand All @@ -31,6 +31,8 @@ export const createStore = (
return store;
};

export const getReducerMap = (): typeof reducerMap => reducerMap;

export const reloadStore = (): void => {
store.replaceReducer(createRootReducer());
if (process.env.NODE_ENV !== 'production') {
Expand Down Expand Up @@ -100,8 +102,9 @@ export function useAlienModule<P>(moduleStore: UseAlienModuleImportType<P>): P |

export default {
createStore,
reloadStore,
getReducerMap,
injectReducers,
withStoreModule,
reloadStore,
useAlienModule,
withStoreModule,
};

0 comments on commit e924018

Please sign in to comment.