Skip to content

Commit f4bfec2

Browse files
author
Brian Vaughn
committed
Added a test
1 parent b573838 commit f4bfec2

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/ErrorBoundary.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ class ErrorBoundary extends Component<Props, State> {
6363
}
6464
}
6565

66-
67-
function getDisplayName(WrappedComponent) {
68-
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
69-
}
70-
7166
export const withErrorBoundary = (
7267
Component: ComponentType<any>,
7368
FallbackComponent: ComponentType<any>,
@@ -78,7 +73,13 @@ export const withErrorBoundary = (
7873
<Component {...props} />
7974
</ErrorBoundary>
8075
);
81-
Wrapped.displayName = `WithErrorBoundary(${getDisplayName(Component)})`;
76+
77+
// Format for display in DevTools
78+
const name = Component.displayName || Component.name;
79+
Wrapped.displayName = name
80+
? `WithErrorBoundary(${name})`
81+
: 'WithErrorBoundary';
82+
8283
return Wrapped;
8384
};
8485

src/__tests__/ErrorBoundary.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,23 @@ describe('ErrorBoundary', () => {
260260
),
261261
).toBe(true);
262262
});
263+
264+
it('sets the correct displayName for wrapped components', () => {
265+
function NormalComponent() {
266+
return null;
267+
}
268+
expect(withErrorBoundary(NormalComponent).displayName).toBe(
269+
'WithErrorBoundary(NormalComponent)',
270+
);
271+
272+
function ComponentWithDisplayNameOverride() {
273+
return null;
274+
}
275+
ComponentWithDisplayNameOverride.displayName = 'Override';
276+
277+
expect(
278+
withErrorBoundary(ComponentWithDisplayNameOverride).displayName,
279+
).toBe('WithErrorBoundary(Override)');
280+
expect(withErrorBoundary(() => null).displayName).toBe('WithErrorBoundary');
281+
});
263282
});

0 commit comments

Comments
 (0)