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

Support connect() usage #8

Closed
JoshuaKGoldberg opened this issue May 25, 2020 · 1 comment · Fixed by #10
Closed

Support connect() usage #8

JoshuaKGoldberg opened this issue May 25, 2020 · 1 comment · Fixed by #10
Assignees
Labels
type: feature New enhancement or request

Comments

@JoshuaKGoldberg
Copy link
Owner

Had a good brief chat about downsides with @ian-craig, who brought up the good point that only targeted hook usage leaves out all applications that use connect().

In theory it should be possible to support connect(). Let's do it!

@JoshuaKGoldberg
Copy link
Owner Author

I investigated a little bit and this seems like it should be doable. Yay!

However, it's going to rely on #6. mapStateToProps calls inline functions and so can't have selectors mapped out nicely the way useSelector does.

Here's a bit of somewhat-working code for just mapStateToProps:

import * as React from "react";

export const mockConnect = (mockSelector) => {
  const mockConnect = (mapStateToProps) => {
    return function createMockConnectedComponent<Props>(Component: React.ComponentType<Props>) {
      return function MockConnectedComponent(props: Props) {
        const stateProps = Object.entries(mapStateToProps?.() ?? {}).reduce(
          (accumulated, [key, selector]) => {
            accumulated[key] = mockSelector(selector);
            return accumulated;
          },
          {},
        );

        return <Component {...stateProps} {...props} />;
      };
    };
  };

  return mockConnect;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature New enhancement or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant