Skip to content

Commit

Permalink
use config flag
Browse files Browse the repository at this point in the history
  • Loading branch information
nico1510 committed Feb 24, 2018
1 parent 19a156e commit f28c962
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
1 change: 1 addition & 0 deletions flow/react-navigation.js
Expand Up @@ -405,6 +405,7 @@ declare module 'react-navigation' {
initialRouteParams?: NavigationParams,
paths?: NavigationPathsConfig,
navigationOptions?: NavigationScreenConfig<*>,
initialRouteKey?: 'initialRouteName' | 'generated',
|};

declare export type NavigationStackViewConfig = {|
Expand Down
7 changes: 6 additions & 1 deletion src/routers/StackRouter.js
Expand Up @@ -92,11 +92,16 @@ export default (routeConfigs, stackConfig = {}) => {
...(action.params || {}),
...(initialRouteParams || {}),
};
const { initialRouteKey } = stackConfig;
route = {
...route,
...(params ? { params } : {}),
routeName: initialRouteName,
key: action.key || generateKey(),
key:
action.key ||
(initialRouteKey && initialRouteKey === 'initialRouteName'
? initialRouteName
: generateKey()),
};
return {
key: 'StackRouterRoot',
Expand Down
10 changes: 6 additions & 4 deletions src/routers/__tests__/Routers-test.js
Expand Up @@ -110,8 +110,8 @@ test('Handles no-op actions with tabs within stack router', () => {
type: NavigationActions.NAVIGATE,
routeName: 'Qux',
});
expect(state1.routes[0].key).toEqual('Foo');
expect(state2.routes[0].key).toEqual('Foo');
expect(state1.routes[0].key).toEqual('id-0');
expect(state2.routes[0].key).toEqual('id-1');
state1.routes[0].key = state2.routes[0].key;
expect(state1).toEqual(state2);
const state3 = TestRouter.getStateForAction(
Expand Down Expand Up @@ -139,7 +139,7 @@ test('Handles deep action', () => {
key: 'StackRouterRoot',
routes: [
{
key: 'Bar',
key: 'id-0',
routeName: 'Bar',
},
],
Expand Down Expand Up @@ -179,7 +179,9 @@ test('Supports lazily-evaluated getScreen', () => {
immediate: true,
routeName: 'Qux',
});
expect(state1.routes[0].key).toEqual('Foo');
expect(state1.routes[0].key).toEqual('id-0');
expect(state2.routes[0].key).toEqual('id-1');
state1.routes[0].key = state2.routes[0].key;
expect(state1).toEqual(state2);
const state3 = TestRouter.getStateForAction(
{
Expand Down
33 changes: 25 additions & 8 deletions src/routers/__tests__/StackRouter-test.js
Expand Up @@ -355,7 +355,7 @@ describe('StackRouter', () => {
index: 0,
isTransitioning: false,
key: 'StackRouterRoot',
routes: [{ key: 'foo', routeName: 'foo' }],
routes: [{ key: 'id-0', routeName: 'foo' }],
});
const pushedState = TestRouter.getStateForAction(
NavigationActions.navigate({ routeName: 'qux' }),
Expand Down Expand Up @@ -547,6 +547,23 @@ describe('StackRouter', () => {
expect(state2.routes[1].routes[1].routes[1].routeName).toEqual('Corge');
});

test('Navigate to initial screen is possible', () => {
const TestRouter = StackRouter(
{
foo: { screen: () => <div /> },
bar: { screen: () => <div /> },
},
{ initialRouteKey: 'initialRouteName' }
);
const initState = TestRouter.getStateForAction(NavigationActions.init());
const pushedState = TestRouter.getStateForAction(
NavigationActions.navigate({ routeName: 'foo', key: 'foo' }),
initState
);
expect(pushedState.index).toEqual(0);
expect(pushedState.routes[0].routeName).toEqual('foo');
});

test('Navigate with key is idempotent', () => {
const TestRouter = StackRouter({
foo: { screen: () => <div /> },
Expand Down Expand Up @@ -636,7 +653,7 @@ describe('StackRouter', () => {
key: 'StackRouterRoot',
routes: [
{
key: 'Foo',
key: 'id-0',
routeName: 'Foo',
},
],
Expand Down Expand Up @@ -664,7 +681,7 @@ describe('StackRouter', () => {
key: 'StackRouterRoot',
routes: [
{
key: 'Foo',
key: 'id-0',
routeName: 'Foo',
},
],
Expand Down Expand Up @@ -761,7 +778,7 @@ describe('StackRouter', () => {
key: 'StackRouterRoot',
routes: [
{
key: 'Foo',
key: 'id-0',
routeName: 'Foo',
},
],
Expand Down Expand Up @@ -789,7 +806,7 @@ describe('StackRouter', () => {
key: 'StackRouterRoot',
routes: [
{
key: 'Foo',
key: 'id-0',
routeName: 'Foo',
},
],
Expand Down Expand Up @@ -863,7 +880,7 @@ describe('StackRouter', () => {
key: 'StackRouterRoot',
routes: [
{
key: 'Bar',
key: 'id-0',
routeName: 'Bar',
},
],
Expand Down Expand Up @@ -972,14 +989,14 @@ describe('StackRouter', () => {
{
type: NavigationActions.SET_PARAMS,
params: { name: 'foobar' },
key: 'Quux',
key: 'id-0',
},
state
);
expect(state2 && state2.index).toEqual(0);
expect(state2 && state2.routes[0].routes[0].routes).toEqual([
{
key: 'Quux',
key: 'id-0',
routeName: 'Quux',
params: { name: 'foobar' },
},
Expand Down

0 comments on commit f28c962

Please sign in to comment.