Skip to content

Support React.memo in pretty-format #7891

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

Merged
merged 2 commits into from
Feb 14, 2019
Merged
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
@@ -4,6 +4,7 @@

- `[expect]`: Improve report when matcher fails, part 7 ([#7866](https://github.com/facebook/jest/pull/7866))
- `[expect]`: Improve report when matcher fails, part 8 ([#7876](https://github.com/facebook/jest/pull/7876))
- `[pretty-format]` Support `React.memo` ([#7891](https://github.com/facebook/jest/pull/7891))

### Fixes

10 changes: 10 additions & 0 deletions packages/pretty-format/src/__tests__/react.test.tsx
Original file line number Diff line number Diff line change
@@ -709,6 +709,16 @@ 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);
}

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

test('supports context Provider with a child', () => {
const {Provider} = React.createContext('test');

7 changes: 7 additions & 0 deletions packages/pretty-format/src/plugins/ReactElement.ts
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ const fragmentSymbol = Symbol.for('react.fragment');
const forwardRefSymbol = Symbol.for('react.forward_ref');
const providerSymbol = Symbol.for('react.provider');
const contextSymbol = Symbol.for('react.context');
const memoSymbol = Symbol.for('react.memo');

// Given element.props.children, or subtree during recursive traversal,
// return flattened array of children.
@@ -60,6 +61,12 @@ const getType = (element: any) => {
? 'ForwardRef(' + functionName + ')'
: 'ForwardRef';
}

if (type.$$typeof === memoSymbol) {
const functionName = type.type.displayName || type.type.name || '';

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