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

fix: displayName of components wrapped with select #185

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/kind-cups-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ima/plugin-select": patch
---

Fix displayName value for components wrapped with `select` HOC. This should resolve the issue with `Component.displayName` being `withContext(undefined)`.
33 changes: 32 additions & 1 deletion packages/plugin-select/src/select/__tests__/SelectSpec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getContextValue, renderWithContext } from '@ima/testing-library';
import { PureComponent, createRef } from 'react';
import { PureComponent, createRef, memo, forwardRef } from 'react';

import forwardedSelect, {
createStateSelector,
Expand Down Expand Up @@ -159,6 +159,37 @@ describe('plugin-select:', () => {
expect(container.firstChild).toMatchSnapshot();
});

it('should set displayName for class component', async () => {
const EnhancedComponent = select(...selectorMethods)(Component);

expect(EnhancedComponent.displayName).toBe('withContext(Component)');
});

it('should set displayName for functional component', async () => {
const FunctionalComponent = () => {
return <div />;
};
const EnhancedComponent = select(...selectorMethods)(FunctionalComponent);

expect(EnhancedComponent.displayName).toBe(
'withContext(FunctionalComponent)'
);
});

it('should set displayName for memoized component', async () => {
const MemoComponent = memo(Component);
const EnhancedComponent = select(...selectorMethods)(MemoComponent);

expect(EnhancedComponent.displayName).toBe('withContext(Component)');
});

it('should set displayName for forwarded component', async () => {
const ForwardedComponent = forwardRef(Component);
const EnhancedComponent = select(...selectorMethods)(ForwardedComponent);

expect(EnhancedComponent.displayName).toBe('withContext(Component)');
});

it('should render component with extraProps modifies by ownProps', async () => {
let EnhancedComponent = select(
...selectorMethods,
Expand Down
19 changes: 17 additions & 2 deletions packages/plugin-select/src/select/select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function select(...selectors) {
return <Component {...restProps} {...state} ref={forwardedRef} />;
};

const componentName = Component.displayName || Component.name;
const componentName = getReactComponentName(Component);
WithContext.displayName = `withContext(${componentName})`;
hoistStaticMethod(WithContext, Component);

Expand Down Expand Up @@ -120,7 +120,7 @@ export default function forwardedSelect(...selectors) {
return <SelectState {...props} forwardedRef={ref} />;
};

const name = Component.displayName || Component.name;
const name = getReactComponentName(Component);
forwardRef.displayName = `select(${name})`;

return hoistStaticMethod(reactForwardRef(forwardRef), Component);
Expand Down Expand Up @@ -179,3 +179,18 @@ export function createStateSelector(...selectors) {

return createSelector(derivedState, passStateOnChange);
}

/**
* Finds display name from the provided component.
* @param {Function|Object} Component
* @returns {string}
*/
function getReactComponentName(Component) {
return Component.displayName ||
Component.name ||
Component.type?.displayName || // React.memo
Component.type?.name ||
Component.render?.displayName || // React.forwardRef
Component.render?.name ||
'Component';
}
Loading