Skip to content

Commit

Permalink
Support React.memo in pretty-format (#7891)
Browse files Browse the repository at this point in the history
  • Loading branch information
bensampaio authored and SimenB committed Feb 14, 2019
1 parent 9e31173 commit af1ab24
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions packages/pretty-format/src/__tests__/react.test.tsx
Expand Up @@ -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');

Expand Down
7 changes: 7 additions & 0 deletions packages/pretty-format/src/plugins/ReactElement.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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';
};
Expand Down

0 comments on commit af1ab24

Please sign in to comment.