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

defaultProps for shallow renders do not work with React.memo #2115

Closed
2 of 5 tasks
KamalAman opened this issue May 1, 2019 · 2 comments
Closed
2 of 5 tasks

defaultProps for shallow renders do not work with React.memo #2115

KamalAman opened this issue May 1, 2019 · 2 comments

Comments

@KamalAman
Copy link

KamalAman commented May 1, 2019

Current behavior

Default props from the "inner component" are not applied to memo'd component. The second test fails with <div>NaN</div>

import React from 'react';
import { shallow } from 'enzyme';

const Add = ({ a, b }) => <div>{a + b}</div>;

Add.defaultProps = {
  a: 2,
  b: 3,
};

const MemoAdd = React.memo(Add);

describe('Enzyme Shallow Default Props Test', () => {
  it('default props should be applied to components', () => {
    const wrapper = shallow(<Add />);
    expect(wrapper.html()).toEqual('<div>5</div>');
  });

  it('default props should be applied to memo components', () => {
    const wrapper = shallow(<MemoAdd />);
    expect(wrapper.html()).toEqual('<div>5</div>');
  });
});

Expected behavior

Default props should be applied to the memo component

Your environment

API

  • shallow
  • mount
  • render

Version

library version
enzyme 3.9.0
react 16.8.6
react-dom 16.8.6
react-test-renderer 16.8.6
adapter (below)

Adapter

  • enzyme-adapter-react-16
  • others ( )
@ljharb
Copy link
Member

ljharb commented May 2, 2019

Note that using .html() invokes render - prefer .debug(). However, this still seems to fail with debug, so thanks, it's a bug :-)

@ljharb
Copy link
Member

ljharb commented May 2, 2019

This actually seems like a bug in the shallow renderer with memoized components. I've filed facebook/react#15556 to track it.

ljharb added a commit that referenced this issue May 2, 2019
@ljharb ljharb closed this as completed in aa9828a May 2, 2019
ljharb added a commit that referenced this issue May 12, 2019
 - [new] support `suspenseFallback` option; support `Suspense`/`Lazy` (#1975)
 - [fix] `shallow`: properly dive through `memo` components (#2103, #2068)
 - [fix] avoid a Context.Provider deprecation warning
 - [fix] shallow renderer for `memo` does not respect `defaultProps` (#2115)
 - [fix] Don’t show wrapped component displayName in lazy component (#1975)
 - [fix] `simulateEvent`: call the adapter’s implementation, not the raw one (#2100)
 - [deps] update `enzyme-adapter-utils`
 - [dev deps] update `eslint-plugin-react`, `eslint-plugin-import`
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