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: make NAVIGATE and JUMP_TO to support key and name of the route #16
Merged
Merged
Changes from all commits
Commits
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
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,10 +1,10 @@ | ||
import { PartialState } from './types'; | ||
import { PartialState, TargetRoute } from './types'; | ||
|
||
export type Action = | ||
| { type: 'GO_BACK' } | ||
| { | ||
type: 'NAVIGATE'; | ||
payload: { name: string; params?: object }; | ||
payload: { name?: string; key?: string; params?: object }; | ||
} | ||
| { | ||
type: 'REPLACE'; | ||
|
@@ -19,8 +19,20 @@ export function goBack(): Action { | |
return { type: 'GO_BACK' }; | ||
} | ||
|
||
export function navigate(name: string, params?: object): Action { | ||
return { type: 'NAVIGATE', payload: { name, params } }; | ||
export function navigate(target: TargetRoute<string>, params?: object): Action { | ||
if ( | ||
(target.hasOwnProperty('key') && target.hasOwnProperty('name')) || | ||
(!target.hasOwnProperty('key') && !target.hasOwnProperty('name')) | ||
) { | ||
throw new Error( | ||
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. Maybe instead of throwing in the action creator, we can throw in the router. Then it'll also prevent someone from calling 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. I don't think so. We don't want anyone to dispatch |
||
'While calling navigate you need to specify either name or key' | ||
); | ||
} | ||
if (typeof target === 'string') { | ||
return { type: 'NAVIGATE', payload: { name: target, params } }; | ||
} else { | ||
return { type: 'NAVIGATE', payload: { ...target, params } }; | ||
} | ||
} | ||
|
||
export function replace(name: string, params?: object): Action { | ||
|
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as React from 'react'; | ||
import { render } from 'react-native-testing-library'; | ||
import Screen from '../Screen'; | ||
import NavigationContainer from '../NavigationContainer'; | ||
import useNavigationBuilder from '../useNavigationBuilder'; | ||
import MockRouter from './__fixtures__/MockRouter'; | ||
|
||
beforeEach(() => (MockRouter.key = 0)); | ||
|
||
it('throws if NAVIGATE dispatched with both key and name', () => { | ||
const TestNavigator = (props: any) => { | ||
const { state, descriptors } = useNavigationBuilder(MockRouter, props); | ||
|
||
return descriptors[state.routes[state.index].key].render(); | ||
}; | ||
|
||
const FooScreen = (props: any) => { | ||
React.useEffect(() => { | ||
props.navigation.navigate({ key: '1', name: '2' }); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return null; | ||
}; | ||
|
||
const onStateChange = jest.fn(); | ||
|
||
const element = ( | ||
<NavigationContainer onStateChange={onStateChange}> | ||
<TestNavigator initialRouteName="foo"> | ||
<Screen | ||
name="foo" | ||
component={FooScreen} | ||
initialParams={{ count: 10 }} | ||
/> | ||
</TestNavigator> | ||
</NavigationContainer> | ||
); | ||
|
||
expect(() => render(element).update(element)).toThrowError( | ||
'While calling navigate you need to specify either name or key' | ||
); | ||
}); | ||
|
||
it('throws if NAVIGATE dispatched neither both key nor name', () => { | ||
const TestNavigator = (props: any) => { | ||
const { state, descriptors } = useNavigationBuilder(MockRouter, props); | ||
|
||
return descriptors[state.routes[state.index].key].render(); | ||
}; | ||
|
||
const FooScreen = (props: any) => { | ||
React.useEffect(() => { | ||
props.navigation.navigate({}); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return null; | ||
}; | ||
|
||
const onStateChange = jest.fn(); | ||
|
||
const element = ( | ||
<NavigationContainer onStateChange={onStateChange}> | ||
<TestNavigator initialRouteName="foo"> | ||
<Screen | ||
name="foo" | ||
component={FooScreen} | ||
initialParams={{ count: 10 }} | ||
/> | ||
</TestNavigator> | ||
</NavigationContainer> | ||
); | ||
|
||
expect(() => render(element).update(element)).toThrowError( | ||
'While calling navigate you need to specify either name or key' | ||
); | ||
}); |
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
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.
Maybe we should also check if the
key
exists in the stackThere 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.
we do it line 180