Skip to content

Commit

Permalink
fix(alienstore): improve useAlienModule
Browse files Browse the repository at this point in the history
check whether the imported module has a "reducers" property, if he does, inject it, othewise inject
the whole module
  • Loading branch information
aneurysmjs committed Nov 3, 2019
1 parent 5930895 commit 5f5f881
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
39 changes: 25 additions & 14 deletions src/app/store/config/alienStore/alienStore.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,44 @@ describe('Dyno Store', () => {

it('should create and return a Redux store', () => {
const reduxStore = createReduxStore(() => ({}));
const store = createStore();
const store = createStore(undefined);
expect(JSON.stringify(store, null)).toEqual(JSON.stringify(reduxStore, null));
});

it('should trigger store\'s "dispatch" when reloading the store', () => {
const store = createStore();
const store = createStore(undefined);
const mockDispatch = jest.spyOn(store, 'dispatch');
reloadStore();
expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(mockDispatch).toHaveBeenCalledWith({ type: '@@ALIEN_STORE/RELOAD' });
});

it('should add reducer and reload the store', () => {
const store = createStore();
const reducer = { INIT_DYNO_STATE: (): {} => ({}) };
const mockDispatch = jest.spyOn(store, 'dispatch');
injectReducers(reducer);
expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(mockDispatch).toHaveBeenCalledWith({ type: '@@ALIEN_STORE/RELOAD' });
describe('injectReducers', () => {
it('should add reducer and reload the store', () => {
const store = createStore(undefined);
const reducer = { OTHER_REDUCER: (): {} => ({}) };
const mockDispatch = jest.spyOn(store, 'dispatch');
injectReducers(reducer);
expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(mockDispatch).toHaveBeenCalledWith({ type: '@@ALIEN_STORE/RELOAD' });
});

it('should not add an already existing reducer', () => {
const store = createStore(undefined);
const reducer = { INIT_REDUCER: (): {} => ({}) };
const mockDispatch = jest.spyOn(store, 'dispatch');
injectReducers(reducer);
expect(mockDispatch).toHaveBeenCalledTimes(0);
expect(mockDispatch).not.toHaveBeenCalledWith({ type: '@@ALIEN_STORE/RELOAD' });
});
});

describe('test "withStoreModule"', () => {
it('should add reducer and reload the store', async () => {
const Example = (): ReactElement => <div>some component</div>;
const component = Promise.resolve({ default: Example });
const reducer = Promise.resolve({ default: { INIT_DYNO_STATE: (): {} => ({}) } });
const store = createStore();
const reducer = Promise.resolve({ default: { INIT_REDUCER: (): {} => ({}) } });
const store = createStore(undefined);
const mockDispatch = jest.spyOn(store, 'dispatch');
const module = await withStoreModule(component, reducer);

Expand All @@ -60,7 +71,7 @@ describe('Dyno Store', () => {

it('should throw if there is an error', async () => {
expect.assertions(3);
const store = createStore();
const store = createStore(undefined);
const mockDispatch = jest.spyOn(store, 'dispatch');
const errorMessage =
// eslint-disable-next-line quotes
Expand Down Expand Up @@ -105,7 +116,7 @@ describe('Dyno Store', () => {
type AlienModuleType = Promise<{ default: typeof alienModuleMock }>;

it('should render "null" at first and then resolve the module', async () => {
const store = createStore();
const store = createStore(undefined);
const mockDispatch = jest.spyOn(store, 'dispatch');
const importAlienModule = (): AlienModuleType =>
Promise.resolve({ default: alienModuleMock });
Expand All @@ -123,7 +134,7 @@ describe('Dyno Store', () => {
});

it('should throw', async () => {
const store = createStore();
const store = createStore(undefined);
const mockDispatch = jest.spyOn(store, 'dispatch');
// @ts-ignore
const importAlienModule = (): AlienModuleType => import(WRONG_COMPONENT_PATH); // eslint-disable-line import/no-unresolved
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 @@ -7,12 +7,14 @@ import {
StoreEnhancer,
} from 'redux';

import { ReducerMap, StoreShape, INIT_DYNO_STATE } from '~/shared/types';
import { ReducerMap, StoreShape } from '~/shared/types';

const INIT_REDUCER = (): {} => ({});

let store = {} as Store;

const alienReducer = {
[INIT_DYNO_STATE]: (): {} => ({}),
INIT_REDUCER,
};

let reducerMap: ReducerMap = {};
Expand Down Expand Up @@ -81,7 +83,8 @@ export function useAlienModule<P>(moduleStore: UseAlienModuleImportType<P>): P |
(async (): Promise<void> => {
try {
const module = await moduleStore();
injectReducers(module.reducers);
const reducerToInject = module.reducers ? module.reducers : module;
injectReducers(reducerToInject);
setAlienModule(module);
} catch (err) {
// throw new Error(`useAlienModule error: ${err}`);
Expand Down

0 comments on commit 5f5f881

Please sign in to comment.