Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/SidePanel/RHPVariantTest/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SidePanelActions from '@libs/actions/SidePanel';
import isReportTopmostSplitNavigator from '@libs/Navigation/helpers/isReportTopmostSplitNavigator';
import isReportRevealedInTopmostSplitNavigator from '@libs/Navigation/helpers/isReportRevealedInTopmostSplitNavigator';
import Navigation from '@libs/Navigation/Navigation';

import CONST from '@src/CONST';
Expand Down Expand Up @@ -67,7 +67,7 @@ const shouldOpenRHPVariant: ShouldOpenRHPVariant = (variantOverride) => {
const handleRHPVariantNavigation: HandleRHPVariantNavigation = (onboardingPolicyID, variantOverride, navigationOptions) => {
const variant = variantOverride ?? onboardingRHPVariant;
if (variant === CONST.ONBOARDING_RHP_VARIANT.TRACK_EXPENSES_WITH_CONCIERGE) {
const shouldPreserveRevealedReport = isReportTopmostSplitNavigator();
const shouldPreserveRevealedReport = isReportRevealedInTopmostSplitNavigator();
if (!shouldPreserveRevealedReport) {
Navigation.navigate(ROUTES.HOME, navigationOptions);
}
Expand All @@ -78,7 +78,7 @@ const handleRHPVariantNavigation: HandleRHPVariantNavigation = (onboardingPolicy
const isRHPHomePage = variant === CONST.ONBOARDING_RHP_VARIANT.RHP_HOME_PAGE;

if (isRHPHomePage) {
const shouldPreserveRevealedReport = isReportTopmostSplitNavigator();
const shouldPreserveRevealedReport = isReportRevealedInTopmostSplitNavigator();
if (!shouldPreserveRevealedReport) {
Navigation.navigate(ROUTES.HOME, navigationOptions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {getPreservedNavigatorState} from '@libs/Navigation/AppNavigator/createSplitNavigator/usePreserveNavigatorState';

import NAVIGATORS from '@src/NAVIGATORS';
import SCREENS from '@src/SCREENS';

import getTopmostFullScreenRoute from './getTopmostFullScreenRoute';

/**
* Onboarding can remove the live split state before this check, which would send a deep-linked report to Home.
*/
function isReportRevealedInTopmostSplitNavigator(): boolean {
const topmostFullScreenRoute = getTopmostFullScreenRoute();

if (topmostFullScreenRoute?.name !== NAVIGATORS.REPORTS_SPLIT_NAVIGATOR) {
return false;
}

const innerRoutes: ReadonlyArray<{name: string; params?: unknown}> | undefined =
topmostFullScreenRoute.state?.routes ?? (topmostFullScreenRoute.key ? getPreservedNavigatorState(topmostFullScreenRoute.key)?.routes : undefined);

// A wide layout pads the split with a placeholder SCREENS.REPORT route that carries no reportID (or an empty one),
// so matching on the route name alone would treat that empty placeholder as a revealed report. Require a non-empty reportID.
return !!innerRoutes?.some(
(route) => route.name === SCREENS.REPORT && typeof route.params === 'object' && route.params !== null && 'reportID' in route.params && !!route.params.reportID,
);
}

export default isReportRevealedInTopmostSplitNavigator;
4 changes: 2 additions & 2 deletions src/libs/navigateAfterOnboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Onyx from 'react-native-onyx';
import {setDisableDismissOnEscape} from './actions/Modal';
import SidePanelActions from './actions/SidePanel';
import {setOnboardingRHPVariant} from './actions/Welcome';
import isReportTopmostSplitNavigator from './Navigation/helpers/isReportTopmostSplitNavigator';
import isReportRevealedInTopmostSplitNavigator from './Navigation/helpers/isReportRevealedInTopmostSplitNavigator';
import {dismissOnboardingModalBeforeExit} from './Navigation/helpers/OnboardingNavigationUtils';
import shouldOpenOnAdminRoom from './Navigation/helpers/shouldOpenOnAdminRoom';
import Navigation from './Navigation/Navigation';
Expand Down Expand Up @@ -108,7 +108,7 @@ function navigateAfterOnboarding(
);
if (reportID) {
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(reportID), navigationOptions);
} else if (!isReportTopmostSplitNavigator()) {
} else if (!isReportRevealedInTopmostSplitNavigator()) {
// Navigate to home to trigger guard evaluation
Navigation.navigate(ROUTES.HOME, navigationOptions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import isReportRevealedInTopmostSplitNavigator from '@libs/Navigation/helpers/isReportRevealedInTopmostSplitNavigator';

import NAVIGATORS from '@src/NAVIGATORS';
import SCREENS from '@src/SCREENS';

const mockGetRootState = jest.fn();

jest.mock('@libs/Navigation/navigationRef', () => ({
__esModule: true,
default: {
getRootState: () => mockGetRootState() as unknown,
},
}));

/**
* Builds a root state whose topmost TAB_NAVIGATOR focuses a REPORTS_SPLIT_NAVIGATOR containing the given inner routes.
*/
function rootStateWithReportsSplitRoutes(innerRoutes: Array<{name: string; params?: Record<string, unknown>}>) {
return {
routes: [
{
name: NAVIGATORS.TAB_NAVIGATOR,
state: {
index: 0,
routes: [
{
name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR,
state: {routes: innerRoutes},
},
],
},
},
],
};
}

describe('isReportRevealedInTopmostSplitNavigator', () => {
beforeEach(() => {
mockGetRootState.mockReset();
});

it('returns false when there is no root state', () => {
mockGetRootState.mockReturnValue(undefined);
expect(isReportRevealedInTopmostSplitNavigator()).toBe(false);
});

it('returns false when the topmost full-screen route is not a REPORTS_SPLIT_NAVIGATOR', () => {
mockGetRootState.mockReturnValue({
routes: [
{
name: NAVIGATORS.TAB_NAVIGATOR,
state: {index: 0, routes: [{name: NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR}]},
},
],
});
expect(isReportRevealedInTopmostSplitNavigator()).toBe(false);
});

it('returns false when the split only contains the Inbox sidebar', () => {
mockGetRootState.mockReturnValue(rootStateWithReportsSplitRoutes([{name: SCREENS.INBOX}]));
expect(isReportRevealedInTopmostSplitNavigator()).toBe(false);
});

it('returns false for a placeholder REPORT route with no reportID param', () => {
mockGetRootState.mockReturnValue(rootStateWithReportsSplitRoutes([{name: SCREENS.INBOX}, {name: SCREENS.REPORT}]));
expect(isReportRevealedInTopmostSplitNavigator()).toBe(false);
});

it('returns false for a placeholder REPORT route with an empty reportID param', () => {
mockGetRootState.mockReturnValue(rootStateWithReportsSplitRoutes([{name: SCREENS.INBOX}, {name: SCREENS.REPORT, params: {reportID: ''}}]));
expect(isReportRevealedInTopmostSplitNavigator()).toBe(false);
});

it('returns true for a REPORT route with a non-empty reportID param', () => {
mockGetRootState.mockReturnValue(rootStateWithReportsSplitRoutes([{name: SCREENS.INBOX}, {name: SCREENS.REPORT, params: {reportID: '1234'}}]));
expect(isReportRevealedInTopmostSplitNavigator()).toBe(true);
});
});
20 changes: 10 additions & 10 deletions tests/unit/components/SidePanel/RHPVariantTest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ROUTES from '@src/ROUTES';

import type * as RHPVariantTest from '../../../../src/components/SidePanel/RHPVariantTest/index';

const mockIsReportTopmostSplitNavigator = jest.fn(() => false);
const mockIsReportRevealedInTopmostSplitNavigator = jest.fn<boolean, []>(() => false);

jest.mock('@expensify/react-native-hybrid-app', () => ({
__esModule: true,
Expand Down Expand Up @@ -35,9 +35,9 @@ jest.mock('react-native-onyx', () => ({
},
}));

jest.mock('@libs/Navigation/helpers/isReportTopmostSplitNavigator', () => ({
jest.mock('@libs/Navigation/helpers/isReportRevealedInTopmostSplitNavigator', () => ({
__esModule: true,
default: () => mockIsReportTopmostSplitNavigator(),
default: () => mockIsReportRevealedInTopmostSplitNavigator(),
}));

jest.mock('@libs/Navigation/Navigation', () => ({
Expand All @@ -59,35 +59,35 @@ const {handleRHPVariantNavigation} = jest.requireActual<typeof RHPVariantTest>('
describe('handleRHPVariantNavigation', () => {
beforeEach(() => {
jest.clearAllMocks();
mockIsReportTopmostSplitNavigator.mockReturnValue(false);
mockIsReportRevealedInTopmostSplitNavigator.mockReturnValue(false);
});

it('preserves the topmost report for the rhpHomePage variant', () => {
mockIsReportTopmostSplitNavigator.mockReturnValue(true);
it('preserves the revealed report for the rhpHomePage variant', () => {
mockIsReportRevealedInTopmostSplitNavigator.mockReturnValue(true);

handleRHPVariantNavigation('policyID', CONST.ONBOARDING_RHP_VARIANT.RHP_HOME_PAGE);

expect(Navigation.navigate).not.toHaveBeenCalled();
expect(SidePanelActions.openSidePanel).toHaveBeenCalledWith(true);
});

it('navigates home for the rhpHomePage variant when no report is topmost', () => {
it('navigates home for the rhpHomePage variant when no report is revealed', () => {
handleRHPVariantNavigation('policyID', CONST.ONBOARDING_RHP_VARIANT.RHP_HOME_PAGE);

expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.HOME, undefined);
expect(SidePanelActions.openSidePanel).toHaveBeenCalledWith(true);
});

it('preserves the topmost report for the trackExpensesWithConcierge variant and opens the side panel on top of it', () => {
mockIsReportTopmostSplitNavigator.mockReturnValue(true);
it('preserves the revealed report for the trackExpensesWithConcierge variant and opens the side panel on top of it', () => {
mockIsReportRevealedInTopmostSplitNavigator.mockReturnValue(true);

handleRHPVariantNavigation('policyID', CONST.ONBOARDING_RHP_VARIANT.TRACK_EXPENSES_WITH_CONCIERGE);

expect(Navigation.navigate).not.toHaveBeenCalled();
expect(SidePanelActions.openSidePanel).toHaveBeenCalledWith(true);
});

it('navigates home for the trackExpensesWithConcierge variant when no report is topmost', () => {
it('navigates home for the trackExpensesWithConcierge variant when the Inbox tab is topmost but no report is revealed', () => {
handleRHPVariantNavigation('policyID', CONST.ONBOARDING_RHP_VARIANT.TRACK_EXPENSES_WITH_CONCIERGE);

expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.HOME, undefined);
Expand Down
20 changes: 14 additions & 6 deletions tests/unit/navigateAfterOnboardingTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const REPORT_ID = '3';
const USER_ID = '4';
const mockFindLastAccessedReport = jest.fn<OnyxEntry<Report>, Parameters<typeof ReportUtils.findLastAccessedReport>>();
const mockShouldOpenOnAdminRoom = jest.fn();
const mockIsReportTopmostSplitNavigator = jest.fn(() => false);
const mockIsReportRevealedInTopmostSplitNavigator = jest.fn<boolean, []>(() => false);

jest.mock('@expensify/react-native-hybrid-app', () => ({
__esModule: true,
Expand Down Expand Up @@ -74,9 +74,9 @@ jest.mock('@libs/Navigation/helpers/shouldOpenOnAdminRoom', () => ({
default: () => mockShouldOpenOnAdminRoom() as boolean,
}));

jest.mock('@libs/Navigation/helpers/isReportTopmostSplitNavigator', () => ({
jest.mock('@libs/Navigation/helpers/isReportRevealedInTopmostSplitNavigator', () => ({
__esModule: true,
default: () => mockIsReportTopmostSplitNavigator(),
default: () => mockIsReportRevealedInTopmostSplitNavigator(),
}));

describe('navigateAfterOnboarding', () => {
Expand All @@ -88,7 +88,7 @@ describe('navigateAfterOnboarding', () => {

beforeEach(async () => {
jest.clearAllMocks();
mockIsReportTopmostSplitNavigator.mockReturnValue(false);
mockIsReportRevealedInTopmostSplitNavigator.mockReturnValue(false);
return Onyx.clear();
});

Expand All @@ -109,14 +109,22 @@ describe('navigateAfterOnboarding', () => {
expect(navigate).toHaveBeenCalledWith(ROUTES.HOME, undefined);
});

it('should preserve the topmost report if onboardingAdminsChatReportID is not provided on larger screens', () => {
it('should preserve the revealed report if onboardingAdminsChatReportID is not provided on larger screens', () => {
const navigate = jest.spyOn(Navigation, 'navigate');
mockIsReportTopmostSplitNavigator.mockReturnValue(true);
mockIsReportRevealedInTopmostSplitNavigator.mockReturnValue(true);

navigateAfterOnboarding(false, true, '', {}, undefined, undefined);
expect(navigate).not.toHaveBeenCalled();
});

it('should navigate to home when the Inbox tab is topmost but no report is revealed on larger screens', () => {
const navigate = jest.spyOn(Navigation, 'navigate');
mockIsReportRevealedInTopmostSplitNavigator.mockReturnValue(false);

navigateAfterOnboarding(false, true, '', {}, undefined, undefined);
expect(navigate).toHaveBeenCalledWith(ROUTES.HOME, undefined);
});

it('should not navigate to last accessed report if it is a concierge chat on small screens', async () => {
const navigate = jest.spyOn(Navigation, 'navigate');
const lastAccessedReport = {
Expand Down
Loading