This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
generated from satya164/typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 41
feat: remove necessity of wrapping navigators into container #21
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,18 @@ import { render } from 'react-native-testing-library'; | |
import NavigationContainer, { | ||
NavigationStateContext, | ||
} from '../NavigationContainer'; | ||
import useNavigationBuilder from "../useNavigationBuilder"; | ||
import MockRouter from "./__fixtures__/MockRouter"; | ||
import {createNavigator} from "../index"; | ||
|
||
it('throws when getState is accessed without a container', () => { | ||
it('throws when setState is accessed without a container', () => { | ||
expect.assertions(1); | ||
|
||
const Test = () => { | ||
const { getState } = React.useContext(NavigationStateContext); | ||
const { setState } = React.useContext(NavigationStateContext); | ||
|
||
// eslint-disable-next-line babel/no-unused-expressions | ||
getState; | ||
setState; | ||
|
||
return null; | ||
}; | ||
|
@@ -23,14 +26,14 @@ it('throws when getState is accessed without a container', () => { | |
); | ||
}); | ||
|
||
it('throws when setState is accessed without a container', () => { | ||
it('throws when performTransaction is accessed without a container', () => { | ||
expect.assertions(1); | ||
|
||
const Test = () => { | ||
const { setState } = React.useContext(NavigationStateContext); | ||
const { performTransaction } = React.useContext(NavigationStateContext); | ||
|
||
// eslint-disable-next-line babel/no-unused-expressions | ||
setState; | ||
performTransaction; | ||
|
||
return null; | ||
}; | ||
|
@@ -42,8 +45,12 @@ it('throws when setState is accessed without a container', () => { | |
); | ||
}); | ||
|
||
it('throws when performTransaction is accessed without a container', () => { | ||
expect.assertions(1); | ||
it('throws when performTransaction is accessed without a container but inside navigator', () => { | ||
const TestNavigator = createNavigator((props: any) => { | ||
const { state, descriptors } = useNavigationBuilder(MockRouter, props); | ||
|
||
return descriptors[state.routes[state.index].key].render(); | ||
})(); | ||
|
||
const Test = () => { | ||
const { performTransaction } = React.useContext(NavigationStateContext); | ||
|
@@ -54,11 +61,11 @@ it('throws when performTransaction is accessed without a container', () => { | |
return null; | ||
}; | ||
|
||
const element = <Test />; | ||
|
||
expect(() => render(element).update(element)).toThrowError( | ||
"We couldn't find a navigation context. Have you wrapped your app with 'NavigationContainer'?" | ||
); | ||
const element = ( | ||
<TestNavigator.Navigator> | ||
<TestNavigator.Screen name="name" component={Test} /> | ||
</TestNavigator.Navigator>); | ||
render(element).update(element); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: new line before xd |
||
}); | ||
|
||
it('throws when setState is called outside performTransaction', () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import * as React from 'react'; | ||
import { ParamListBase, RouteConfig, TypedNavigator } from './types'; | ||
import Screen from './Screen'; | ||
import { NavigationStateContext } from './NavigationContainer'; | ||
import { NavigationContainer } from './index'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move the import to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: unrelated, but can you move the types import to bottom xd |
||
|
||
export default function createNavigator<N extends React.ComponentType<any>>( | ||
RawNavigator: N | ||
|
@@ -10,7 +12,21 @@ export default function createNavigator<N extends React.ComponentType<any>>( | |
typeof RawNavigator | ||
> { | ||
return { | ||
Navigator: RawNavigator, | ||
Navigator: function Navigator(props) { | ||
const { getState } = React.useContext(NavigationStateContext); | ||
|
||
// @ts-ignore | ||
const Navigator = <RawNavigator {...props} /> | ||
if (getState() === null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: new line before xd |
||
return ( | ||
<NavigationContainer> | ||
{Navigator} | ||
</NavigationContainer> | ||
); | ||
} | ||
|
||
return Navigator; | ||
}, | ||
Screen: Screen as React.ComponentType< | ||
RouteConfig<ParamList, keyof ParamList> | ||
>, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't throw?