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
Enzyme's wrapper.state() returns "null" when use react-onclickoutside #133
Comments
I hadn't even heard of Enzyme before now so unfortunately I think you might be more qualified than I am when it comes to knowing what's happening here =( Might be something the Enzyme project maintainers can shed light on; if they have a github repo you can file an issue and then mention this one to set up an automatic cross-link |
closing this due to inactivity, but feel free to reopen or refile if there is new information |
Bummer...i'm also experiencing this. Trying to write some unit tests on my react component using react-onclickoutside the the call to |
Maybe @lelandrichardson or @ljharb have seem this before and can shed light on what's wrong when you encounter this (might even warrant a little troubleshooting section on http://airbnb.io/enzyme/ but that's well beyond the scope of my involvement...) |
So far, I've found one workaround (by modifying this project). In the componentDidMount, I use the following line:
Essentially setting this wrapper's state object to reference the state of the child instance (i.e. the real component). |
Reading more about similar scenarios here...sounds like its recommended to export the unwrapped class and just do tests around that. Then essentially you can have another export that contains the wrapped version. |
In that case you have getInstance() available on the wrapped component. Give that a shot and if that works we can come up with some troubleshooting text to add to the README.md for |
@ammmze did getInstance work for you? If so, I'd like to update the README.md so that others benefit from an explanation. @jochenberger I see a merged PR, is any of the work done there of the kind that requires code changes in this library? |
@Pomax no, no changes are required, I only referenced this issue because I somehow had to work around it in the PR. |
ah, I see. Thanks for replying! |
closing this for now as it's been a few months and despite ~20k installs a day no one seems to have run into this as a problem large to +1 in the last three months. Should it pop again in the future, work can be done in that future issue. |
@Pomax that Thanks for maintaining this component, which works really nicely in my project! |
If you have : /*MyComponent.js*/
//....
export default ClickedOutSide(MyComponent); Add another line of export : /*MyComponent.js*/
//....
export { MyComponent }; // This export is useful in unit-tests 😉
export default ClickedOutSide(MyComponent); Then in your test/spec file : import { MyComponent } from './MyComponent';
// and NOT [ import MyComponent from './MyComponent'; ] |
@abdennour I strongly discourage doing that; that's what |
I finally found a very good solution to get a wrapper from a decorated component. For shallowed components you can use const wrapper = mount(shallow(<MyComponent />).get(0)) Works like a charm :) |
I use enzyme to test React components.
Recently, I've encountered a problem while testing some of my components where I used
react-onclickoutside
.See following example.
As you can see from the example above. Wrapping
Component
intoonClickOutside
makeswrapper
returnnull
instead of a state object.Any ideas?
The text was updated successfully, but these errors were encountered: