Skip to content

Commit 7e0c93b

Browse files
committed
fix: displayName of components wrapped with select
1 parent 9f95f11 commit 7e0c93b

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

.changeset/kind-cups-change.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ima/plugin-select": patch
3+
---
4+
5+
Fix displayName value for components wrapped with `select` HOC. This should resolve the issue with `Component.displayName` being `withContext(undefined)`.

packages/plugin-select/src/select/__tests__/SelectSpec.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getContextValue, renderWithContext } from '@ima/testing-library';
2-
import { PureComponent, createRef } from 'react';
2+
import { PureComponent, createRef, memo, forwardRef } from 'react';
33

44
import forwardedSelect, {
55
createStateSelector,
@@ -159,6 +159,37 @@ describe('plugin-select:', () => {
159159
expect(container.firstChild).toMatchSnapshot();
160160
});
161161

162+
it('should set displayName for class component', async () => {
163+
const EnhancedComponent = select(...selectorMethods)(Component);
164+
165+
expect(EnhancedComponent.displayName).toBe('withContext(Component)');
166+
});
167+
168+
it('should set displayName for functional component', async () => {
169+
const FunctionalComponent = () => {
170+
return <div />;
171+
};
172+
const EnhancedComponent = select(...selectorMethods)(FunctionalComponent);
173+
174+
expect(EnhancedComponent.displayName).toBe(
175+
'withContext(FunctionalComponent)'
176+
);
177+
});
178+
179+
it('should set displayName for memoized component', async () => {
180+
const MemoComponent = memo(Component);
181+
const EnhancedComponent = select(...selectorMethods)(MemoComponent);
182+
183+
expect(EnhancedComponent.displayName).toBe('withContext(Component)');
184+
});
185+
186+
it('should set displayName for forwarded component', async () => {
187+
const ForwardedComponent = forwardRef(Component);
188+
const EnhancedComponent = select(...selectorMethods)(ForwardedComponent);
189+
190+
expect(EnhancedComponent.displayName).toBe('withContext(Component)');
191+
});
192+
162193
it('should render component with extraProps modifies by ownProps', async () => {
163194
let EnhancedComponent = select(
164195
...selectorMethods,

packages/plugin-select/src/select/select.jsx

+17-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function select(...selectors) {
5151
return <Component {...restProps} {...state} ref={forwardedRef} />;
5252
};
5353

54-
const componentName = Component.displayName || Component.name;
54+
const componentName = getReactComponentName(Component);
5555
WithContext.displayName = `withContext(${componentName})`;
5656
hoistStaticMethod(WithContext, Component);
5757

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

123-
const name = Component.displayName || Component.name;
123+
const name = getReactComponentName(Component);
124124
forwardRef.displayName = `select(${name})`;
125125

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

180180
return createSelector(derivedState, passStateOnChange);
181181
}
182+
183+
/**
184+
* Finds display name from the provided component.
185+
* @param {Function|Object} Component
186+
* @returns {string}
187+
*/
188+
function getReactComponentName(Component) {
189+
return Component.displayName ||
190+
Component.name ||
191+
Component.type?.displayName || // React.memo
192+
Component.type?.name ||
193+
Component.render?.displayName || // React.forwardRef
194+
Component.render?.name ||
195+
'Component';
196+
}

0 commit comments

Comments
 (0)