I have a HOC to test, during shallow mount I should to call some class methods:
it('Should not call dispatch', () => {
const dispatch = jest.fn()
const WrappedComponent = someHoc(DummyComponent)
const instance = shallow(
<WrappedComponent
dispatch={dispatch}
/>,
).instance() as WrappedComponent
instance.someMethod()
expect(dispatch).toHaveBeenCalledTimes(0)
})
test works fine but TS compiler throws an error
Cannot find name 'WrappedComponent'.
And it is right because WrappedComponent is not a type or class, but if I remove the
as WrappedComponent
line, TS throws an error
Property 'someMethod' does not exist on type 'Component<{}, {}, any>'.
Also, it does not compile if I change that line as
as typeof WrappedComponent
someHoc description:
import ...
interface State {
/*state*/
}
interface Props {
dispatch: Dispatch<Action>
/*props*/
}
export someHoc = <T extends {}>(
ChildComponent: React.ComponentClass<T>,
) => {
class Wrapper extends React.PureComponent<T & Props, State> {
someMethod = () => {
/*do smth*/
}
render() {
return (
<div>
<ChildComponent {...this.props} />
</div>
)
}
}
return Wrapper
}
How can I type the HOC instance? Thanks.
someHoc
? How is it typed? Also, it does not compile if I change that line as - what is the error? – Such