-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(alien): add withAlien higher-order hook
just revolve the redux module import and pass the result as props for the given component #43
- Loading branch information
1 parent
b6f3a7c
commit 2869ef1
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |