-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix pretty-format to respect displayName on forwardRef. (#9422)
Unlike React DevTools, Jest ignores a custom displayName set on forwardRef components. We should align serialization with DevTools, so change behavior.
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import setPrettyPrint from './setPrettyPrint'; | ||
import prettyFormat from '..'; | ||
|
||
const {ReactElement} = prettyFormat.plugins; | ||
|
||
setPrettyPrint([ReactElement]); | ||
|
||
describe('ReactElement Plugin', () => { | ||
let forwardRefComponent: { | ||
(_props: any, _ref: any): any; | ||
displayName?: string; | ||
}; | ||
|
||
let forwardRefExample: ReturnType<typeof React.forwardRef>; | ||
|
||
beforeEach(() => { | ||
forwardRefComponent = (_props, _ref) => null; | ||
|
||
forwardRefExample = React.forwardRef(forwardRefComponent); | ||
|
||
forwardRefExample.displayName = undefined; | ||
}); | ||
|
||
test('serializes forwardRef without displayName', () => { | ||
forwardRefExample = React.forwardRef((_props, _ref) => null); | ||
expect(React.createElement(forwardRefExample)).toPrettyPrintTo( | ||
'<ForwardRef />', | ||
); | ||
}); | ||
|
||
test('serializes forwardRef with displayName', () => { | ||
forwardRefExample.displayName = 'Display'; | ||
expect(React.createElement(forwardRefExample)).toPrettyPrintTo( | ||
'<Display />', | ||
); | ||
}); | ||
|
||
test('serializes forwardRef component with displayName', () => { | ||
forwardRefComponent.displayName = 'Display'; | ||
expect(React.createElement(forwardRefExample)).toPrettyPrintTo( | ||
'<ForwardRef(Display) />', | ||
); | ||
}); | ||
}); |
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