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(pretty-format): Render custom displayName of memoized components #8546

Merged
merged 2 commits into from
Jun 11, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- `[expect]` Highlight substring differences when matcher fails, part 1 ([#8448](https://github.com/facebook/jest/pull/8448))
- `[jest-cli]` Improve chai support (with detailed output, to match jest exceptions) ([#8454](https://github.com/facebook/jest/pull/8454))
- `[*]` Manage the global timeout with `--testTimeout` command line argument. ([#8456](https://github.com/facebook/jest/pull/8456))
- `[pretty-format]` Render custom displayName of memoized components

### Fixes

Expand Down
41 changes: 34 additions & 7 deletions packages/pretty-format/src/__tests__/react.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,41 @@ test('supports forwardRef with a child', () => {
).toEqual('<ForwardRef(Cat)>\n mouse\n</ForwardRef(Cat)>');
});

test('supports memo with a child', () => {
function Dog(props: any) {
return React.createElement('div', props, props.children);
}
describe('React.memo', () => {
describe('without displayName', () => {
test('renders the component name', () => {
function Dog(props: any) {
return React.createElement('div', props, props.children);
}

expect(
formatElement(React.createElement(React.memo(Dog), null, 'cat')),
).toEqual('<Memo(Dog)>\n cat\n</Memo(Dog)>');
});
});

expect(
formatElement(React.createElement(React.memo(Dog), null, 'cat')),
).toEqual('<Memo(Dog)>\n cat\n</Memo(Dog)>');
describe('with displayName', () => {
test('renders the displayName of component before memoizing', () => {
const Foo = () => React.createElement('div');
Foo.displayName = 'DisplayNameBeforeMemoizing(Foo)';
const MemoFoo = React.memo(Foo);

expect(formatElement(React.createElement(MemoFoo, null, 'cat'))).toEqual(
'<Memo(DisplayNameBeforeMemoizing(Foo))>\n cat\n</Memo(DisplayNameBeforeMemoizing(Foo))>',
);
});

test('renders the displayName of memoized component', () => {
const Foo = () => React.createElement('div');
Foo.displayName = 'DisplayNameThatWillBeIgnored(Foo)';
const MemoFoo = React.memo(Foo);
MemoFoo.displayName = 'DisplayNameForMemoized(Foo)';

expect(formatElement(React.createElement(MemoFoo, null, 'cat'))).toEqual(
'<Memo(DisplayNameForMemoized(Foo))>\n cat\n</Memo(DisplayNameForMemoized(Foo))>',
);
});
});
});

test('supports context Provider with a child', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/pretty-format/src/plugins/ReactElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ const getType = (element: any) => {
}

if (ReactIs.isMemo(type)) {
const functionName = type.type.displayName || type.type.name || '';
const functionName =
type.displayName || type.type.displayName || type.type.name || '';

return functionName !== '' ? 'Memo(' + functionName + ')' : 'Memo';
}
Expand Down