Skip to content
16 changes: 8 additions & 8 deletions src/libs/actions/Welcome/OnboardingFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {translate} from '@libs/Localize';
import getAdaptedStateFromPath from '@libs/Navigation/helpers/getAdaptedStateFromPath';
import {linkingConfig} from '@libs/Navigation/linkingConfig';
import Navigation, {navigationRef} from '@libs/Navigation/Navigation';
import type {RootNavigatorParamList} from '@libs/Navigation/types';

import {openApp} from '@userActions/App';
import type {Video} from '@userActions/Report';
Expand All @@ -20,7 +19,7 @@ import type {NavigationState, PartialState} from '@react-navigation/native';
import type {OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';

import {findFocusedRoute, getStateFromPath} from '@react-navigation/native';
import {getStateFromPath} from '@react-navigation/native';
import Onyx from 'react-native-onyx';

type OnboardingCompanySize = ValueOf<typeof CONST.ONBOARDING_COMPANY_SIZE>;
Expand Down Expand Up @@ -90,15 +89,16 @@ Onyx.connectWithoutView({
* Start a new onboarding flow or continue from the last visited onboarding page.
*/
function startOnboardingFlow(startOnboardingFlowParams: GetOnboardingInitialPathParamsType) {
const currentRoute = navigationRef.getCurrentRoute();
const onboardingPath = startOnboardingFlowParams.resumePath ?? getOnboardingInitialPath(startOnboardingFlowParams);
const adaptedState = getAdaptedStateFromPath(onboardingPath as Route, undefined, false);
const focusedRoute = findFocusedRoute(adaptedState as PartialState<NavigationState<RootNavigatorParamList>>);
if (focusedRoute?.name === currentRoute?.name) {
const rootState = navigationRef.getRootState();

if (rootState.routes.some((route) => route.name === NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR)) {
return;
}
const rootState = navigationRef.getRootState();

const onboardingPath = startOnboardingFlowParams.resumePath ?? getOnboardingInitialPath(startOnboardingFlowParams);
const adaptedState = getAdaptedStateFromPath(onboardingPath as Route, undefined, false);
const rootStateRouteNamesSet = new Set(rootState.routes.map((route) => route.name));

navigationRef.resetRoot({
...rootState,
...adaptedState,
Expand Down
67 changes: 66 additions & 1 deletion tests/unit/OnboardingFlowTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import {getOnboardingInitialPath, getRequired2FAOnboardingResumePath} from '@libs/actions/Welcome/OnboardingFlow';
import {getOnboardingInitialPath, getRequired2FAOnboardingResumePath, startOnboardingFlow} from '@libs/actions/Welcome/OnboardingFlow';
import type {GetOnboardingInitialPathParamsType} from '@libs/actions/Welcome/OnboardingFlow';
import getAdaptedStateFromPath from '@libs/Navigation/helpers/getAdaptedStateFromPath';
import navigationRef from '@libs/Navigation/navigationRef';

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

import type * as NativeNavigation from '@react-navigation/native';

jest.mock('@libs/Navigation/navigationRef', () => ({
getRootState: jest.fn(),
resetRoot: jest.fn(),
}));

jest.mock('@libs/Navigation/helpers/getAdaptedStateFromPath', () => jest.fn());

describe('OnboardingFlow', () => {
describe('getOnboardingInitialPath', () => {
Expand Down Expand Up @@ -260,4 +273,56 @@ describe('OnboardingFlow', () => {
expect(getRequired2FAOnboardingResumePath(params)).toBe('/onboarding/work-email/validation');
});
});

describe('startOnboardingFlow', () => {
/* eslint-disable @typescript-eslint/unbound-method -- jest.fn() mocks don't rely on `this` binding */
const mockedGetRootState = jest.mocked(navigationRef.getRootState);
const mockedResetRoot = jest.mocked(navigationRef.resetRoot);
/* eslint-enable @typescript-eslint/unbound-method */
const mockedGetAdaptedStateFromPath = jest.mocked(getAdaptedStateFromPath);

const params: GetOnboardingInitialPathParamsType = {
isUserFromPublicDomain: false,
hasAccessiblePolicies: true,
currentOnboardingPurposeSelected: undefined,
currentOnboardingCompanySize: undefined,
onboardingInitialPath: '/onboarding/private-domain',
onboardingValues: undefined,
// resumePath bypasses getOnboardingInitialPath so the test drives the resolved target directly.
resumePath: '/onboarding/personal-details',
};

// getRootState's return type requires a full NavigationState, so build a minimal-but-complete one.
const buildRootState = (routes: NativeNavigation.NavigationState['routes']): NativeNavigation.NavigationState => ({
key: 'root',
index: Math.max(routes.length - 1, 0),
routeNames: routes.map((route) => route.name),
routes,
type: 'stack',
stale: false,
});

beforeEach(() => {
jest.clearAllMocks();
mockedGetAdaptedStateFromPath.mockReturnValue({routes: [{name: NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR}]} as ReturnType<typeof getAdaptedStateFromPath>);
});

it('should not call resetRoot when the onboarding navigator is already mounted (no-op that would still fire replaceState)', () => {
// Onboarding navigator already in the root state, so there is nothing to mount and resetRoot must be skipped.
mockedGetRootState.mockReturnValue(buildRootState([{key: 'onboarding', name: NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR}]));

startOnboardingFlow(params);

expect(mockedResetRoot).not.toHaveBeenCalled();
});

it('should call resetRoot to mount the onboarding navigator when it is not yet in the root state', () => {
// Onboarding navigator not yet in root state, so resetRoot runs to mount it.
mockedGetRootState.mockReturnValue(buildRootState([{key: 'home', name: SCREENS.HOME}]));

startOnboardingFlow(params);

expect(mockedResetRoot).toHaveBeenCalledTimes(1);
});
});
});
Loading