Skip to content

Commit be1dda3

Browse files
gustavovniciusSimenB
authored andcommitted
fix(pretty-format): Render custom displayName of memoized components (#8546)
1 parent 9ac8ca7 commit be1dda3

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- `[expect]` Highlight substring differences when matcher fails, part 1 ([#8448](https://github.com/facebook/jest/pull/8448))
66
- `[jest-cli]` Improve chai support (with detailed output, to match jest exceptions) ([#8454](https://github.com/facebook/jest/pull/8454))
77
- `[*]` Manage the global timeout with `--testTimeout` command line argument. ([#8456](https://github.com/facebook/jest/pull/8456))
8+
- `[pretty-format]` Render custom displayName of memoized components
89

910
### Fixes
1011

packages/pretty-format/src/__tests__/react.test.tsx

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -722,14 +722,41 @@ test('supports forwardRef with a child', () => {
722722
).toEqual('<ForwardRef(Cat)>\n mouse\n</ForwardRef(Cat)>');
723723
});
724724

725-
test('supports memo with a child', () => {
726-
function Dog(props: any) {
727-
return React.createElement('div', props, props.children);
728-
}
725+
describe('React.memo', () => {
726+
describe('without displayName', () => {
727+
test('renders the component name', () => {
728+
function Dog(props: any) {
729+
return React.createElement('div', props, props.children);
730+
}
731+
732+
expect(
733+
formatElement(React.createElement(React.memo(Dog), null, 'cat')),
734+
).toEqual('<Memo(Dog)>\n cat\n</Memo(Dog)>');
735+
});
736+
});
729737

730-
expect(
731-
formatElement(React.createElement(React.memo(Dog), null, 'cat')),
732-
).toEqual('<Memo(Dog)>\n cat\n</Memo(Dog)>');
738+
describe('with displayName', () => {
739+
test('renders the displayName of component before memoizing', () => {
740+
const Foo = () => React.createElement('div');
741+
Foo.displayName = 'DisplayNameBeforeMemoizing(Foo)';
742+
const MemoFoo = React.memo(Foo);
743+
744+
expect(formatElement(React.createElement(MemoFoo, null, 'cat'))).toEqual(
745+
'<Memo(DisplayNameBeforeMemoizing(Foo))>\n cat\n</Memo(DisplayNameBeforeMemoizing(Foo))>',
746+
);
747+
});
748+
749+
test('renders the displayName of memoized component', () => {
750+
const Foo = () => React.createElement('div');
751+
Foo.displayName = 'DisplayNameThatWillBeIgnored(Foo)';
752+
const MemoFoo = React.memo(Foo);
753+
MemoFoo.displayName = 'DisplayNameForMemoized(Foo)';
754+
755+
expect(formatElement(React.createElement(MemoFoo, null, 'cat'))).toEqual(
756+
'<Memo(DisplayNameForMemoized(Foo))>\n cat\n</Memo(DisplayNameForMemoized(Foo))>',
757+
);
758+
});
759+
});
733760
});
734761

735762
test('supports context Provider with a child', () => {

packages/pretty-format/src/plugins/ReactElement.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ const getType = (element: any) => {
6161
}
6262

6363
if (ReactIs.isMemo(type)) {
64-
const functionName = type.type.displayName || type.type.name || '';
64+
const functionName =
65+
type.displayName || type.type.displayName || type.type.name || '';
6566

6667
return functionName !== '' ? 'Memo(' + functionName + ')' : 'Memo';
6768
}

0 commit comments

Comments
 (0)