Skip to content

Commit 8f764d8

Browse files
committed
fix: improve the error message for incorrect screen configuration
1 parent f8e998b commit 8f764d8

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

packages/core/src/__tests__/index.test.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,51 @@ it('throws when Screen is not the direct children', () => {
14621462
);
14631463
});
14641464

1465+
it('throws when undefined component is a direct children', () => {
1466+
const TestNavigator = (props: any) => {
1467+
useNavigationBuilder(MockRouter, props);
1468+
return null;
1469+
};
1470+
1471+
const Undefined = undefined;
1472+
1473+
const spy = jest.spyOn(console, 'error').mockImplementation();
1474+
const element = (
1475+
<BaseNavigationContainer>
1476+
<TestNavigator>
1477+
{/* @ts-ignore */}
1478+
<Undefined name="foo" component={jest.fn()} />
1479+
</TestNavigator>
1480+
</BaseNavigationContainer>
1481+
);
1482+
1483+
spy.mockRestore();
1484+
1485+
expect(() => render(element).update(element)).toThrowError(
1486+
"A navigator can only contain 'Screen' components as its direct children (found 'undefined' for the screen 'foo')"
1487+
);
1488+
});
1489+
1490+
it('throws when a tag is a direct children', () => {
1491+
const TestNavigator = (props: any) => {
1492+
useNavigationBuilder(MockRouter, props);
1493+
return null;
1494+
};
1495+
1496+
const element = (
1497+
<BaseNavigationContainer>
1498+
<TestNavigator>
1499+
{/* @ts-ignore */}
1500+
<screen name="foo" component={jest.fn()} />
1501+
</TestNavigator>
1502+
</BaseNavigationContainer>
1503+
);
1504+
1505+
expect(() => render(element).update(element)).toThrowError(
1506+
"A navigator can only contain 'Screen' components as its direct children (found 'screen' for the screen 'foo')"
1507+
);
1508+
});
1509+
14651510
it('throws when a React Element is not the direct children', () => {
14661511
const TestNavigator = (props: any) => {
14671512
useNavigationBuilder(MockRouter, props);

packages/core/src/useNavigationBuilder.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,17 @@ const getRouteConfigsFromChildren = <
9090
}
9191

9292
throw new Error(
93-
`A navigator can only contain 'Screen' components as its direct children (found '${
94-
// @ts-expect-error: child can be any type and we're accessing it safely, but TS doesn't understand it
95-
child.type?.name ? child.type.name : String(child)
96-
}'). To render this component in the navigator, pass it in the 'component' prop to 'Screen'.`
93+
`A navigator can only contain 'Screen' components as its direct children (found ${
94+
React.isValidElement(child)
95+
? `'${
96+
typeof child.type === 'string' ? child.type : child.type?.name
97+
}'${
98+
child.props?.name ? ` for the screen '${child.props.name}'` : ''
99+
}`
100+
: typeof child === 'object'
101+
? JSON.stringify(child)
102+
: `'${String(child)}'`
103+
}). To render this component in the navigator, pass it in the 'component' prop to 'Screen'.`
97104
);
98105
}, []);
99106

0 commit comments

Comments
 (0)