-
Notifications
You must be signed in to change notification settings - Fork 766
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use withHOC for wrapping components in providers
- Loading branch information
Showing
5 changed files
with
146 additions
and
36 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
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
53 changes: 53 additions & 0 deletions
53
packages/core/src/utils/__snapshots__/withHOC.spec.tsx.snap
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,53 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`withHOC renders component with HOC 1`] = ` | ||
<div> | ||
<div> | ||
{} | ||
null | ||
<div> | ||
{"myProp":"component-prop"} | ||
null | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`withHOC renders component with HOC and HOC props 1`] = ` | ||
<div> | ||
<div> | ||
{"myProp":"hoc-prop"} | ||
null | ||
<div> | ||
{"myProp":"component-prop"} | ||
null | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`withHOC renders component with HOC and HOC ref 1`] = ` | ||
<div> | ||
<div> | ||
{} | ||
{"current":{"myRef":"hoc-ref"}} | ||
<div> | ||
{"myProp":"component-prop"} | ||
null | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`withHOC renders component with HOC and component ref 1`] = ` | ||
<div> | ||
<div> | ||
{} | ||
null | ||
<div> | ||
{"myProp":"component-prop"} | ||
{"current":{"myRef":"component-ref"}} | ||
</div> | ||
</div> | ||
</div> | ||
`; |
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,67 @@ | ||
import React, { ReactNode } from 'react'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import { withHOC } from './withHOC'; | ||
|
||
/* eslint-disable react/display-name */ | ||
describe('withHOC', () => { | ||
type DummyRef = { myRef: string }; | ||
type DummyProps = { myProp: string }; | ||
|
||
const HOC = React.forwardRef<DummyRef, DummyProps & { children: ReactNode }>( | ||
({ children, ...props }, ref) => ( | ||
<div> | ||
{JSON.stringify(props)} | ||
{JSON.stringify(ref)} | ||
{children} | ||
</div> | ||
) | ||
); | ||
|
||
const Component = React.forwardRef<DummyRef, DummyProps>((props, ref) => ( | ||
<div> | ||
{JSON.stringify(props)} | ||
{JSON.stringify(ref)} | ||
</div> | ||
)); | ||
|
||
it('renders component with HOC', () => { | ||
const WithHOC = withHOC(HOC, Component); | ||
|
||
const { container } = render(<WithHOC myProp="component-prop" />); | ||
|
||
expect(container).toHaveTextContent('component-prop'); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders component with HOC and HOC props', () => { | ||
const WithHOC = withHOC(HOC, Component, { myProp: 'hoc-prop' }); | ||
|
||
const { container } = render(<WithHOC myProp="component-prop" />); | ||
|
||
expect(container).toHaveTextContent('hoc-prop'); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders component with HOC and HOC ref', () => { | ||
const hocRef = { current: { myRef: 'hoc-ref' } }; | ||
const WithHOC = withHOC(HOC, Component, undefined, hocRef); | ||
|
||
const { container } = render(<WithHOC myProp="component-prop" />); | ||
|
||
expect(container).toHaveTextContent('hoc-ref'); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders component with HOC and component ref', () => { | ||
const componentRef = { current: { myRef: 'component-ref' } }; | ||
const WithHOC = withHOC(HOC, Component); | ||
|
||
const { container } = render( | ||
<WithHOC myProp="component-prop" ref={componentRef} /> | ||
); | ||
|
||
expect(container).toHaveTextContent('component-ref'); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,14 +1,18 @@ | ||
import React, { FunctionComponent } from 'react'; | ||
import React from 'react'; | ||
|
||
export const withHOC = <T,>( | ||
HOC: FunctionComponent<any>, | ||
Component: FunctionComponent<T>, | ||
hocProps?: any | ||
): FunctionComponent<T> => | ||
function hoc(props: T) { | ||
return ( | ||
<HOC {...hocProps}> | ||
<Component {...(props as any)} /> | ||
</HOC> | ||
); | ||
}; | ||
type RefComponent<P, R> = React.FC<P> & { | ||
ref?: React.Ref<R>; | ||
}; | ||
|
||
/* eslint-disable react/display-name */ | ||
export const withHOC = <ComponentProps, HOCProps, ComponentRef, HOCRef>( | ||
HOC: RefComponent<HOCProps, HOCRef>, | ||
Component: RefComponent<ComponentProps, ComponentRef>, | ||
hocProps?: Omit<HOCProps, 'children'>, | ||
hocRef?: React.Ref<HOCRef> | ||
) => | ||
React.forwardRef<ComponentRef, ComponentProps>((props, componentRef) => ( | ||
<HOC {...(hocProps as any)} ref={hocRef}> | ||
<Component {...props} ref={componentRef} /> | ||
</HOC> | ||
)); |