mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
react-test-renderer: improve findByType() error message (#17439)
* improve findByType error message * fix flow typing * Adding a test for the "Unknown" branch when `getComponentName()` returns a falsy value. The error message in this case not the most descriptive but seems consistent with the `getComponentName(type) || 'Unknown'` pattern seen in multiple places in this code base.
This commit is contained in:
+2
-1
@@ -42,6 +42,7 @@ import {
|
||||
ScopeComponent,
|
||||
} from 'shared/ReactWorkTags';
|
||||
import invariant from 'shared/invariant';
|
||||
import getComponentName from 'shared/getComponentName';
|
||||
import ReactVersion from 'shared/ReactVersion';
|
||||
|
||||
import {getPublicInstance} from './ReactTestHostConfig';
|
||||
@@ -346,7 +347,7 @@ class ReactTestInstance {
|
||||
findByType(type: any): ReactTestInstance {
|
||||
return expectOne(
|
||||
this.findAllByType(type, {deep: false}),
|
||||
`with node type: "${type.displayName || type.name}"`,
|
||||
`with node type: "${getComponentName(type) || 'Unknown'}"`,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1022,4 +1022,14 @@ describe('ReactTestRenderer', () => {
|
||||
expect(Scheduler).toFlushWithoutYielding();
|
||||
ReactTestRenderer.create(<App />);
|
||||
});
|
||||
|
||||
it('calling findByType() with an invalid component will fall back to "Unknown" for component name', () => {
|
||||
const App = () => null;
|
||||
const renderer = ReactTestRenderer.create(<App />);
|
||||
const NonComponent = {};
|
||||
|
||||
expect(() => {
|
||||
renderer.root.findByType(NonComponent);
|
||||
}).toThrowError(`No instances found with node type: "Unknown"`);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user