Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: throw when duplicate screens are defined
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Aug 6, 2019
1 parent 71f4ef1 commit adc2fe4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/core/src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')"
);
});
6 changes: 6 additions & 0 deletions packages/core/src/useNavigationBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down

0 comments on commit adc2fe4

Please sign in to comment.