Repository navigation
[No QA] Create useTodos hook #76768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[No QA] Create useTodos hook #76768
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6670150
create useTodoHook
luacmartins f2dec15
add loop to identify todos
luacmartins 2fafb2a
update return response
luacmartins b6cf402
refactor selectors
luacmartins 824df72
refactor more code
luacmartins 3bd7a50
refactor code
luacmartins 64c33e3
add search key
luacmartins d410f64
update hook
luacmartins 7a5ec2c
refactor conditions for submit,approve
luacmartins e222c67
update conditionals
luacmartins 17020e7
rm unused imports
luacmartins 7659a7a
Merge branch 'main' into cmartins-todoHook
luacmartins 3b8d1a5
fix lint issues
luacmartins 000d273
fix style
luacmartins 6fe8dc0
add test
luacmartins 1249cda
fix eslint
luacmartins c7b0edb
fix lint
luacmartins e4f2740
rm import/logs
luacmartins bf8ff49
Merge branch 'main' into cmartins-todoHook
luacmartins 70be8ca
address comment
luacmartins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import {useMemo} from 'react'; | ||
| import {isApproveAction, isExportAction, isPrimaryPayAction, isSubmitAction} from '@libs/ReportPrimaryActionUtils'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {Report, Transaction} from '@src/types/onyx'; | ||
| import useOnyx from './useOnyx'; | ||
|
|
||
| export default function useTodos() { | ||
| const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: false}); | ||
| const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false}); | ||
| const [allReportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {canBeMissing: false}); | ||
| const [allTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {canBeMissing: false}); | ||
| const [allReportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS, {canBeMissing: false}); | ||
|
|
||
| return useMemo(() => { | ||
| const reportsToSubmit: Report[] = []; | ||
| const reportsToApprove: Report[] = []; | ||
| const reportsToPay: Report[] = []; | ||
| const reportsToExport: Report[] = []; | ||
|
|
||
| const reports = allReports ? Object.values(allReports) : []; | ||
| if (reports.length === 0) { | ||
| return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; | ||
| } | ||
|
|
||
| const transactionsByReportID: Record<string, Transaction[]> = {}; | ||
| if (allTransactions) { | ||
| for (const transaction of Object.values(allTransactions)) { | ||
| if (!transaction?.reportID) { | ||
| continue; | ||
| } | ||
| (transactionsByReportID[transaction.reportID] ??= []).push(transaction); | ||
| } | ||
| } | ||
|
|
||
| for (const report of reports) { | ||
| if (!report?.reportID || report.type !== CONST.REPORT.TYPE.EXPENSE) { | ||
| continue; | ||
| } | ||
| const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`]; | ||
| const reportNameValuePair = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.chatReportID}`]; | ||
| const reportActions = Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`] ?? []); | ||
| const reportTransactions = transactionsByReportID[report.reportID] ?? []; | ||
|
|
||
| if (isSubmitAction(report, reportTransactions, policy, reportNameValuePair)) { | ||
| reportsToSubmit.push(report); | ||
| } | ||
| if (isApproveAction(report, reportTransactions, policy)) { | ||
| reportsToApprove.push(report); | ||
| } | ||
| if (isPrimaryPayAction(report, policy, reportNameValuePair)) { | ||
| reportsToPay.push(report); | ||
| } | ||
| if (isExportAction(report, policy, reportActions)) { | ||
| reportsToExport.push(report); | ||
| } | ||
| } | ||
|
|
||
| return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; | ||
| }, [allReports, allPolicies, allReportNameValuePairs, allTransactions, allReportActions]); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| import {act, renderHook} from '@testing-library/react-native'; | ||
| import type {OnyxMultiSetInput} from 'react-native-onyx'; | ||
| import Onyx from 'react-native-onyx'; | ||
| import useTodos from '@hooks/useTodos'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {Policy, Report, Transaction} from '@src/types/onyx'; | ||
| import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; | ||
|
|
||
| const CURRENT_USER_ACCOUNT_ID = 1; | ||
| const CURRENT_USER_EMAIL = 'tester@mail.com'; | ||
| const OTHER_USER_ACCOUNT_ID = 2; | ||
|
|
||
| const POLICY_ID = 'policy123'; | ||
| const POLICY_WITH_CONNECTION_ID = 'policy_with_connection'; | ||
|
|
||
| // This keeps the error "@rnmapbox/maps native code not available." from causing the tests to fail | ||
| jest.mock('@components/ConfirmedRoute.tsx'); | ||
|
|
||
| const createMockReport = (reportID: string, overrides: Partial<Report> = {}): Report => | ||
| ({ | ||
| reportID, | ||
| chatReportID: `chat_${reportID}`, | ||
| policyID: POLICY_ID, | ||
| type: CONST.REPORT.TYPE.EXPENSE, | ||
| ownerAccountID: CURRENT_USER_ACCOUNT_ID, | ||
| managerID: OTHER_USER_ACCOUNT_ID, | ||
| stateNum: CONST.REPORT.STATE_NUM.OPEN, | ||
| statusNum: CONST.REPORT.STATUS_NUM.OPEN, | ||
| ...overrides, | ||
| }) as Report; | ||
|
|
||
| const createMockPolicy = (policyID: string, overrides: Partial<Policy> = {}): Policy => | ||
| ({ | ||
| id: policyID, | ||
| type: CONST.POLICY.TYPE.TEAM, | ||
| approvalMode: CONST.POLICY.APPROVAL_MODE.BASIC, | ||
| role: CONST.POLICY.ROLE.USER, | ||
| ...overrides, | ||
| }) as Policy; | ||
|
|
||
| const createMockTransaction = (transactionID: string, reportID: string, overrides: Partial<Transaction> = {}): Transaction => | ||
| ({ | ||
| transactionID, | ||
| reportID, | ||
| amount: 100, | ||
| modifiedAmount: 0, | ||
| reimbursable: true, | ||
| ...overrides, | ||
| }) as Transaction; | ||
|
|
||
| describe('useTodos', () => { | ||
| beforeAll(() => { | ||
| Onyx.init({ | ||
| keys: ONYXKEYS, | ||
| }); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| await Onyx.clear(); | ||
| await waitForBatchedUpdatesWithAct(); | ||
| }); | ||
|
|
||
| describe('categorizes reports correctly', () => { | ||
| // Report IDs for each category | ||
| const SUBMIT_REPORT_IDS = ['submit_1', 'submit_2', 'submit_3', 'submit_4']; | ||
| const APPROVE_REPORT_IDS = ['approve_1', 'approve_2', 'approve_3']; | ||
| const PAY_REPORT_IDS = ['pay_1', 'pay_2']; | ||
| const EXPORT_REPORT_ID = 'export_1'; | ||
| const EXCLUDED_REPORT_IDS = ['excluded_1', 'excluded_2']; | ||
|
|
||
| beforeEach(async () => { | ||
| // Create 4 reports that can be submitted (open, owned by current user, with transactions) | ||
| const reportsToSubmit = SUBMIT_REPORT_IDS.map((id) => | ||
| createMockReport(id, { | ||
| stateNum: CONST.REPORT.STATE_NUM.OPEN, | ||
| statusNum: CONST.REPORT.STATUS_NUM.OPEN, | ||
| ownerAccountID: CURRENT_USER_ACCOUNT_ID, | ||
| }), | ||
| ); | ||
|
|
||
| // Create 3 reports that can be approved (submitted, current user is manager, with transactions) | ||
| const reportsToApprove = APPROVE_REPORT_IDS.map((id) => | ||
| createMockReport(id, { | ||
| stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, | ||
| statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED, | ||
| ownerAccountID: OTHER_USER_ACCOUNT_ID, | ||
| managerID: CURRENT_USER_ACCOUNT_ID, | ||
| }), | ||
| ); | ||
|
|
||
| // Create 2 reports that can be paid (approved, current user is admin/payer, with reimbursable transactions) | ||
| const reportsToPay = PAY_REPORT_IDS.map((id) => | ||
| createMockReport(id, { | ||
| stateNum: CONST.REPORT.STATE_NUM.APPROVED, | ||
| statusNum: CONST.REPORT.STATUS_NUM.APPROVED, | ||
| ownerAccountID: OTHER_USER_ACCOUNT_ID, | ||
| managerID: CURRENT_USER_ACCOUNT_ID, | ||
| total: -100, | ||
| }), | ||
| ); | ||
|
|
||
| // Create 1 report that can be exported: | ||
| // - Approved status | ||
| // - User is admin | ||
| // - Policy has a valid accounting connection with auto-sync disabled | ||
| // - Not waiting on bank account | ||
| const reportToExport = createMockReport(EXPORT_REPORT_ID, { | ||
| policyID: POLICY_WITH_CONNECTION_ID, | ||
| stateNum: CONST.REPORT.STATE_NUM.APPROVED, | ||
| statusNum: CONST.REPORT.STATUS_NUM.APPROVED, | ||
| ownerAccountID: OTHER_USER_ACCOUNT_ID, | ||
| isWaitingOnBankAccount: false, | ||
| }); | ||
|
|
||
| // Create 2 reports that don't fit any condition: | ||
| // 1. A chat report (not expense type) | ||
| // 2. An expense report owned by another user that's not submitted (can't submit, approve, pay, or export) | ||
| const excludedReports = [ | ||
| createMockReport(EXCLUDED_REPORT_IDS.at(0) ?? '', { | ||
| type: CONST.REPORT.TYPE.CHAT, // Not an expense report | ||
| }), | ||
| createMockReport(EXCLUDED_REPORT_IDS.at(1) ?? '', { | ||
| stateNum: CONST.REPORT.STATE_NUM.OPEN, | ||
| statusNum: CONST.REPORT.STATUS_NUM.OPEN, | ||
| ownerAccountID: OTHER_USER_ACCOUNT_ID, // Not owned by current user, so can't submit | ||
| managerID: OTHER_USER_ACCOUNT_ID, // Not managed by current user, so can't approve | ||
| }), | ||
| ]; | ||
|
|
||
| // Create main policy (for submit, approve, pay reports) | ||
| const policy = createMockPolicy(POLICY_ID, { | ||
| approvalMode: CONST.POLICY.APPROVAL_MODE.BASIC, | ||
| role: CONST.POLICY.ROLE.ADMIN, | ||
| ownerAccountID: CURRENT_USER_ACCOUNT_ID, | ||
| reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES, | ||
| }); | ||
|
|
||
| // Create policy with accounting connection (for export report) | ||
| // Cast to Policy to avoid strict type checking on connection config | ||
| const policyWithConnection = { | ||
| ...createMockPolicy(POLICY_WITH_CONNECTION_ID, { | ||
| role: CONST.POLICY.ROLE.ADMIN, | ||
| }), | ||
| connections: { | ||
| // QuickBooks Online connection with auto-sync disabled | ||
| [CONST.POLICY.CONNECTIONS.NAME.QBO]: { | ||
| lastSync: { | ||
| isConnected: true, | ||
| isSuccessful: true, | ||
| isAuthenticationError: false, | ||
| source: 'DIRECT', | ||
| }, | ||
| config: { | ||
| autoSync: { | ||
| jobID: 'job123', | ||
| enabled: false, // Auto-sync disabled so manual export is available | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } as Policy; | ||
|
|
||
| // Create transactions for each report that needs them | ||
| const transactions: Record<string, Transaction> = {}; | ||
|
|
||
| // Transactions for submit reports | ||
| for (const reportID of SUBMIT_REPORT_IDS) { | ||
| const transactionID = `trans_submit_${reportID}`; | ||
| transactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = createMockTransaction(transactionID, reportID); | ||
| } | ||
|
|
||
| // Transactions for approve reports | ||
| for (const reportID of APPROVE_REPORT_IDS) { | ||
| const transactionID = `trans_approve_${reportID}`; | ||
| transactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = createMockTransaction(transactionID, reportID); | ||
| } | ||
|
|
||
| // Transactions for pay reports (must be reimbursable) | ||
| for (const reportID of PAY_REPORT_IDS) { | ||
| const transactionID = `trans_pay_${reportID}`; | ||
| transactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = createMockTransaction(transactionID, reportID, { | ||
| reimbursable: true, | ||
| }); | ||
| } | ||
|
|
||
| // Build reports object | ||
| const reports: Record<string, Report> = {}; | ||
| for (const report of [...reportsToSubmit, ...reportsToApprove, ...reportsToPay, reportToExport, ...excludedReports]) { | ||
| reports[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = report; | ||
| } | ||
|
|
||
| await act(async () => { | ||
| await Onyx.multiSet({ | ||
| [ONYXKEYS.SESSION]: { | ||
| email: CURRENT_USER_EMAIL, | ||
| accountID: CURRENT_USER_ACCOUNT_ID, | ||
| }, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`]: policy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}${POLICY_WITH_CONNECTION_ID}`]: policyWithConnection, | ||
| ...reports, | ||
| ...transactions, | ||
| } as unknown as OnyxMultiSetInput); | ||
| await waitForBatchedUpdatesWithAct(); | ||
| }); | ||
| }); | ||
|
|
||
| it('returns correct number of reports for each category', async () => { | ||
| const {result} = renderHook(() => useTodos()); | ||
| await waitForBatchedUpdatesWithAct(); | ||
|
|
||
| expect(result.current.reportsToSubmit.length).toBe(4); | ||
| expect(result.current.reportsToApprove.length).toBe(3); | ||
| expect(result.current.reportsToPay.length).toBe(2); | ||
| expect(result.current.reportsToExport.length).toBe(1); | ||
|
|
||
| for (const id of SUBMIT_REPORT_IDS) { | ||
| expect(result.current.reportsToSubmit.map((r) => r.reportID)).toContain(id); | ||
| } | ||
| for (const id of APPROVE_REPORT_IDS) { | ||
| expect(result.current.reportsToApprove.map((r) => r.reportID)).toContain(id); | ||
| } | ||
| for (const id of PAY_REPORT_IDS) { | ||
| expect(result.current.reportsToPay.map((r) => r.reportID)).toContain(id); | ||
| } | ||
| expect(result.current.reportsToExport.map((r) => r.reportID)).toContain(EXPORT_REPORT_ID); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coming from #85861,
useTodoscounted payable reports viaisPrimaryPayActionwithout filtering held expenses.SearchUIUtils.getActions()already excludes those with!hasHeldExpenses, so Home showed phantom "Pay N Reports" counts.Fixed in #92499.