New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to mock local state of Redux connected component #828
Comments
Hi @wec84, Thank you for the elaborate response! Unfortunately this is a known limitation (for now), so maybe we add this to the docs to inform people sooner. The problem is this: Once you wrap a component, the instance of the inner component is no longer accessible, it's a private detail of the wrapper component, which what Cosmos receives when you have a fixture for a wrapped component. But what about forwardRef, you might ask? Yes, that is the cure. But redux-connect does not use it yet. Here's a thread: reduxjs/react-redux#914 |
Hi, I thing you can use this pattern to wrap your third party HOC (the sample is for the Yahoo injectIntl HOC): // @flow
// injectIntl.js
// Intl flow definitions managed by flow-typed:
// https://github.com/flow-typed/flow-typed
import * as React from 'react';
import { injectIntl as originalInjectIntl } from "react-intl";
// noinspection ES6CheckImport
import type { IntlShape } from "react-intl";
// constant
const __DEV__ = process.env.NODE_ENV !== 'production';
// utils
function getComponentName(Component: React.ComponentType<any>): string {
if (typeof Component === 'object') {
// $FlowFixMe React.forwardRef does not have a flow definition at the moment
return Component.render.displayName;
}
return Component.displayName || Component.name || 'Component';
}
function getComponentWithProperName<EC: React.ComponentType<any>, WC: React.ComponentType<any>>(
EnhancedComponent: EC,
WrappedComponent: WC,
HigherOrderComponent: (WC, ...other: Array<any>) => EC
): EC {
if (__DEV__) {
// noinspection JSUndefinedPropertyAssignment
EnhancedComponent.displayName =
`${HigherOrderComponent.name}(${getComponentName(WrappedComponent)})`;
}
return EnhancedComponent;
}
// your new HOC
export default function injectIntl<PropsWC: {},
PropsEC: $Diff<PropsWC, {|
intl: IntlShape | void,
|}>>(
WrappedComponent: React.ComponentType<PropsWC>
): React.ComponentType<PropsEC> {
class PropRefRouterComponent extends React.Component<PropsWC & { forwardedRef: * }> {
render() {
const {forwardedRef, ...other} = this.props;
return (
<WrappedComponent {...other}
ref={forwardedRef}/>
)
}
}
const WithIntlComponent = originalInjectIntl(PropRefRouterComponent);
function forwardRefRenderFn(props: PropsEC, ref: *) {
return <WithIntlComponent {...props} forwardedRef={ref}/>;
}
// $FlowFixMe React.forwardRef does not have a flow definition at the moment
return React.forwardRef(
getComponentWithProperName(forwardRefRenderFn, WrappedComponent, injectIntl));
} |
@skidding just so you're aware it appears that they decided to release it in 5.1.0. I've upgraded my versions of redux and react-redux to the latest versions and it doesn't appear that this fixes the issue. Are you aware of changes that need to be made to this project in order for it to work? |
Thanks for the heads-up, @adam8810! I didn't get my hands on react-redux 5.1 yet. If you share an example repo I'll look into it and report back with my findings! |
I glanced quickly over this commit and connectAdvanced.js, and it seems forwardRef is opt-in. You may need to pass Let me know if it works. |
Let me know if you have any update on this. Note that react-redux6+ isn't supported at the moment. |
What's wrong?
I want to mock state in a fixture for a Redux-connected component. It works when my component is not connected, but when my component is connected, the state in the fixture is not applied.
Steps to reproduce
Fixture which reproduces the issue:
Screenshots
Observed results in cosmos ui:
User info
Additional Context
Not sure if it matters, but I also reproduced this in the headless environment:
Test code producing the above:
The text was updated successfully, but these errors were encountered: