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

ReactWrapper#update example is incorrect #2348

Open
robertknight opened this issue Mar 4, 2020 · 1 comment
Open

ReactWrapper#update example is incorrect #2348

robertknight opened this issue Mar 4, 2020 · 1 comment
Labels

Comments

@robertknight
Copy link
Contributor

@robertknight robertknight commented Mar 4, 2020

The code example for ReactWrapper's update method (https://github.com/enzymejs/enzyme/blob/master/docs/api/ReactWrapper/update.md#example) is incorrect.

I'll reproduce it here:

class ImpureRender extends React.Component {
  constructor(props) {
    super(props);
    this.count = 0;
  }

  render() {
    this.count += 1;
    return <div>{this.count}</div>;
  }
}

const wrapper = mount(<ImpureRender />);
expect(wrapper.text()).to.equal('0');
wrapper.update();
expect(wrapper.text()).to.equal('1');

The first issue is that the initial render output is "1" not "0" because the counter is incremented on the first render before this.value is used in the return value. The second issue is that incrementing the counter is exactly what doesn't happen when using wrapper.update() (unlike wrapper.setProps()).

I didn't just submit a PR to fix these two details because I think a bigger revision is needed to illustrate the difference between wrapper.update() and wrapper.setProps({}) more clearly, but I'm not sure what yet.

@ljharb
Copy link
Member

@ljharb ljharb commented Mar 4, 2020

Thanks, I agree some improvements are needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.