fix: throw when duplicate screens are defined

This commit is contained in:
satyajit.happy
2019-08-06 18:09:24 +02:00
parent 71f4ef1a92
commit adc2fe4ef3
2 changed files with 27 additions and 0 deletions

View File

@@ -617,3 +617,24 @@ it("doesn't throw when direct children is Screen or empty element", () => {
</NavigationContainer>
);
});
it('throws when multiple screens with same name are defined', () => {
const TestNavigator = (props: any) => {
useNavigationBuilder(MockRouter, props);
return null;
};
const element = (
<NavigationContainer>
<TestNavigator>
<Screen name="foo" component={jest.fn()} />
<Screen name="bar" component={jest.fn()} />
<Screen name="foo" component={jest.fn()} />
</TestNavigator>
</NavigationContainer>
);
expect(() => render(element).update(element)).toThrowError(
"A navigator cannot contain multiple 'Screen' components with the same name (found duplicate screen named 'foo')"
);
});

View File

@@ -81,6 +81,12 @@ export default function useNavigationBuilder<
const screens = getRouteConfigsFromChildren<ScreenOptions>(children).reduce(
(acc, curr) => {
if (curr.name in acc) {
throw new Error(
`A navigator cannot contain multiple 'Screen' components with the same name (found duplicate screen named '${curr.name}')`
);
}
acc[curr.name] = curr;
return acc;
},