enzymejs/enzyme

ReactWrapper#update example is incorrect

開放

#2,348 建立於 2020年3月4日

 (1 則留言) (0 個反應) (0 位負責人)JavaScript (1,970 個分叉)batch import
docshelp wanted

倉庫指標

星標
 (19,828 顆星)
PR 合併指標
 (30 天內沒有已合併 PR)

描述

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.

貢獻者指南