Skip to content

Commit 80fe088

Browse files
committed
refactor debug utils
1 parent 68ed6fe commit 80fe088

3 files changed

Lines changed: 87 additions & 38 deletions

File tree

src/libs/DebugUtils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type {Beta, Report, ReportAction, ReportActions, ReportNameValuePairs, Tr
99
import type {Errors} from '@src/types/onyx/OnyxCommon';
1010
import type {Comment} from '@src/types/onyx/Transaction';
1111
import SafeString from '@src/utils/SafeString';
12-
import {getCurrentUserEmail, getUserAccountID} from './actions/IOU';
1312
import {getLinkedTransactionID} from './ReportActionsUtils';
1413
import {getReasonAndReportActionThatRequiresAttention, reasonForReportToBeInOptionList} from './ReportUtils';
1514
import SidebarUtils from './SidebarUtils';
@@ -1458,12 +1457,17 @@ type GBRReasonAndReportAction = {
14581457
/**
14591458
* Gets the reason and report action that is causing the GBR to show up in LHN row
14601459
*/
1461-
function getReasonAndReportActionForGBRInLHNRow(report: OnyxEntry<Report>, isReportArchived = false): GBRReasonAndReportAction | null {
1460+
function getReasonAndReportActionForGBRInLHNRow(
1461+
report: OnyxEntry<Report>,
1462+
currentUserLogin: string,
1463+
currentUserAccountID: number,
1464+
isReportArchived = false,
1465+
): GBRReasonAndReportAction | null {
14621466
if (!report) {
14631467
return null;
14641468
}
14651469

1466-
const {reason, reportAction} = getReasonAndReportActionThatRequiresAttention(report, getCurrentUserEmail(), getUserAccountID(), undefined, isReportArchived) ?? {};
1470+
const {reason, reportAction} = getReasonAndReportActionThatRequiresAttention(report, currentUserLogin, currentUserAccountID, undefined, isReportArchived) ?? {};
14671471

14681472
if (reason) {
14691473
return {reason: `debug.reasonGBR.${reason}`, reportAction};

src/pages/Debug/Report/DebugReportPage.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function DebugReportPage({
7676
const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID);
7777
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED);
7878
const [isSelfTourViewed] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {selector: hasSeenTourSelector});
79-
const {accountID: currentUserAccountID} = useCurrentUserPersonalDetails();
79+
const {accountID: currentUserAccountID, email: currentUserLogin = ''} = useCurrentUserPersonalDetails();
8080
const transactionID = DebugUtils.getTransactionID(report, reportActions);
8181
const isReportArchived = useReportIsArchived(reportID);
8282

@@ -87,7 +87,7 @@ function DebugReportPage({
8787

8888
const shouldDisplayViolations = !!getViolatingReportIDForRBRInLHN(report, transactionViolations);
8989
const hasViolations = !!shouldDisplayViolations;
90-
const {reason: reasonGBR, reportAction: reportActionGBR} = DebugUtils.getReasonAndReportActionForGBRInLHNRow(report, isReportArchived) ?? {};
90+
const {reason: reasonGBR, reportAction: reportActionGBR} = DebugUtils.getReasonAndReportActionForGBRInLHNRow(report, currentUserLogin, currentUserAccountID, isReportArchived) ?? {};
9191
const {reason: reasonRBR, reportAction: reportActionRBR} =
9292
DebugUtils.getReasonAndReportActionForRBRInLHNRow(
9393
report,
@@ -156,7 +156,22 @@ function DebugReportPage({
156156
: undefined,
157157
},
158158
];
159-
}, [report, transactionViolations, isReportArchived, chatReport, reportActions, transactions, reportAttributes?.reportErrors, betas, priorityMode, draftComment, translate, isOffline]);
159+
}, [
160+
report,
161+
transactionViolations,
162+
currentUserLogin,
163+
currentUserAccountID,
164+
isReportArchived,
165+
chatReport,
166+
reportActions,
167+
transactions,
168+
reportAttributes?.reportErrors,
169+
isOffline,
170+
betas,
171+
priorityMode,
172+
draftComment,
173+
translate,
174+
]);
160175

161176
const icons = useMemoizedLazyExpensifyIcons(['Eye']);
162177

tests/unit/DebugUtilsTest.ts

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const MOCK_REPORT_ACTION: ReportAction = {
3030
const MOCK_TRANSACTION: Transaction = createRandomTransaction(0);
3131

3232
const MOCK_DRAFT_REPORT_ACTION = DebugUtils.onyxDataToString(MOCK_REPORT_ACTION);
33+
const RORY_EMAIL = 'rory@email.com';
34+
const RORY_ACCOUNT_ID = 5;
3335

3436
const MOCK_CONST_ENUM = {
3537
foo: 'foo',
@@ -1006,7 +1008,7 @@ describe('DebugUtils', () => {
10061008
Onyx.clear();
10071009
});
10081010
it('returns undefined reason when report is not defined', () => {
1009-
const {reason} = DebugUtils.getReasonAndReportActionForGBRInLHNRow(undefined) ?? {};
1011+
const {reason} = DebugUtils.getReasonAndReportActionForGBRInLHNRow(undefined, RORY_EMAIL, RORY_ACCOUNT_ID) ?? {};
10101012
expect(reason).toBeUndefined();
10111013
});
10121014
it('returns correct reason when report has a join request', async () => {
@@ -1024,50 +1026,70 @@ describe('DebugUtils', () => {
10241026
};
10251027
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`, MOCK_REPORT_ACTIONS);
10261028
const {reason} =
1027-
DebugUtils.getReasonAndReportActionForGBRInLHNRow({
1028-
reportID: '1',
1029-
}) ?? {};
1029+
DebugUtils.getReasonAndReportActionForGBRInLHNRow(
1030+
{
1031+
reportID: '1',
1032+
},
1033+
RORY_EMAIL,
1034+
RORY_ACCOUNT_ID,
1035+
) ?? {};
10301036
expect(reason).toBe('debug.reasonGBR.hasJoinRequest');
10311037
});
10321038
it('returns correct reason when report is unread with mention', () => {
10331039
const {reason} =
1034-
DebugUtils.getReasonAndReportActionForGBRInLHNRow({
1035-
reportID: '1',
1036-
lastMentionedTime: '2024-08-10 18:70:44.171',
1037-
lastReadTime: '2024-08-08 18:70:44.171',
1038-
}) ?? {};
1040+
DebugUtils.getReasonAndReportActionForGBRInLHNRow(
1041+
{
1042+
reportID: '1',
1043+
lastMentionedTime: '2024-08-10 18:70:44.171',
1044+
lastReadTime: '2024-08-08 18:70:44.171',
1045+
},
1046+
RORY_EMAIL,
1047+
RORY_ACCOUNT_ID,
1048+
) ?? {};
10391049
expect(reason).toBe('debug.reasonGBR.isUnreadWithMention');
10401050
});
10411051
it('returns correct reason when report has a task which is waiting for assignee to complete it', async () => {
10421052
await Onyx.set(ONYXKEYS.SESSION, {accountID: 12345});
10431053
const {reason} =
1044-
DebugUtils.getReasonAndReportActionForGBRInLHNRow({
1045-
reportID: '1',
1046-
type: CONST.REPORT.TYPE.TASK,
1047-
hasParentAccess: false,
1048-
managerID: 12345,
1049-
stateNum: CONST.REPORT.STATE_NUM.OPEN,
1050-
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
1051-
}) ?? {};
1054+
DebugUtils.getReasonAndReportActionForGBRInLHNRow(
1055+
{
1056+
reportID: '1',
1057+
type: CONST.REPORT.TYPE.TASK,
1058+
hasParentAccess: false,
1059+
managerID: 12345,
1060+
stateNum: CONST.REPORT.STATE_NUM.OPEN,
1061+
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
1062+
},
1063+
RORY_EMAIL,
1064+
RORY_ACCOUNT_ID,
1065+
) ?? {};
10521066
expect(reason).toBe('debug.reasonGBR.isWaitingForAssigneeToCompleteAction');
10531067
});
10541068
it('returns correct reason when report has a child report awaiting action from the user', () => {
10551069
const {reason} =
1056-
DebugUtils.getReasonAndReportActionForGBRInLHNRow({
1057-
reportID: '1',
1058-
hasOutstandingChildRequest: true,
1059-
}) ?? {};
1070+
DebugUtils.getReasonAndReportActionForGBRInLHNRow(
1071+
{
1072+
reportID: '1',
1073+
hasOutstandingChildRequest: true,
1074+
},
1075+
RORY_EMAIL,
1076+
RORY_ACCOUNT_ID,
1077+
) ?? {};
10601078
expect(reason).toBe('debug.reasonGBR.hasChildReportAwaitingAction');
10611079
});
10621080
it('returns undefined reason when report has no GBR', () => {
10631081
const {reason} =
1064-
DebugUtils.getReasonAndReportActionForGBRInLHNRow({
1065-
reportID: '1',
1066-
}) ?? {};
1082+
DebugUtils.getReasonAndReportActionForGBRInLHNRow(
1083+
{
1084+
reportID: '1',
1085+
},
1086+
RORY_EMAIL,
1087+
RORY_ACCOUNT_ID,
1088+
) ?? {};
10671089
expect(reason).toBeUndefined();
10681090
});
10691091
it('returns undefined reportAction when report is not defined', () => {
1070-
const {reportAction} = DebugUtils.getReasonAndReportActionForGBRInLHNRow(undefined) ?? {};
1092+
const {reportAction} = DebugUtils.getReasonAndReportActionForGBRInLHNRow(undefined, RORY_EMAIL, RORY_ACCOUNT_ID) ?? {};
10711093
expect(reportAction).toBeUndefined();
10721094
});
10731095
it('returns the report action which is a join request', async () => {
@@ -1091,9 +1113,13 @@ describe('DebugUtils', () => {
10911113
};
10921114
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`, MOCK_REPORT_ACTIONS);
10931115
const {reportAction} =
1094-
DebugUtils.getReasonAndReportActionForGBRInLHNRow({
1095-
reportID: '1',
1096-
}) ?? {};
1116+
DebugUtils.getReasonAndReportActionForGBRInLHNRow(
1117+
{
1118+
reportID: '1',
1119+
},
1120+
RORY_EMAIL,
1121+
RORY_ACCOUNT_ID,
1122+
) ?? {};
10971123
expect(reportAction).toMatchObject(MOCK_REPORT_ACTIONS['1']);
10981124
});
10991125
it('returns the report action which is awaiting action', async () => {
@@ -1160,14 +1186,18 @@ describe('DebugUtils', () => {
11601186
},
11611187
});
11621188
// eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style
1163-
const {reportAction} = DebugUtils.getReasonAndReportActionForGBRInLHNRow(MOCK_REPORTS[`${ONYXKEYS.COLLECTION.REPORT}1`] as Report) ?? {};
1189+
const {reportAction} = DebugUtils.getReasonAndReportActionForGBRInLHNRow(MOCK_REPORTS[`${ONYXKEYS.COLLECTION.REPORT}1`] as Report, RORY_EMAIL, RORY_ACCOUNT_ID) ?? {};
11641190
expect(reportAction).toMatchObject(MOCK_REPORT_ACTIONS['1']);
11651191
});
11661192
it('returns undefined report action when report has no GBR', () => {
11671193
const {reportAction} =
1168-
DebugUtils.getReasonAndReportActionForGBRInLHNRow({
1169-
reportID: '1',
1170-
}) ?? {};
1194+
DebugUtils.getReasonAndReportActionForGBRInLHNRow(
1195+
{
1196+
reportID: '1',
1197+
},
1198+
RORY_EMAIL,
1199+
RORY_ACCOUNT_ID,
1200+
) ?? {};
11711201
expect(reportAction).toBeUndefined();
11721202
});
11731203
});

0 commit comments

Comments
 (0)