Skip to content

Commit

Permalink
feat(alien): add withAlien higher-order hook
Browse files Browse the repository at this point in the history
just revolve the redux module import and pass the result as props for the given component

#43
  • Loading branch information
aneurysmjs committed Dec 8, 2019
1 parent b6f3a7c commit 2869ef1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/app/store/config/alienStore/withAlien.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { ReactElement, ReactNode, ComponentType } from 'react';
import { Store, AnyAction } from 'redux';
import { Provider } from 'react-redux';
import { renderHook } from '@testing-library/react-hooks';

import alien from './alien';

import withAlien from './withAlien';

let store = {} as Store;

beforeEach(() => {
store = alien();
});

type WrapperProps = {
children?: ReactNode;
};

// eslint-disable-next-line react/prop-types
const wrapper: ComponentType<WrapperProps> = ({ children }) => (
<Provider store={store}>{children}</Provider>
);

describe('test "withAlien"', () => {
const reduxModule = {
reducers: {
dummy: (state: { name: string } = { name: 'stupid' }, action: AnyAction): typeof state => {
switch (action.type) {
case 'DUMMY_ACTION':
return {
name: action.name,
};
default:
return state;
}
},
},
actions: {
dummyAction: (): AnyAction => ({
type: 'DUMMY_ACTION',
payload: {
name: 'Джеро',
},
}),
},
};
it('should resolve and alien module and add actions to the Component', async () => {
const Example = (): ReactElement => <div>some component</div>;

const getModule = (): Promise<typeof reduxModule> => Promise.resolve(reduxModule);

const { result, waitForNextUpdate } = renderHook(() => withAlien(Example, getModule), {
wrapper,
});

expect(result.current).toBe(null);

await waitForNextUpdate();

expect(result.current.props.actions).toStrictEqual(reduxModule.actions);
});
});
13 changes: 13 additions & 0 deletions src/app/store/config/alienStore/withAlien.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { ComponentType, useEffect } from 'react';

import useAlien from './useAlien';

function withAlien<T>(Component: ComponentType, getModule): ComponentType | null {
const alienResult = useAlien<ReturnType<getModule>>({
getModule,
});

return alienResult ? <Component actions={alienResult.actions} /> : null;
}

export default withAlien;

0 comments on commit 2869ef1

Please sign in to comment.