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

Connected components loses its original name #128

Open
andolf opened this issue Nov 26, 2018 · 1 comment
Open

Connected components loses its original name #128

andolf opened this issue Nov 26, 2018 · 1 comment

Comments

@andolf
Copy link

andolf commented Nov 26, 2018

It seems that connecting a component will mean that it loses its original name. Down below is a test case where I was able to reproduce it.

it('should not affect display name', () => {
  const Child = () => <div>Foo</div>;
  const ConnectedChild = connect(Object)(Child);
  expect(Child.name).toEqual('Child');
  expect(ConnectedChild.name).toEqual('Child'); // becomes "Wrapper"
});

The reason this is a problem for us is because we're using shallow render in some test cases to only render a certain amount of levels of depth to verify that a correct component was mounted, but unfortunately the name is lost unless we explicitly set displayName on the connected component before exporting and rendering it.

EDIT: Forgot to mention that is using Preact.

@orzechowskid
Copy link

I think that losing the original name of the component is to be expected, since connect returns a higher-order component. However, setting a displayName of "Wrapper" for every connected component doesn't seem great.

It's pretty easy to set the displayName of the Wrapper HOC to something more useful:

--- a/src/integrations/preact.js
+++ b/src/integrations/preact.js
@@ -43,6 +43,7 @@ export function connect(mapStateToProps, actions) {
                        };
                        this.render = props => h(Child, assign(assign(assign({}, boundActions), props), state));
                }
+               Wrapper.displayName = `connect(${Child.displayName || Child.name})`;
                return (Wrapper.prototype = new Component()).constructor = Wrapper;
        };
 }

But this causes an integration test to fail since unistore now compiles to a file which is larger than the 750B threshold specified by the test, so I don't know if I should open a PR or not (or even if this is the preferred approach to solve the problem).

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

2 participants