From a68649b73d077a1a6d7c8f0dc18bca6c329ff253 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A1bio=20Henriques?= <contact@fabiohenriques.dev>
Date: Mon, 2 Jun 2025 19:14:55 +0100
Subject: [PATCH] Fix endless spinner in TestDrive modal when using a boss
 email that was used before

---
 .../Modal/EmployeeTestDriveModal.tsx          | 69 ++++++++++---------
 1 file changed, 37 insertions(+), 32 deletions(-)

diff --git a/src/components/TestDrive/Modal/EmployeeTestDriveModal.tsx b/src/components/TestDrive/Modal/EmployeeTestDriveModal.tsx
index f4971106d6e0..c75d3e754311 100644
--- a/src/components/TestDrive/Modal/EmployeeTestDriveModal.tsx
+++ b/src/components/TestDrive/Modal/EmployeeTestDriveModal.tsx
@@ -48,42 +48,47 @@ function EmployeeTestDriveModal() {
 
         setIsLoading(true);
 
-        verifyTestDriveRecipient(bossEmail).then(() => {
-            setTestReceipt(
-                TestReceipt,
-                'jpg',
-                (source, _, filename) => {
-                    const transactionID = CONST.IOU.OPTIMISTIC_TRANSACTION_ID;
-                    const reportID = generateReportID();
-                    initMoneyRequest(reportID, undefined, false, undefined, CONST.IOU.REQUEST_TYPE.SCAN);
+        verifyTestDriveRecipient(bossEmail)
+            .then(() => {
+                setTestReceipt(
+                    TestReceipt,
+                    'jpg',
+                    (source, _, filename) => {
+                        const transactionID = CONST.IOU.OPTIMISTIC_TRANSACTION_ID;
+                        const reportID = generateReportID();
+                        initMoneyRequest(reportID, undefined, false, undefined, CONST.IOU.REQUEST_TYPE.SCAN);
 
-                    setMoneyRequestReceipt(transactionID, source, filename, true, CONST.TEST_RECEIPT.FILE_TYPE, false, true);
+                        setMoneyRequestReceipt(transactionID, source, filename, true, CONST.TEST_RECEIPT.FILE_TYPE, false, true);
 
-                    setMoneyRequestParticipants(transactionID, [
-                        {
-                            accountID: generateAccountID(bossEmail),
-                            login: bossEmail,
-                            displayName: bossEmail,
-                            selected: true,
-                        },
-                    ]);
+                        setMoneyRequestParticipants(transactionID, [
+                            {
+                                accountID: generateAccountID(bossEmail),
+                                login: bossEmail,
+                                displayName: bossEmail,
+                                selected: true,
+                            },
+                        ]);
 
-                    setMoneyRequestAmount(transactionID, CONST.TEST_DRIVE.EMPLOYEE_FAKE_RECEIPT.AMOUNT, CONST.TEST_DRIVE.EMPLOYEE_FAKE_RECEIPT.CURRENCY);
-                    setMoneyRequestDescription(transactionID, CONST.TEST_DRIVE.EMPLOYEE_FAKE_RECEIPT.DESCRIPTION, true);
-                    setMoneyRequestMerchant(transactionID, CONST.TEST_DRIVE.EMPLOYEE_FAKE_RECEIPT.MERCHANT, true);
-                    setMoneyRequestCreated(transactionID, format(new Date(), CONST.DATE.FNS_FORMAT_STRING), true);
+                        setMoneyRequestAmount(transactionID, CONST.TEST_DRIVE.EMPLOYEE_FAKE_RECEIPT.AMOUNT, CONST.TEST_DRIVE.EMPLOYEE_FAKE_RECEIPT.CURRENCY);
+                        setMoneyRequestDescription(transactionID, CONST.TEST_DRIVE.EMPLOYEE_FAKE_RECEIPT.DESCRIPTION, true);
+                        setMoneyRequestMerchant(transactionID, CONST.TEST_DRIVE.EMPLOYEE_FAKE_RECEIPT.MERCHANT, true);
+                        setMoneyRequestCreated(transactionID, format(new Date(), CONST.DATE.FNS_FORMAT_STRING), true);
 
-                    InteractionManager.runAfterInteractions(() => {
-                        Navigation.goBack();
-                        Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, CONST.IOU.TYPE.SUBMIT, transactionID, reportID));
-                    });
-                },
-                () => {
-                    setIsLoading(false);
-                    setFormError(translate('testDrive.modal.employee.error'));
-                },
-            );
-        });
+                        InteractionManager.runAfterInteractions(() => {
+                            Navigation.goBack();
+                            Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, CONST.IOU.TYPE.SUBMIT, transactionID, reportID));
+                        });
+                    },
+                    () => {
+                        setIsLoading(false);
+                        setFormError(translate('testDrive.modal.employee.error'));
+                    },
+                );
+            })
+            .catch(() => {
+                setIsLoading(false);
+                setFormError(translate('testDrive.modal.employee.error'));
+            });
     };
 
     const skipTestDrive = () => {
