Skip to content

Commit

Permalink
Merge pull request #55654 from mkzie2/mkzie2-issue/55336
Browse files Browse the repository at this point in the history
  • Loading branch information
dangrous authored Feb 4, 2025
2 parents 69557c8 + 7e793ce commit c400da4
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/libs/navigateAfterOnboarding.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import ROUTES from '@src/ROUTES';
import Navigation from './Navigation/Navigation';
import shouldOpenOnAdminRoom from './Navigation/shouldOpenOnAdminRoom';
import * as ReportUtils from './ReportUtils';
import {findLastAccessedReport, isConciergeChatReport} from './ReportUtils';

const navigateAfterOnboarding = (isSmallScreenWidth: boolean, canUseDefaultRooms: boolean | undefined, onboardingPolicyID?: string, activeWorkspaceID?: string) => {
const navigateAfterOnboarding = (
isSmallScreenWidth: boolean,
canUseDefaultRooms: boolean | undefined,
onboardingPolicyID?: string,
activeWorkspaceID?: string,
onboardingAdminsChatReportID?: string,
) => {
Navigation.dismissModal();

// When hasCompletedGuidedSetupFlow is true, OnboardingModalNavigator in AuthScreen is removed from the navigation stack.
// On small screens, this removal redirects navigation to HOME. Dismissing the modal doesn't work properly,
// so we need to specifically navigate to the last accessed report.
if (!isSmallScreenWidth) {
if (onboardingAdminsChatReportID) {
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(onboardingAdminsChatReportID));
}
return;
}

const lastAccessedReport = ReportUtils.findLastAccessedReport(!canUseDefaultRooms, shouldOpenOnAdminRoom(), activeWorkspaceID);
const lastAccessedReport = findLastAccessedReport(!canUseDefaultRooms, shouldOpenOnAdminRoom(), activeWorkspaceID);
const lastAccessedReportID = lastAccessedReport?.reportID;
// we don't want to navigate to newly creaded workspace after onboarding completed.
if (!lastAccessedReportID || lastAccessedReport.policyID === onboardingPolicyID || ReportUtils.isConciergeChatReport(lastAccessedReport)) {
// we don't want to navigate to newly created workspaces after onboarding is completed.
if (!lastAccessedReportID || lastAccessedReport.policyID === onboardingPolicyID || isConciergeChatReport(lastAccessedReport)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ function BaseOnboardingAccounting({shouldUseNativeStyles}: BaseOnboardingAccount
setOnboardingAdminsChatReportID();
setOnboardingPolicyID();
});
navigateAfterOnboarding(isSmallScreenWidth, canUseDefaultRooms, onboardingPolicyID, activeWorkspaceID);
navigateAfterOnboarding(isSmallScreenWidth, canUseDefaultRooms, onboardingPolicyID, activeWorkspaceID, onboardingAdminsChatReportID);
}}
pressOnEnter
/>
Expand Down
92 changes: 92 additions & 0 deletions tests/unit/navigateAfterOnboardingTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import type {OnyxEntry} from 'react-native-onyx';
import navigateAfterOnboarding from '@libs/navigateAfterOnboarding';
import Navigation from '@libs/Navigation/Navigation';
// eslint-disable-next-line no-restricted-syntax
import type * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
import type {Report} from '@src/types/onyx';

const ONBOARDING_ADMINS_CHAT_REPORT_ID = '1';
const ONBOARDING_POLICY_ID = '2';
const ACTIVE_WORKSPACE_ID = '3';
const REPORT_ID = '4';
const USER_ID = '5';
const mockFindLastAccessedReport = jest.fn();
const mockShouldOpenOnAdminRoom = jest.fn();

jest.mock('@react-navigation/native', () => {
const actualNav = jest.requireActual<typeof Navigation>('@react-navigation/native');
return {
...actualNav,
useIsFocused: jest.fn(),
triggerTransitionEnd: jest.fn(),
};
});

jest.mock('@libs/ReportUtils', () => ({
findLastAccessedReport: () => mockFindLastAccessedReport() as OnyxEntry<Report>,
parseReportRouteParams: jest.fn(() => ({})),
isConciergeChatReport: jest.requireActual<typeof ReportUtils>('@libs/ReportUtils').isConciergeChatReport,
}));

jest.mock('@libs/Navigation/shouldOpenOnAdminRoom', () => ({
// eslint-disable-next-line @typescript-eslint/naming-convention
__esModule: true,
default: () => mockShouldOpenOnAdminRoom() as boolean,
}));

describe('navigateAfterOnboarding', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should navigate to the admin room report if onboardingAdminsChatReportID is provided', () => {
const navigate = jest.spyOn(Navigation, 'navigate');

navigateAfterOnboarding(false, true, undefined, undefined, ONBOARDING_ADMINS_CHAT_REPORT_ID);
expect(navigate).toHaveBeenCalledWith(ROUTES.REPORT_WITH_ID.getRoute(ONBOARDING_ADMINS_CHAT_REPORT_ID));
});

it('should not navigate if onboardingAdminsChatReportID is not provided', () => {
navigateAfterOnboarding(false, true, undefined, undefined);
expect(Navigation.navigate).not.toHaveBeenCalled();
});

it('should navigate to LHN if it is a concierge chat on small screens', () => {
const navigate = jest.spyOn(Navigation, 'navigate');
const lastAccessedReport = {
reportID: REPORT_ID,
participants: {
[CONST.ACCOUNT_ID.CONCIERGE.toString()]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS},
[USER_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS},
},
reportName: 'Concierge',
type: CONST.REPORT.TYPE.CHAT,
};
mockFindLastAccessedReport.mockReturnValue(lastAccessedReport);
mockShouldOpenOnAdminRoom.mockReturnValue(false);

navigateAfterOnboarding(true, true, ONBOARDING_POLICY_ID, ACTIVE_WORKSPACE_ID, ONBOARDING_ADMINS_CHAT_REPORT_ID);
expect(navigate).not.toHaveBeenCalled();
});

it('should navigate to LHN if it is onboarding workspace chat on small screens', () => {
const lastAccessedReport = {reportID: REPORT_ID, policyID: ONBOARDING_POLICY_ID};
mockFindLastAccessedReport.mockReturnValue(lastAccessedReport);
mockShouldOpenOnAdminRoom.mockReturnValue(false);

navigateAfterOnboarding(true, true, ONBOARDING_POLICY_ID, ACTIVE_WORKSPACE_ID, ONBOARDING_ADMINS_CHAT_REPORT_ID);
expect(Navigation.navigate).not.toHaveBeenCalled();
});

it('should navigate to last accessed report if shouldOpenOnAdminRoom is true on small screens', () => {
const navigate = jest.spyOn(Navigation, 'navigate');
const lastAccessedReport = {reportID: REPORT_ID};
mockFindLastAccessedReport.mockReturnValue(lastAccessedReport);
mockShouldOpenOnAdminRoom.mockReturnValue(true);

navigateAfterOnboarding(true, true, ONBOARDING_POLICY_ID, ACTIVE_WORKSPACE_ID, ONBOARDING_ADMINS_CHAT_REPORT_ID);
expect(navigate).toHaveBeenCalledWith(ROUTES.REPORT_WITH_ID.getRoute(REPORT_ID));
});
});

0 comments on commit c400da4

Please sign in to comment.