@samitouri / QOS-React / commits / 053347e6bc

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.

Henry Q. Dineen committed Feb 28, 2020 at 12:55 UTC 053347e6bc69de79dfa46bb422868b023851c315
2 files changed +12 -1
packages/react-test-renderer/src/ReactTestRenderer.js
+2 -1
@@ -42,6 +42,7 @@ import {
42 ScopeComponent,
43 } from 'shared/ReactWorkTags';
44 import invariant from 'shared/invariant';
45 +import getComponentName from 'shared/getComponentName';
46 import ReactVersion from 'shared/ReactVersion';
47
48 import {getPublicInstance} from './ReactTestHostConfig';
@@ -346,7 +347,7 @@ class ReactTestInstance {
347 findByType(type: any): ReactTestInstance {
348 return expectOne(
349 this.findAllByType(type, {deep: false}),
349 - `with node type: "${type.displayName || type.name}"`,
350 + `with node type: "${getComponentName(type) || 'Unknown'}"`,
351 );
352 }
353
packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.internal.js
+10
@@ -1022,4 +1022,14 @@ describe('ReactTestRenderer', () => {
1022 expect(Scheduler).toFlushWithoutYielding();
1023 ReactTestRenderer.create(<App />);
1024 });
1025 +
1026 + it('calling findByType() with an invalid component will fall back to "Unknown" for component name', () => {
1027 + const App = () => null;
1028 + const renderer = ReactTestRenderer.create(<App />);
1029 + const NonComponent = {};
1030 +
1031 + expect(() => {
1032 + renderer.root.findByType(NonComponent);
1033 + }).toThrowError(`No instances found with node type: "Unknown"`);
1034 + });
1035 });