-
Notifications
You must be signed in to change notification settings - Fork 47.9k
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
Support React.memo in ReactShallowRenderer #14816
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
786262d
Support React.memo in ReactShallowRenderer
f06a74e
Allow Rect.memo to prevent re-renders
75dcd59
Support memo(forwardRef())
0dbe1fd
Dont call memo comparison function on initial render
2230fab
Fix test
gaearon 137f390
Small tweaks
gaearon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1454,4 +1454,115 @@ describe('ReactShallowRenderer', () => { | |
shallowRenderer.render(<Foo foo="bar" />); | ||
expect(logs).toEqual([undefined]); | ||
}); | ||
|
||
it('should handle memo', () => { | ||
function Foo() { | ||
return <div>foo</div>; | ||
} | ||
const MemoFoo = React.memo(Foo); | ||
const shallowRenderer = createRenderer(); | ||
shallowRenderer.render(<MemoFoo />); | ||
}); | ||
|
||
it('should enable React.memo to prevent a re-render', () => { | ||
const logs = []; | ||
const Foo = React.memo(({count}) => { | ||
logs.push(`Foo: ${count}`); | ||
return <div>{count}</div>; | ||
}); | ||
const Bar = React.memo(({count}) => { | ||
logs.push(`Bar: ${count}`); | ||
return <div>{count}</div>; | ||
}); | ||
const shallowRenderer = createRenderer(); | ||
shallowRenderer.render(<Foo count={1} />); | ||
expect(logs).toEqual(['Foo: 1']); | ||
logs.length = 0; | ||
// Rendering the same element with the same props should be prevented | ||
shallowRenderer.render(<Foo count={1} />); | ||
expect(logs).toEqual([]); | ||
// A different element with the same props should cause a re-render | ||
shallowRenderer.render(<Bar count={1} />); | ||
expect(logs).toEqual(['Bar: 1']); | ||
}); | ||
|
||
it('should respect a custom comparison function with React.memo', () => { | ||
let renderCount = 0; | ||
function areEqual(props, nextProps) { | ||
return props.foo === nextProps.foo; | ||
} | ||
const Foo = React.memo(({foo, bar}) => { | ||
renderCount++; | ||
return ( | ||
<div> | ||
{foo} {bar} | ||
</div> | ||
); | ||
}, areEqual); | ||
|
||
const shallowRenderer = createRenderer(); | ||
shallowRenderer.render(<Foo foo={1} bar={1} />); | ||
expect(renderCount).toBe(1); | ||
// Change a prop that the comparison funciton ignores | ||
shallowRenderer.render(<Foo foo={1} bar={2} />); | ||
expect(renderCount).toBe(1); | ||
shallowRenderer.render(<Foo foo={2} bar={2} />); | ||
expect(renderCount).toBe(2); | ||
}); | ||
|
||
it('should not call the comparison function with React.memo on the initial render', () => { | ||
const areEqual = jest.fn(() => false); | ||
const SomeComponent = React.memo(({foo}) => { | ||
return <div>{foo}</div>; | ||
}, areEqual); | ||
const shallowRenderer = createRenderer(); | ||
shallowRenderer.render(<SomeComponent foo={1} />); | ||
expect(areEqual).not.toHaveBeenCalled(); | ||
expect(shallowRenderer.getRenderOutput()).toEqual(<div>{1}</div>); | ||
}); | ||
|
||
it('should handle memo(forwardRef())', () => { | ||
const testRef = React.createRef(); | ||
const SomeComponent = React.forwardRef((props, ref) => { | ||
expect(ref).toEqual(testRef); | ||
return ( | ||
<div> | ||
<span className="child1" /> | ||
<span className="child2" /> | ||
</div> | ||
); | ||
}); | ||
|
||
const SomeMemoComponent = React.memo(SomeComponent); | ||
|
||
const shallowRenderer = createRenderer(); | ||
const result = shallowRenderer.render(<SomeMemoComponent ref={testRef} />); | ||
|
||
expect(result.type).toBe('div'); | ||
expect(result.props.children).toEqual([ | ||
<span className="child1" />, | ||
<span className="child2" />, | ||
]); | ||
}); | ||
|
||
it('should warn for forwardRef(memo())', () => { | ||
const testRef = React.createRef(); | ||
const SomeMemoComponent = React.memo(({foo}) => { | ||
return <div>{foo}</div>; | ||
}); | ||
const shallowRenderer = createRenderer(); | ||
expect(() => { | ||
expect(() => { | ||
const SomeComponent = React.forwardRef(SomeMemoComponent); | ||
shallowRenderer.render(<SomeComponent ref={testRef} />); | ||
}).toWarnDev( | ||
'Warning: forwardRef requires a render function but received ' + | ||
'a `memo` component. Instead of forwardRef(memo(...)), use ' + | ||
'memo(forwardRef(...))', | ||
{withoutStack: true}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is failing saying |
||
); | ||
}).toThrowError( | ||
'forwardRef requires a render function but was given object.', | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason not to use isValidElementType from react-is here, and asserting the type isn’t string, rather than repeating some of the logic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably because
isValidElementType
was introduced after this version of the shallow renderer. That's a good point though, because looking atisValidElementType
catches more uncommon cases that React technically supports and that fail with the shallow renderer.For example, you can do:
But rendering that with the shallow render will trigger this invariant. The same goes for other elements that have their own type like
React.Suspense
,React.ConcurrentMode
,React.StrictMode
, etc.