diff --git a/src/components/MoneyRequestReportView/SelectionToolbar/index.tsx b/src/components/MoneyRequestReportView/SelectionToolbar/index.tsx index 326535b4fdac..78bcf1fdfca0 100644 --- a/src/components/MoneyRequestReportView/SelectionToolbar/index.tsx +++ b/src/components/MoneyRequestReportView/SelectionToolbar/index.tsx @@ -1,4 +1,3 @@ -import DecisionModal from '@components/DecisionModal'; import {useDelegateNoAccessActions, useDelegateNoAccessState} from '@components/DelegateNoAccessModalProvider'; import HoldOrRejectEducationalModal from '@components/HoldOrRejectEducationalModal'; import {ModalActions} from '@components/Modal/Global/ModalContext'; @@ -10,6 +9,7 @@ import BulkDuplicateHandler from '@components/Search/BulkDuplicateHandler'; import {useSearchSelectionActions, useSearchSelectionContext} from '@components/Search/SearchContext'; import useConfirmModal from '@hooks/useConfirmModal'; +import useDecisionModal from '@hooks/useDecisionModal'; import useFilterSelectedTransactions from '@hooks/useFilterSelectedTransactions'; import useLocalize from '@hooks/useLocalize'; import useMobileSelectionMode from '@hooks/useMobileSelectionMode'; @@ -91,9 +91,24 @@ function SelectionToolbar({reportID, transactions, reportActions}: SelectionTool const isMobileSelectionModeEnabled = useMobileSelectionMode(); const {showConfirmModal} = useConfirmModal(); + const {showDecisionModal} = useDecisionModal(); + + const showOfflineModal = () => { + showDecisionModal({ + title: translate('common.youAppearToBeOffline'), + prompt: translate('common.offlinePrompt'), + secondOptionText: translate('common.buttonConfirm'), + }); + }; + + const showDownloadErrorModal = () => { + showDecisionModal({ + title: translate('common.downloadFailedTitle'), + prompt: translate('common.downloadFailedDescription'), + secondOptionText: translate('common.buttonConfirm'), + }); + }; - const [offlineModalVisible, setOfflineModalVisible] = useState(false); - const [isDownloadErrorModalVisible, setIsDownloadErrorModalVisible] = useState(false); const [rejectModalAction, setRejectModalAction] = useState | null>(null); const transactionsWithoutPendingDelete = transactions.filter((t) => !isTransactionPendingDelete(t)); @@ -102,7 +117,7 @@ function SelectionToolbar({reportID, transactions, reportActions}: SelectionTool const beginExportWithTemplate = (templateName: string, templateType: string, transactionIDList: string[], exportName: string) => { if (isOffline) { - setOfflineModalVisible(true); + showOfflineModal(); return; } @@ -164,8 +179,8 @@ function SelectionToolbar({reportID, transactions, reportActions}: SelectionTool reportActions, allTransactionsLength: transactions.length, session, - onExportFailed: () => setIsDownloadErrorModalVisible(true), - onExportOffline: () => setOfflineModalVisible(true), + onExportFailed: showDownloadErrorModal, + onExportOffline: showOfflineModal, policy, beginExportWithTemplate: (templateName, templateType, transactionIDList, exportName) => beginExportWithTemplate(templateName, templateType, transactionIDList, exportName), onDeleteSelected, @@ -306,24 +321,6 @@ function SelectionToolbar({reportID, transactions, reportActions}: SelectionTool onClearSelection={() => clearSelectedTransactions(true)} /> )} - setIsDownloadErrorModalVisible(false)} - secondOptionText={translate('common.buttonConfirm')} - isVisible={isDownloadErrorModalVisible} - onClose={() => setIsDownloadErrorModalVisible(false)} - /> - setOfflineModalVisible(false)} - secondOptionText={translate('common.buttonConfirm')} - isVisible={offlineModalVisible} - onClose={() => setOfflineModalVisible(false)} - /> {!!rejectModalAction && ( (), selectedReports, areAllMatchingItemsSelected} = useSearchSelectionContext(); const {clearSelectedTransactions} = useSearchSelectionActions(); const {currentSearchResults} = useSearchResultsContext(); @@ -80,20 +77,13 @@ function SearchBulkActionsButton({queryJSON}: SearchBulkActionsButtonProps) { selectedReportIDs, businessBankAccountOptions, confirmPayment, - isOfflineModalVisible, - isDownloadErrorModalVisible, isHoldEducationalModalVisible, areAllTransactionsFromDMReports, rejectModalAction, - emptyReportsCount, - handleOfflineModalClose, - handleDownloadErrorModalClose, isExpensifyCardStatementPDFModalVisible, setIsExpensifyCardStatementPDFModalVisible, expensifyCardStatementPDFParams, handleExpensifyCardStatementPDFModalHide, - isExpensifyCardStatementMultiFeedAlertVisible, - handleExpensifyCardStatementMultiFeedAlertClose, dismissModalAndUpdateUseHold, dismissRejectModalBasedOnAction, isDuplicateOptionVisible, @@ -273,24 +263,6 @@ function SearchBulkActionsButton({queryJSON}: SearchBulkActionsButtonProps) { ) } - - {!!expensifyCardStatementPDFParams && ( )} - {!!rejectModalAction && ( (undefined); - const [isExpensifyCardStatementMultiFeedAlertVisible, setIsExpensifyCardStatementMultiFeedAlertVisible] = useState(false); const {showConfirmModal} = useConfirmModal(); + const {showDecisionModal} = useDecisionModal(); + + const showOfflineModal = useCallback(() => { + showDecisionModal({ + title: translate('common.youAppearToBeOffline'), + prompt: translate('common.offlinePrompt'), + secondOptionText: translate('common.buttonConfirm'), + }); + }, [showDecisionModal, translate]); + + const showDownloadErrorModal = useCallback(() => { + showDecisionModal({ + title: translate('common.downloadFailedTitle'), + prompt: translate('common.downloadFailedDescription'), + secondOptionText: translate('common.buttonConfirm'), + }); + }, [showDecisionModal, translate]); + + const showEmptyReportsDownloadErrorModal = useCallback( + (count: number) => { + showDecisionModal({ + title: translate('common.downloadFailedTitle'), + prompt: translate('common.downloadFailedEmptyReportDescription', {count}), + secondOptionText: translate('common.buttonConfirm'), + }); + }, + [showDecisionModal, translate], + ); + + const showExpensifyCardStatementMultiFeedAlert = useCallback(() => { + showDecisionModal({ + title: translate('search.expensifyCardStatementPDF.title'), + prompt: translate('search.expensifyCardStatementPDF.oneFeedAtATime'), + secondOptionText: translate('common.buttonConfirm'), + }); + }, [showDecisionModal, translate]); + const openSearchReportSubmitToPopover = useOpenSearchReportSubmitToPopover(); const {showReportPDFDownloadModal} = useReportPDFDownloadModal(); const [isHoldEducationalModalVisible, setIsHoldEducationalModalVisible] = useState(false); @@ -683,8 +718,6 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { typeof CONST.REPORT.TRANSACTION_SECONDARY_ACTIONS.HOLD | typeof CONST.REPORT.TRANSACTION_SECONDARY_ACTIONS.REJECT > | null>(null); - const [emptyReportsCount, setEmptyReportsCount] = useState(0); - const [dismissedRejectUseExplanation] = useOnyx(ONYXKEYS.NVP_DISMISSED_REJECT_USE_EXPLANATION); const [dismissedHoldUseExplanation] = useOnyx(ONYXKEYS.NVP_DISMISSED_HOLD_USE_EXPLANATION); const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector}); @@ -935,7 +968,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { const exportExpensifyCardStatementPDF = useCallback(() => { if (isOffline) { - setIsOfflineModalVisible(true); + showOfflineModal(); return; } @@ -945,7 +978,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { } if (selection.hasMultipleFeeds) { - setIsExpensifyCardStatementMultiFeedAlertVisible(true); + showExpensifyCardStatementMultiFeedAlert(); return; } @@ -965,7 +998,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { } setIsExpensifyCardStatementPDFModalVisible(false); setExpensifyCardStatementPDFParams(undefined); - setIsDownloadErrorModalVisible(true); + showDownloadErrorModal(); }; setExpensifyCardStatementPDFParams(statementParams); @@ -989,7 +1022,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { setExpensifyCardStatementPDFParams((currentParams) => (currentParams ? {...currentParams, statementKey} : currentParams)); }) .catch(showStatementError); - }, [isOffline]); + }, [isOffline, showOfflineModal, showExpensifyCardStatementMultiFeedAlert, showDownloadErrorModal]); const firstTransactionID = selectedTransactionsKeys.at(0); const firstTransaction = (firstTransactionID ? currentSearchResults?.data?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${firstTransactionID}`] : undefined) ?? @@ -1037,18 +1070,17 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { const beginExportWithTemplate = useCallback( (templateName: string, templateType: string, policyID: string | undefined, exportName: string) => { if (hasOnlyEmptyReports) { - setEmptyReportsCount(emptyReports.length); - setIsDownloadErrorModalVisible(true); + showEmptyReportsDownloadErrorModal(emptyReports.length); return; } if (isOffline) { - setIsOfflineModalVisible(true); + showOfflineModal(); return; } if (areAllMatchingItemsSelected) { const allMatchingQueryJSON = queryJSON ? getUngroupedTemplateExportQuery(queryJSON) : undefined; if (queryJSON && !allMatchingQueryJSON) { - setIsDownloadErrorModalVisible(true); + showDownloadErrorModal(); return; } queueExportSearchWithTemplate( @@ -1069,7 +1101,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { const {isGroupExport, transactionIDList} = getGroupExportScope(queryJSON, selectedTransactions); const groupExportQueryJSON = isGroupExport && queryJSON ? getTemplateGroupExportQuery(queryJSON, selectedTransactions, currentSearchResults?.data) : undefined; if (isGroupExport && !groupExportQueryJSON) { - setIsDownloadErrorModalVisible(true); + showDownloadErrorModal(); return; } queueExportSearchWithTemplate( @@ -1099,6 +1131,9 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { emptyReports.length, selectedTransactions, isOffline, + showOfflineModal, + showDownloadErrorModal, + showEmptyReportsDownloadErrorModal, areAllMatchingItemsSelected, currentSearchResults?.data, currentSearchKey, @@ -1200,12 +1235,11 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { const handleCSVExport = useCallback( async (isBasicExport: boolean) => { if (hasOnlyEmptyReports) { - setEmptyReportsCount(emptyReports.length); - setIsDownloadErrorModalVisible(true); + showEmptyReportsDownloadErrorModal(emptyReports.length); return; } if (isOffline) { - setIsOfflineModalVisible(true); + showOfflineModal(); return; } @@ -1219,7 +1253,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { const allMatchingExportData = isExpenseType && queryJSON ? getAllMatchingExportQueryAndExclusions(queryJSON, excludedTransactions, currentSearchResults?.data) : undefined; const allMatchingReportExportQuery = isExpenseReportType && queryJSON ? getAllMatchingReportQuery(queryJSON, excludedTransactions) : undefined; if ((isExpenseType && !allMatchingExportData) || (isExpenseReportType && !allMatchingReportExportQuery)) { - setIsDownloadErrorModalVisible(true); + showDownloadErrorModal(); return; } const reportIDList = selectedReports?.map((report) => report?.reportID).filter((reportID) => reportID !== undefined) ?? []; @@ -1258,8 +1292,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { }, () => { didFail = true; - setEmptyReportsCount(0); - setIsDownloadErrorModalVisible(true); + showDownloadErrorModal(); }, translate, allReportsTransactionsAndViolations, @@ -1272,6 +1305,9 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { hasOnlyEmptyReports, emptyReports.length, isOffline, + showOfflineModal, + showDownloadErrorModal, + showEmptyReportsDownloadErrorModal, areAllMatchingItemsSelected, queryJSON, selectedReports, @@ -1297,7 +1333,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { const handleApproveWithDEWCheck = useCallback(async () => { if (isOffline) { - setIsOfflineModalVisible(true); + showOfflineModal(); return; } @@ -1376,6 +1412,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { } }, [ isOffline, + showOfflineModal, isDelegateAccessRestricted, showDelegateNoAccessModal, selectedReports, @@ -1567,7 +1604,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { if (isOffline) { // On iOS, presenting the offline modal while the payment popover is still dismissing freezes the app, // so defer it until the popover transition completes (matching the delegate/locked-account guards). - deferModalPresentationAfterPopoverDismiss(() => setIsOfflineModalVisible(true)); + deferModalPresentationAfterPopoverDismiss(() => showOfflineModal()); return; } @@ -1844,6 +1881,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { }, [ hash, + showOfflineModal, areAllMatchingItemsSelected, queryJSON, excludedTransactions, @@ -2355,7 +2393,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { return; } if (isOffline) { - setIsOfflineModalVisible(true); + showOfflineModal(); return; } clearSelectedTransactions(); @@ -2535,7 +2573,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { onSelected: () => { // A queued all-matching move would send a stale query, so ask the user to reconnect like export does if (areAllMatchingItemsSelected && isOffline) { - setIsOfflineModalVisible(true); + showOfflineModal(); return; } Navigation.navigate(ROUTES.MOVE_TRANSACTIONS_SEARCH_RHP.getRoute()); @@ -2567,7 +2605,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { shouldCloseModalOnSelect: true, onSelected: async () => { if (isOffline) { - setIsOfflineModalVisible(true); + showOfflineModal(); return; } // In "Select all" mode the matching reports aren't enumerated on the client (results are paged), @@ -2576,7 +2614,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { if (areAllMatchingItemsSelected) { const allMatchingReportQuery = queryJSON ? getAllMatchingReportQuery(queryJSON, excludedTransactions) : undefined; if (!allMatchingReportQuery) { - setIsDownloadErrorModalVisible(true); + showDownloadErrorModal(); return; } const serializedQuery = serializeQueryJSONForBackend({...allMatchingReportQuery, searchKey: currentSearchKey}); @@ -2729,7 +2767,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { shouldCloseModalOnSelect: true, onSelected: () => { if (isOffline) { - setIsOfflineModalVisible(true); + showOfflineModal(); return; } @@ -2789,7 +2827,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { shouldCloseModalOnSelect: true, onSelected: () => { if (isOffline) { - setIsOfflineModalVisible(true); + showOfflineModal(); return; } @@ -3029,7 +3067,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { shouldCloseModalOnSelect: true, onSelected: () => { if (isOffline) { - setIsOfflineModalVisible(true); + showOfflineModal(); return; } exportReceiptsToZip({reportIDs: selectedReportIDs}); @@ -3059,7 +3097,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { shouldCloseModalOnSelect: true, onSelected: () => { if (isOffline) { - setIsOfflineModalVisible(true); + showOfflineModal(); return; } exportReceiptsToZip({transactionIDs}); @@ -3094,7 +3132,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { shouldCloseModalOnSelect: true, onSelected: () => { if (isOffline) { - setIsOfflineModalVisible(true); + showOfflineModal(); return; } @@ -3290,6 +3328,8 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { return buildResult(options); }, [ selectedTransactionsKeys, + showOfflineModal, + showDownloadErrorModal, cardList, hash, selectedTransactions, @@ -3398,14 +3438,6 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { dropdownButtonsOptions.length > 0 && dropdownButtonsOptions.every((option) => option.value === CONST.SEARCH.BULK_ACTION_TYPES.EXPORT && !option.subMenuItems); const bulkActionsMenuHeaderText = isShowingExportOptionsDirectly ? translate('common.export') : undefined; - const handleOfflineModalClose = useCallback(() => { - setIsOfflineModalVisible(false); - }, [setIsOfflineModalVisible]); - - const handleDownloadErrorModalClose = useCallback(() => { - setIsDownloadErrorModalVisible(false); - }, [setIsDownloadErrorModalVisible]); - const handleExpensifyCardStatementPDFModalHide = useCallback(() => { setExpensifyCardStatementPDFParams(undefined); // Clear the selection when the statement modal closes (after download or failure), like the other bulk @@ -3414,10 +3446,6 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { clearSelectedTransactions(); }, [clearSelectedTransactions]); - const handleExpensifyCardStatementMultiFeedAlertClose = useCallback(() => { - setIsExpensifyCardStatementMultiFeedAlertVisible(false); - }, []); - const dismissModalAndUpdateUseHold = useCallback(() => { setIsHoldEducationalModalVisible(false); setNameValuePair(ONYXKEYS.NVP_DISMISSED_HOLD_USE_EXPLANATION, true, false, !isOffline); @@ -3448,20 +3476,13 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { selectedReportIDs, businessBankAccountOptions, confirmPayment: stableOnBulkPaySelected, - isOfflineModalVisible, - isDownloadErrorModalVisible, isHoldEducationalModalVisible, areAllTransactionsFromDMReports, rejectModalAction, - emptyReportsCount, - handleOfflineModalClose, - handleDownloadErrorModalClose, isExpensifyCardStatementPDFModalVisible, setIsExpensifyCardStatementPDFModalVisible, expensifyCardStatementPDFParams, handleExpensifyCardStatementPDFModalHide, - isExpensifyCardStatementMultiFeedAlertVisible, - handleExpensifyCardStatementMultiFeedAlertClose, dismissModalAndUpdateUseHold, dismissRejectModalBasedOnAction, isDuplicateOptionVisible, diff --git a/src/hooks/useSelectedTransactionsActions.ts b/src/hooks/useSelectedTransactionsActions.ts index bd88e40df03f..904ab27f5107 100644 --- a/src/hooks/useSelectedTransactionsActions.ts +++ b/src/hooks/useSelectedTransactionsActions.ts @@ -466,6 +466,7 @@ function useSelectedTransactionsActions({ clearSelectedTransactions(true); } : () => beginExportWithTemplate(template.templateName, template.type, selectedTransactionIDs, template.name, template.policyID), + shouldCallAfterModalHide: true, addSeparatorBefore, }; }; diff --git a/tests/unit/Search/SearchBulkActionsButtonTest.tsx b/tests/unit/Search/SearchBulkActionsButtonTest.tsx index 5908379c751f..12ef7024f21c 100644 --- a/tests/unit/Search/SearchBulkActionsButtonTest.tsx +++ b/tests/unit/Search/SearchBulkActionsButtonTest.tsx @@ -39,7 +39,6 @@ jest.mock('@components/ButtonWithDropdownMenu', () => ({ __esModule: true, default: (props: MockButtonProps) => mockButtonWithDropdownMenu(props), })); -jest.mock('@components/DecisionModal', () => () => null); jest.mock('@components/HoldOrRejectEducationalModal', () => () => null); jest.mock('@components/HoldSubmitterEducationalModal', () => () => null); jest.mock('@components/ReportPDFDownloadModal', () => () => null); @@ -78,7 +77,6 @@ jest.mock('@hooks/useSearchBulkActions', () => ({ selectedTransactionReportIDs: [], selectedReportIDs: [], businessBankAccountOptions: [], - emptyReportsCount: 0, isDuplicateOptionVisible: false, isDuplicateReportOptionVisible: false, allTransactions: {}, diff --git a/tests/unit/hooks/useSearchBulkActionsDownloadPDFTest.ts b/tests/unit/hooks/useSearchBulkActionsDownloadPDFTest.ts index 1b59af832309..24ead3724068 100644 --- a/tests/unit/hooks/useSearchBulkActionsDownloadPDFTest.ts +++ b/tests/unit/hooks/useSearchBulkActionsDownloadPDFTest.ts @@ -40,6 +40,12 @@ jest.mock('@hooks/useNetwork', () => ({ default: () => ({isOffline: mockIsOffline}), })); +const mockShowDecisionModal = jest.fn(); +jest.mock('@hooks/useDecisionModal', () => ({ + __esModule: true, + default: () => ({showDecisionModal: mockShowDecisionModal}), +})); + jest.mock('@hooks/useEnvironment', () => ({ __esModule: true, default: () => ({isProduction: false, isDevelopment: true, environment: 'development'}), @@ -587,7 +593,7 @@ describe('useSearchBulkActions - Download report', () => { // Then no incomplete export is started and the existing download error is shown expect(exportReportsToPDF).not.toHaveBeenCalled(); - expect(result.current.isDownloadErrorModalVisible).toBe(true); + expect(mockShowDecisionModal).toHaveBeenCalledWith(expect.objectContaining({title: 'common.downloadFailedTitle', prompt: 'common.downloadFailedDescription'})); }); it('should show Export as PDF for selected Expensify Card settlement groups', async () => { @@ -666,7 +672,7 @@ describe('useSearchBulkActions - Download report', () => { // The loading modal is dismissed and the download-error modal is shown instead of hanging on "waiting". await waitFor(() => { - expect(result.current.isDownloadErrorModalVisible).toBe(true); + expect(mockShowDecisionModal).toHaveBeenCalledWith(expect.objectContaining({title: 'common.downloadFailedTitle', prompt: 'common.downloadFailedDescription'})); }); expect(result.current.isExpensifyCardStatementPDFModalVisible).toBe(false); expect(result.current.expensifyCardStatementPDFParams).toBeUndefined(); @@ -718,7 +724,7 @@ describe('useSearchBulkActions - Download report', () => { await Promise.resolve(); }); - expect(result.current.isDownloadErrorModalVisible).toBe(false); + expect(mockShowDecisionModal).not.toHaveBeenCalled(); expect(result.current.isExpensifyCardStatementPDFModalVisible).toBe(true); expect(result.current.expensifyCardStatementPDFParams?.entryIDs).toEqual([456]); }); @@ -748,7 +754,9 @@ describe('useSearchBulkActions - Download report', () => { }); expect(getExpensifyCardStatementPDF).not.toHaveBeenCalled(); - expect(result.current.isExpensifyCardStatementMultiFeedAlertVisible).toBe(true); + expect(mockShowDecisionModal).toHaveBeenCalledWith( + expect.objectContaining({title: 'search.expensifyCardStatementPDF.title', prompt: 'search.expensifyCardStatementPDF.oneFeedAtATime'}), + ); }); it('should export a cross-workspace settlement as the whole settlement (no policyID)', async () => { diff --git a/tests/unit/hooks/useSearchBulkActionsDownloadReceiptsTest.ts b/tests/unit/hooks/useSearchBulkActionsDownloadReceiptsTest.ts index e7559784eb1a..06b4f72d9263 100644 --- a/tests/unit/hooks/useSearchBulkActionsDownloadReceiptsTest.ts +++ b/tests/unit/hooks/useSearchBulkActionsDownloadReceiptsTest.ts @@ -31,6 +31,12 @@ jest.mock('@hooks/useNetwork', () => ({ default: () => ({isOffline: mockIsOffline}), })); +const mockShowDecisionModal = jest.fn(); +jest.mock('@hooks/useDecisionModal', () => ({ + __esModule: true, + default: () => ({showDecisionModal: mockShowDecisionModal}), +})); + jest.mock('@hooks/useEnvironment', () => ({ __esModule: true, default: () => ({isProduction: false, isDevelopment: true, environment: 'development'}), @@ -344,7 +350,7 @@ describe('useSearchBulkActions - Download receipts', () => { }); expect(exportReceiptsToZip).not.toHaveBeenCalled(); - expect(result.current.isOfflineModalVisible).toBe(true); + expect(mockShowDecisionModal).toHaveBeenCalledWith(expect.objectContaining({title: 'common.youAppearToBeOffline', prompt: 'common.offlinePrompt'})); }); }); @@ -451,7 +457,7 @@ describe('useSearchBulkActions - Download receipts', () => { }); expect(exportReceiptsToZip).not.toHaveBeenCalled(); - expect(result.current.isOfflineModalVisible).toBe(true); + expect(mockShowDecisionModal).toHaveBeenCalledWith(expect.objectContaining({title: 'common.youAppearToBeOffline', prompt: 'common.offlinePrompt'})); }); }); }); diff --git a/tests/unit/hooks/useSearchBulkActionsExportTest.ts b/tests/unit/hooks/useSearchBulkActionsExportTest.ts index d3b6991c18d5..0e948567ee1e 100644 --- a/tests/unit/hooks/useSearchBulkActionsExportTest.ts +++ b/tests/unit/hooks/useSearchBulkActionsExportTest.ts @@ -151,6 +151,12 @@ jest.mock('@hooks/useNetwork', () => ({ default: () => ({isOffline: mockIsOffline}), })); +const mockShowDecisionModal = jest.fn(); +jest.mock('@hooks/useDecisionModal', () => ({ + __esModule: true, + default: () => ({showDecisionModal: mockShowDecisionModal}), +})); + jest.mock('@hooks/useEnvironment', () => ({ __esModule: true, default: () => ({isProduction: true, isDevelopment: false, environment: 'production'}), @@ -1379,12 +1385,34 @@ describe('useSearchBulkActions - export options', () => { // Then: the offline modal opens and nothing is queued, matching bulk pay's offline behavior. await waitFor(() => { - expect(result.current.isOfflineModalVisible).toBe(true); + expect(mockShowDecisionModal).toHaveBeenCalledWith(expect.objectContaining({title: 'common.youAppearToBeOffline', prompt: 'common.offlinePrompt'})); }); expect(queueBulkMarkAsExported).not.toHaveBeenCalled(); expect(mockClearSelectedTransactions).not.toHaveBeenCalled(); }); + it('shows the empty-report download error with the empty report count when every selected report has no expenses', async () => { + // Given a selected report that the search snapshot says has no expenses, so there is nothing to export + mockCurrentSearchResults = makeSearchResults([{...makeSnapshotReport(), transactionCount: 0}]); + mockSelectedReports = [makeSelectedReport()]; + mockSelectedTransactions = {tx1: makeSelectedTransaction()}; + + const {result} = renderHook(() => useSearchBulkActions({queryJSON: expenseReportQueryJSON}), {wrapper: OnyxListItemProvider}); + + await waitFor(() => { + expect(getExportOptionByText(result.current.headerButtonsOptions, 'export.currentView')).toBeDefined(); + }); + + // When the user runs the Current view CSV export + getExportOptionByText(result.current.headerButtonsOptions, 'export.currentView')?.onSelected?.(); + + // Then the download error explains the reports are empty, because the prompt is passed when the modal is shown rather than read from shared state + await waitFor(() => { + expect(mockShowDecisionModal).toHaveBeenCalledWith(expect.objectContaining({title: 'common.downloadFailedTitle', prompt: 'common.downloadFailedEmptyReportDescription'})); + }); + expect(exportSearchItemsToCSV).not.toHaveBeenCalled(); + }); + it('marks the specific selected report IDs, not the search query, for a limited (non-select-all) selection', async () => { // Given: a finite selection of specific reports ("Select all" is NOT checked). mockAreAllMatchingItemsSelected = false; @@ -1701,7 +1729,7 @@ describe('useSearchBulkActions - export options', () => { // Then nothing is exported, because dropping groupBy without a cardID filter would export every card in the search await waitFor(() => { - expect(result.current.isDownloadErrorModalVisible).toBe(true); + expect(mockShowDecisionModal).toHaveBeenCalledWith(expect.objectContaining({title: 'common.downloadFailedTitle', prompt: 'common.downloadFailedDescription'})); }); expect(queueExportSearchWithTemplate).not.toHaveBeenCalled(); }); diff --git a/tests/unit/hooks/useSearchBulkActionsPayTest.ts b/tests/unit/hooks/useSearchBulkActionsPayTest.ts index 01a6d3bf7cc6..42039125021b 100644 --- a/tests/unit/hooks/useSearchBulkActionsPayTest.ts +++ b/tests/unit/hooks/useSearchBulkActionsPayTest.ts @@ -124,6 +124,12 @@ jest.mock('@hooks/useNetwork', () => ({ default: () => ({isOffline: mockIsOffline}), })); +const mockShowDecisionModal = jest.fn(); +jest.mock('@hooks/useDecisionModal', () => ({ + __esModule: true, + default: () => ({showDecisionModal: mockShowDecisionModal}), +})); + jest.mock('@hooks/useEnvironment', () => ({ __esModule: true, default: () => ({isProduction: false, isDevelopment: true, environment: 'development'}), @@ -338,7 +344,7 @@ describe('useSearchBulkActions - Pay option', () => { }); // Then the offline modal should open and no payment should be triggered because payments must not be queued while offline - expect(result.current.isOfflineModalVisible).toBe(true); + expect(mockShowDecisionModal).toHaveBeenCalledWith(expect.objectContaining({title: 'common.youAppearToBeOffline', prompt: 'common.offlinePrompt'})); expect(payMoneyRequest).not.toHaveBeenCalled(); }); diff --git a/tests/unit/hooks/useSearchBulkActionsTest.ts b/tests/unit/hooks/useSearchBulkActionsTest.ts index 369ca3dfc0d2..dd268cc6068a 100644 --- a/tests/unit/hooks/useSearchBulkActionsTest.ts +++ b/tests/unit/hooks/useSearchBulkActionsTest.ts @@ -106,6 +106,12 @@ jest.mock('@hooks/useNetwork', () => ({ default: () => ({isOffline: mockIsOffline}), })); +const mockShowDecisionModal = jest.fn(); +jest.mock('@hooks/useDecisionModal', () => ({ + __esModule: true, + default: () => ({showDecisionModal: mockShowDecisionModal}), +})); + jest.mock('@hooks/useEnvironment', () => ({ __esModule: true, default: () => ({isProduction: false, isDevelopment: true, environment: 'development'}), @@ -658,7 +664,7 @@ describe('useSearchBulkActions - CSV export flow', () => { expect(mockUnholdRequest).toHaveBeenCalledTimes(2); expect(mockUnholdRequest).toHaveBeenCalledWith(expect.objectContaining({transactionID: 'tx1', reportID: 'childReport1', isOffline: true})); expect(mockUnholdRequest).toHaveBeenCalledWith(expect.objectContaining({transactionID: 'tx2', reportID: 'childReport2', isOffline: true})); - expect(result.current.isOfflineModalVisible).toBe(false); + expect(mockShowDecisionModal).not.toHaveBeenCalled(); }); }); });