Skip to content
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 does not rerender MobX components with @observable using shallow rendering #1267

Closed
d4rky-pl opened this issue Oct 16, 2017 · 5 comments

Comments

@d4rky-pl
Copy link

When using enzyme with MobX and shallow rendering, it used to properly rerender components when changing @observable value. Unfortunately this is no longer the case with React v16.

I've prepared a test repository. You can see that tests run correctly on React 15 with enzyme-adapter-react-15 but exactly the same code fails with React 16 and enzyme-adapter-react-16 (same enzyme version).

Full mount works correctly.

@d4rky-pl
Copy link
Author

I've tracked this bug down to a bug in react-test-renderer/shallow. I'm not sure if this issue should be closed or not as enzyme "inherits" this buggy behaviour and other people may stumble upon it and look here.

@bradyep
Copy link

bradyep commented Mar 6, 2018

I am experiencing this issue as well. Changes to observable values are not affecting the shallow rendering. Is there any workaround or way to fix this?

@ljharb
Copy link
Member

ljharb commented Mar 6, 2018

I think mobx will need it’s own adapter; it’s not guaranteed it will work with any of the React adapters.

@bradyep
Copy link

bradyep commented Mar 6, 2018

I believe I've found a workaround, and since this issue is currently my top Google result for 'enzyme mobx react 16', I'll post it here. I got it from the comment from 'dsumer' here: #1184

Everytime I want anything dependent on an observable to be updated, I just call setState on the wrapper:

  viewStore.openModal();
  wrapper.setState({ forceUpdateCall: 1 });
  expect(wrapper.find(Modal).prop('isOpen')).toBeTruthy();

I don't know if you need to supply a different state each time in order for things to update, but I personally increment that forceUpdateCall value on every call out of paranoia.

This obviously undercuts the premise of MobX where things should 'just update', but it works for me without too much hassle. I'd love to not have to do this so if anyone knows of a fix or better workaround, please let me know. Thanks.

@ljharb
Copy link
Member

ljharb commented Jul 6, 2018

Closing this; if anyone builds a MobX enzyme adapter, it'd be great to link it in the docs.

@ljharb ljharb closed this as completed Jul 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants