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

False Negative: Add support to React.memo 2nd arg "areEquals" #2128

Closed
3 of 4 tasks
sandrina-p opened this issue May 21, 2019 · 3 comments
Closed
3 of 4 tasks

False Negative: Add support to React.memo 2nd arg "areEquals" #2128

sandrina-p opened this issue May 21, 2019 · 3 comments

Comments

@sandrina-p
Copy link

sandrina-p commented May 21, 2019

Current behavior

Function Components with React.memo(Component, areEquals) fails when I want to assert that a re-render didn't happen. When using Class Components that bug doesn't happen.

Expected behavior

Function Components with React.memo(Component, areEquals) should not fail when asserting a scenario where a re-render didn't happen.

function DumbMemo({ number }) {
  return <span>It's number {number}</span>;
}

function areEqual(props, nextProps) {
  // this function isn't covered by Enzyme, so the test fails:
  return nextProps.number > 10 && nextProps.number < 20;
}

export default React.memo(DumbMemo, areEqual);
it("<DumbMemo /> - should only re-render when number is between 10 and 20", () => {
  // shallow or mount, both have the same bug
  const tree = shallow(
    <DumbMemo number={5} />
  );

  expect(tree.text()).toBe("It's number 5");

  tree.setProps({ number: 15 });

  // BUG: It should pass, but it fails returning "It's number 15"
  expect(tree.text()).toBe("It's number 5");
});

See Demo at CodeSandbox.

Your environment

  • Mac High Sierra, Codesandbox
  • node 8.11.4
  • npm 5.6

API

  • shallow
  • mount
  • render

Version

library version
enzyme 3.9.0
react 16.8.6
react-dom 16.8.6
react-test-renderer N/A

Adapter

  • enzyme-adapter-react-16
@sandrina-p sandrina-p changed the title Support React.memo() 2nd arg areEquals False Negative: Missing support to React.memo 2nd arg "areEquals" May 21, 2019
@sandrina-p sandrina-p changed the title False Negative: Missing support to React.memo 2nd arg "areEquals" False Negative: Add support to React.memo 2nd arg "areEquals" May 21, 2019
@ljharb ljharb self-assigned this May 22, 2019
@ljharb
Copy link
Member

ljharb commented May 22, 2019

Thanks! I had no idea memo took a second argument.

@sandrina-p
Copy link
Author

You can check on their docs - React.memo(). The second works like shouldComponentUpdate to "override" the props comparison.

@ljharb
Copy link
Member

ljharb commented May 22, 2019

Fix incoming.

@ljharb ljharb closed this as completed in d94d25d May 22, 2019
ljharb added a commit that referenced this issue May 26, 2019
 - [fix] ensure the correct displayName for `memo` components (#2137)
 - [fix] `mount`: wrap simulate() in ReactTestUtils.act() (#2135)
 - [fix] properly handle `memo`’s `areEquals` arg (#2128)
 - [dev deps] update `eslint-plugin-import`
 - [deps] update `semver`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants