Skip to content

Commit 4ab4f0e

Browse files
authored
Merge pull request Expensify#67756 from shubham1206agra/refactor-onyx-36
Removed Onyx.connect from DebugUtils (part 2)
2 parents 26391d2 + 3af7c00 commit 4ab4f0e

4 files changed

Lines changed: 62 additions & 41 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"test:debug": "TZ=utc NODE_OPTIONS='--inspect-brk --experimental-vm-modules' jest --runInBand",
4747
"perf-test": "NODE_OPTIONS=--experimental-vm-modules npx reassure",
4848
"typecheck": "NODE_OPTIONS=--max_old_space_size=8192 tsc",
49-
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=261 --cache --cache-location=node_modules/.cache/eslint",
49+
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=260 --cache --cache-location=node_modules/.cache/eslint",
5050
"lint-changed": "NODE_OPTIONS=--max_old_space_size=8192 ./scripts/lintChanged.sh",
5151
"lint-watch": "npx eslint-watch --watch --changed",
5252
"shellcheck": "./scripts/shellCheck.sh",

src/libs/DebugUtils.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
/* eslint-disable max-classes-per-file */
44
import {isMatch, isValid} from 'date-fns';
55
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
6-
import Onyx from 'react-native-onyx';
76
import type {TupleToUnion} from 'type-fest';
87
import CONST from '@src/CONST';
98
import type {TranslationPaths} from '@src/languages/types';
10-
import ONYXKEYS from '@src/ONYXKEYS';
119
import type {Beta, Report, ReportAction, ReportActions, ReportNameValuePairs, Transaction, TransactionViolation} from '@src/types/onyx';
1210
import type {Errors} from '@src/types/onyx/OnyxCommon';
1311
import type {Comment} from '@src/types/onyx/Transaction';
1412
import {getLinkedTransactionID} from './ReportActionsUtils';
15-
import {getReasonAndReportActionThatRequiresAttention, reasonForReportToBeInOptionList, shouldDisplayViolationsRBRInLHN} from './ReportUtils';
13+
import {getReasonAndReportActionThatRequiresAttention, reasonForReportToBeInOptionList} from './ReportUtils';
1614
import SidebarUtils from './SidebarUtils';
1715
import {getTransactionID as TransactionUtilsGetTransactionID} from './TransactionUtils';
1816

@@ -87,15 +85,6 @@ const TRANSACTION_REQUIRED_PROPERTIES: Array<keyof Transaction> = ['transactionI
8785

8886
const TRANSACTION_VIOLATION_REQUIRED_PROPERTIES: Array<keyof TransactionViolation> = ['type', 'name'] satisfies Array<keyof TransactionViolation>;
8987

90-
let transactionViolations: OnyxCollection<TransactionViolation[]>;
91-
Onyx.connect({
92-
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
93-
waitForCollectionCallback: true,
94-
callback: (value) => {
95-
transactionViolations = value;
96-
},
97-
});
98-
9988
function stringifyJSON(data: Record<string, unknown>) {
10089
return JSON.stringify(data, null, 6);
10190
}
@@ -1320,13 +1309,15 @@ function validateTransactionViolationJSON(json: string) {
13201309
function getReasonForShowingRowInLHN({
13211310
report,
13221311
chatReport,
1312+
doesReportHaveViolations,
13231313
hasRBR = false,
13241314
isReportArchived = false,
13251315
isInFocusMode = false,
13261316
betas = undefined,
13271317
}: {
13281318
report: OnyxEntry<Report>;
13291319
chatReport: OnyxEntry<Report>;
1320+
doesReportHaveViolations: boolean;
13301321
hasRBR?: boolean;
13311322
isReportArchived?: boolean;
13321323
isInFocusMode?: boolean;
@@ -1336,8 +1327,6 @@ function getReasonForShowingRowInLHN({
13361327
return null;
13371328
}
13381329

1339-
const doesReportHaveViolations = shouldDisplayViolationsRBRInLHN(report, transactionViolations);
1340-
13411330
const reason = reasonForReportToBeInOptionList({
13421331
report,
13431332
chatReport,
@@ -1399,6 +1388,7 @@ function getReasonAndReportActionForRBRInLHNRow(
13991388
chatReport: OnyxEntry<Report>,
14001389
reportActions: OnyxEntry<ReportActions>,
14011390
transactions: OnyxCollection<Transaction>,
1391+
transactionViolations: OnyxCollection<TransactionViolation[]>,
14021392
hasViolations: boolean,
14031393
reportErrors: Errors,
14041394
isArchivedReport = false,

src/pages/Debug/Report/DebugReportPage.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,27 @@ function DebugReportPage({
7575
const hasViolations = !!shouldDisplayViolations || shouldDisplayReportViolations;
7676
const {reason: reasonGBR, reportAction: reportActionGBR} = DebugUtils.getReasonAndReportActionForGBRInLHNRow(report, isReportArchived) ?? {};
7777
const {reason: reasonRBR, reportAction: reportActionRBR} =
78-
DebugUtils.getReasonAndReportActionForRBRInLHNRow(report, chatReport, reportActions, transactions, hasViolations, reportAttributes?.reportErrors ?? {}, isReportArchived) ?? {};
78+
DebugUtils.getReasonAndReportActionForRBRInLHNRow(
79+
report,
80+
chatReport,
81+
reportActions,
82+
transactions,
83+
transactionViolations,
84+
hasViolations,
85+
reportAttributes?.reportErrors ?? {},
86+
isReportArchived,
87+
) ?? {};
7988
const hasRBR = !!reasonRBR;
8089
const hasGBR = !hasRBR && !!reasonGBR;
81-
const reasonLHN = DebugUtils.getReasonForShowingRowInLHN({report, chatReport, betas, hasRBR, isReportArchived, isInFocusMode: priorityMode === CONST.PRIORITY_MODE.GSD});
90+
const reasonLHN = DebugUtils.getReasonForShowingRowInLHN({
91+
report,
92+
chatReport,
93+
betas,
94+
doesReportHaveViolations: shouldDisplayViolations,
95+
hasRBR,
96+
isReportArchived,
97+
isInFocusMode: priorityMode === CONST.PRIORITY_MODE.GSD,
98+
});
8299

83100
return [
84101
{

tests/unit/DebugUtilsTest.ts

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -735,12 +735,12 @@ describe('DebugUtils', () => {
735735
Onyx.clear();
736736
});
737737
it('returns null when report is not defined', () => {
738-
const reason = DebugUtils.getReasonForShowingRowInLHN({report: undefined, chatReport: chatReportR14932});
738+
const reason = DebugUtils.getReasonForShowingRowInLHN({report: undefined, chatReport: chatReportR14932, doesReportHaveViolations: false});
739739
expect(reason).toBeNull();
740740
});
741741
it('returns correct reason when report has a valid draft comment', async () => {
742742
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}1`, 'Hello world!');
743-
const reason = DebugUtils.getReasonForShowingRowInLHN({report: baseReport, chatReport: chatReportR14932});
743+
const reason = DebugUtils.getReasonForShowingRowInLHN({report: baseReport, chatReport: chatReportR14932, doesReportHaveViolations: false});
744744
expect(reason).toBe('debug.reasonVisibleInLHN.hasDraftComment');
745745
});
746746
it('returns correct reason when report has GBR', () => {
@@ -751,6 +751,7 @@ describe('DebugUtils', () => {
751751
lastReadTime: '2024-08-08 18:70:44.171',
752752
},
753753
chatReport: chatReportR14932,
754+
doesReportHaveViolations: false,
754755
});
755756
expect(reason).toBe('debug.reasonVisibleInLHN.hasGBR');
756757
});
@@ -761,6 +762,7 @@ describe('DebugUtils', () => {
761762
isPinned: true,
762763
},
763764
chatReport: chatReportR14932,
765+
doesReportHaveViolations: false,
764766
});
765767
expect(reason).toBe('debug.reasonVisibleInLHN.pinnedByUser');
766768
});
@@ -775,6 +777,7 @@ describe('DebugUtils', () => {
775777
},
776778
},
777779
chatReport: chatReportR14932,
780+
doesReportHaveViolations: false,
778781
});
779782
expect(reason).toBe('debug.reasonVisibleInLHN.hasAddWorkspaceRoomErrors');
780783
});
@@ -797,6 +800,7 @@ describe('DebugUtils', () => {
797800
},
798801
chatReport: chatReportR14932,
799802
isInFocusMode: true,
803+
doesReportHaveViolations: false,
800804
});
801805
expect(reason).toBe('debug.reasonVisibleInLHN.isUnread');
802806
});
@@ -813,6 +817,7 @@ describe('DebugUtils', () => {
813817
chatReport: chatReportR14932,
814818
hasRBR: false,
815819
isReportArchived: isReportArchived.current,
820+
doesReportHaveViolations: false,
816821
});
817822
expect(reason).toBe('debug.reasonVisibleInLHN.isArchived');
818823
});
@@ -823,13 +828,15 @@ describe('DebugUtils', () => {
823828
chatType: CONST.REPORT.CHAT_TYPE.SELF_DM,
824829
},
825830
chatReport: chatReportR14932,
831+
doesReportHaveViolations: false,
826832
});
827833
expect(reason).toBe('debug.reasonVisibleInLHN.isSelfDM');
828834
});
829835
it('returns correct reason when report is temporarily focused', () => {
830836
const reason = DebugUtils.getReasonForShowingRowInLHN({
831837
report: baseReport,
832838
chatReport: chatReportR14932,
839+
doesReportHaveViolations: false,
833840
});
834841
expect(reason).toBe('debug.reasonVisibleInLHN.isFocused');
835842
});
@@ -881,14 +888,8 @@ describe('DebugUtils', () => {
881888
modifiedAmount: 10,
882889
reportID: '1',
883890
},
884-
[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}1` as const]: [
885-
{
886-
type: CONST.VIOLATION_TYPES.VIOLATION,
887-
name: CONST.VIOLATIONS.MISSING_CATEGORY,
888-
},
889-
],
890891
});
891-
const reason = DebugUtils.getReasonForShowingRowInLHN({report: MOCK_TRANSACTION_REPORT, chatReport: chatReportR14932, hasRBR: true});
892+
const reason = DebugUtils.getReasonForShowingRowInLHN({report: MOCK_TRANSACTION_REPORT, chatReport: chatReportR14932, hasRBR: true, doesReportHaveViolations: true});
892893
expect(reason).toBe('debug.reasonVisibleInLHN.hasRBR');
893894
});
894895
it('returns correct reason when report has violations', async () => {
@@ -939,18 +940,12 @@ describe('DebugUtils', () => {
939940
modifiedAmount: 10,
940941
reportID: '1',
941942
},
942-
[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}1` as const]: [
943-
{
944-
type: CONST.VIOLATION_TYPES.VIOLATION,
945-
name: CONST.VIOLATIONS.MISSING_CATEGORY,
946-
},
947-
],
948943
});
949-
const reason = DebugUtils.getReasonForShowingRowInLHN({report: MOCK_EXPENSE_REPORT, chatReport: chatReportR14932, hasRBR: true});
944+
const reason = DebugUtils.getReasonForShowingRowInLHN({report: MOCK_EXPENSE_REPORT, chatReport: chatReportR14932, hasRBR: true, doesReportHaveViolations: true});
950945
expect(reason).toBe('debug.reasonVisibleInLHN.hasRBR');
951946
});
952947
it('returns correct reason when report has errors', () => {
953-
const reason = DebugUtils.getReasonForShowingRowInLHN({report: baseReport, chatReport: chatReportR14932, hasRBR: true});
948+
const reason = DebugUtils.getReasonForShowingRowInLHN({report: baseReport, chatReport: chatReportR14932, hasRBR: true, doesReportHaveViolations: false});
954949
expect(reason).toBe('debug.reasonVisibleInLHN.hasRBR');
955950
});
956951
});
@@ -1148,6 +1143,7 @@ describe('DebugUtils', () => {
11481143
chatReportR14932,
11491144
undefined,
11501145
{},
1146+
undefined,
11511147
false,
11521148
{},
11531149
) ?? {};
@@ -1202,6 +1198,7 @@ describe('DebugUtils', () => {
12021198
chatReportR14932,
12031199
undefined,
12041200
{},
1201+
undefined,
12051202
false,
12061203
{},
12071204
) ?? {};
@@ -1263,7 +1260,8 @@ describe('DebugUtils', () => {
12631260
},
12641261
});
12651262
const reportErrors = getAllReportErrors(MOCK_IOU_REPORT, MOCK_REPORT_ACTIONS);
1266-
const {reportAction} = DebugUtils.getReasonAndReportActionForRBRInLHNRow(MOCK_IOU_REPORT, chatReportR14932, MOCK_REPORT_ACTIONS, {}, false, reportErrors) ?? {};
1263+
const {reportAction} =
1264+
DebugUtils.getReasonAndReportActionForRBRInLHNRow(MOCK_IOU_REPORT, chatReportR14932, MOCK_REPORT_ACTIONS, {}, undefined, false, reportErrors) ?? {};
12671265
expect(reportAction).toMatchObject(MOCK_REPORT_ACTIONS['3']);
12681266
});
12691267
});
@@ -1322,7 +1320,8 @@ describe('DebugUtils', () => {
13221320
},
13231321
});
13241322
const reportErrors = getAllReportErrors(MOCK_IOU_REPORT, MOCK_REPORT_ACTIONS);
1325-
const {reportAction} = DebugUtils.getReasonAndReportActionForRBRInLHNRow(MOCK_IOU_REPORT, chatReportR14932, MOCK_REPORT_ACTIONS, {}, false, reportErrors) ?? {};
1323+
const {reportAction} =
1324+
DebugUtils.getReasonAndReportActionForRBRInLHNRow(MOCK_IOU_REPORT, chatReportR14932, MOCK_REPORT_ACTIONS, {}, undefined, false, reportErrors) ?? {};
13261325
expect(reportAction).toMatchObject(MOCK_REPORT_ACTIONS['3']);
13271326
});
13281327
});
@@ -1382,7 +1381,8 @@ describe('DebugUtils', () => {
13821381
},
13831382
});
13841383
const reportErrors = getAllReportErrors(MOCK_CHAT_REPORT, MOCK_CHAT_REPORT_ACTIONS);
1385-
const {reportAction} = DebugUtils.getReasonAndReportActionForRBRInLHNRow(MOCK_CHAT_REPORT, chatReportR14932, MOCK_CHAT_REPORT_ACTIONS, {}, false, reportErrors) ?? {};
1384+
const {reportAction} =
1385+
DebugUtils.getReasonAndReportActionForRBRInLHNRow(MOCK_CHAT_REPORT, chatReportR14932, MOCK_CHAT_REPORT_ACTIONS, {}, undefined, false, reportErrors) ?? {};
13861386
expect(reportAction).toMatchObject(MOCK_CHAT_REPORT_ACTIONS['1']);
13871387
});
13881388
it('returns correct report action which is a split bill and has an error', async () => {
@@ -1445,7 +1445,8 @@ describe('DebugUtils', () => {
14451445
},
14461446
});
14471447
const reportErrors = getAllReportErrors(MOCK_CHAT_REPORT, MOCK_REPORT_ACTIONS);
1448-
const {reportAction} = DebugUtils.getReasonAndReportActionForRBRInLHNRow(MOCK_CHAT_REPORT, chatReportR14932, MOCK_REPORT_ACTIONS, {}, false, reportErrors) ?? {};
1448+
const {reportAction} =
1449+
DebugUtils.getReasonAndReportActionForRBRInLHNRow(MOCK_CHAT_REPORT, chatReportR14932, MOCK_REPORT_ACTIONS, {}, undefined, false, reportErrors) ?? {};
14491450
expect(reportAction).toMatchObject(MOCK_REPORT_ACTIONS['3']);
14501451
});
14511452
it("returns undefined if there's no report action is a report preview or a split bill", async () => {
@@ -1502,7 +1503,8 @@ describe('DebugUtils', () => {
15021503
},
15031504
});
15041505
const reportErrors = getAllReportErrors(MOCK_IOU_REPORT, MOCK_REPORT_ACTIONS);
1505-
const {reportAction} = DebugUtils.getReasonAndReportActionForRBRInLHNRow(MOCK_IOU_REPORT, chatReportR14932, MOCK_REPORT_ACTIONS, {}, false, reportErrors) ?? {};
1506+
const {reportAction} =
1507+
DebugUtils.getReasonAndReportActionForRBRInLHNRow(MOCK_IOU_REPORT, chatReportR14932, MOCK_REPORT_ACTIONS, {}, undefined, false, reportErrors) ?? {};
15061508
expect(reportAction).toMatchObject(MOCK_REPORT_ACTIONS['3']);
15071509
});
15081510
});
@@ -1557,6 +1559,7 @@ describe('DebugUtils', () => {
15571559
chatReportR14932,
15581560
MOCK_REPORT_ACTIONS,
15591561
{},
1562+
undefined,
15601563
false,
15611564
reportErrors,
15621565
) ?? {};
@@ -1586,7 +1589,7 @@ describe('DebugUtils', () => {
15861589
};
15871590

15881591
const reportErrors = getAllReportErrors(mockedReport, mockedReportActions);
1589-
const {reason} = DebugUtils.getReasonAndReportActionForRBRInLHNRow(mockedReport, chatReportR14932, mockedReportActions, {}, false, reportErrors) ?? {};
1592+
const {reason} = DebugUtils.getReasonAndReportActionForRBRInLHNRow(mockedReport, chatReportR14932, mockedReportActions, {}, undefined, false, reportErrors) ?? {};
15901593
expect(reason).toBe('debug.reasonRBR.hasErrors');
15911594
});
15921595
it('returns correct reason when there are violations', () => {
@@ -1598,6 +1601,7 @@ describe('DebugUtils', () => {
15981601
chatReportR14932,
15991602
undefined,
16001603
{},
1604+
undefined,
16011605
true,
16021606
{},
16031607
) ?? {};
@@ -1612,6 +1616,7 @@ describe('DebugUtils', () => {
16121616
chatReportR14932,
16131617
undefined,
16141618
{},
1619+
undefined,
16151620
true,
16161621
{},
16171622
true,
@@ -1626,6 +1631,15 @@ describe('DebugUtils', () => {
16261631
policyID: '1',
16271632
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
16281633
};
1634+
const transactionViolations = {
1635+
[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}1` as const]: [
1636+
{
1637+
type: CONST.VIOLATION_TYPES.VIOLATION,
1638+
name: CONST.VIOLATIONS.MISSING_CATEGORY,
1639+
showInReview: true,
1640+
},
1641+
],
1642+
};
16291643
await Onyx.multiSet({
16301644
[ONYXKEYS.SESSION]: {
16311645
accountID: 1234,
@@ -1652,7 +1666,7 @@ describe('DebugUtils', () => {
16521666
},
16531667
],
16541668
});
1655-
const {reason} = DebugUtils.getReasonAndReportActionForRBRInLHNRow(report, chatReportR14932, {}, {}, false, {}) ?? {};
1669+
const {reason} = DebugUtils.getReasonAndReportActionForRBRInLHNRow(report, chatReportR14932, {}, {}, transactionViolations, false, {}) ?? {};
16561670
expect(reason).toBe('debug.reasonRBR.hasTransactionThreadViolations');
16571671
});
16581672
});

0 commit comments

Comments
 (0)