diff --git a/src/nav/__tests__/navReducer-test.js b/src/nav/__tests__/navReducer-test.js index 2b468f38cdb..8b3215b0538 100644 --- a/src/nav/__tests__/navReducer-test.js +++ b/src/nav/__tests__/navReducer-test.js @@ -2,10 +2,17 @@ import deepFreeze from 'deep-freeze'; import { INITIAL_FETCH_COMPLETE } from '../../actionConstants'; import navReducer, { getStateForRoute } from '../navReducer'; +import NavigationService from '../NavigationService'; describe('navReducer', () => { describe('INITIAL_FETCH_COMPLETE', () => { test('do not mutate navigation state if already at the same route', () => { + NavigationService.getState = jest.fn().mockReturnValue( + deepFreeze({ + index: 0, + routes: [{ routeName: 'main' }], + }), + ); const prevState = getStateForRoute('main'); const action = deepFreeze({ diff --git a/src/nav/navReducer.js b/src/nav/navReducer.js index 5375e10bcc4..4db70b7a7c3 100644 --- a/src/nav/navReducer.js +++ b/src/nav/navReducer.js @@ -2,6 +2,7 @@ import type { NavigationAction } from 'react-navigation'; import type { NavigationState, Action } from '../types'; +import { getNavigationRoutes } from './navSelectors'; import AppNavigator from './AppNavigator'; import { INITIAL_FETCH_COMPLETE } from '../actionConstants'; @@ -29,7 +30,16 @@ export const initialState = getStateForRoute('loading'); export default (state: NavigationState = initialState, action: Action): NavigationState => { switch (action.type) { case INITIAL_FETCH_COMPLETE: - return state.routes[0].routeName === 'main' ? state : getStateForRoute('main'); + // If we're anywhere in the normal UI of the app, then remain + // where we are. Only reset the nav state if we're elsewhere, + // and in that case, go to the main screen. + // + // TODO: "elsewhere" is probably just a way of saying "on the + // loading screen", but we're not sure. We could adjust the + // conditional accordingly, if we found out we're not depending on + // the more general condition; see + // https://github.com/zulip/zulip-mobile/pull/4274#discussion_r505941875 + return getNavigationRoutes()[0].routeName === 'main' ? state : getStateForRoute('main'); default: { // The `react-navigation` libdef says this only takes a NavigationAction, diff --git a/src/nav/navSelectors.js b/src/nav/navSelectors.js index 44ef8084817..dce36f6bc65 100644 --- a/src/nav/navSelectors.js +++ b/src/nav/navSelectors.js @@ -5,7 +5,7 @@ import NavigationService from './NavigationService'; export const getNavState = (): NavigationState => NavigationService.getState(); -const getNavigationRoutes = () => getNavState().routes; +export const getNavigationRoutes = () => getNavState().routes; const getNavigationIndex = () => getNavState().index;