From c497d3a941ec6cab093d0a9df801c9e2f8eeae40 Mon Sep 17 00:00:00 2001
From: Matiss Janis Aboltins <matiss@mja.lv>
Date: Wed, 25 Sep 2024 21:56:44 +0100
Subject: [PATCH] :test_tube: improve onboarding e2e test stability (#3503)

---
 .../src/components/FinancesApp.tsx            | 36 +++++++++++--------
 upcoming-release-notes/3503.md                |  6 ++++
 2 files changed, 28 insertions(+), 14 deletions(-)
 create mode 100644 upcoming-release-notes/3503.md

diff --git a/packages/desktop-client/src/components/FinancesApp.tsx b/packages/desktop-client/src/components/FinancesApp.tsx
index 73b8948ad..c6d71930e 100644
--- a/packages/desktop-client/src/components/FinancesApp.tsx
+++ b/packages/desktop-client/src/components/FinancesApp.tsx
@@ -31,6 +31,7 @@ import { TransactionEdit } from './mobile/transactions/TransactionEdit';
 import { Notifications } from './Notifications';
 import { ManagePayeesPage } from './payees/ManagePayeesPage';
 import { Reports } from './reports';
+import { LoadingIndicator } from './reports/LoadingIndicator';
 import { NarrowAlternate, WideComponent } from './responsive';
 import { Settings } from './settings';
 import { FloatableSidebar } from './sidebar';
@@ -65,19 +66,6 @@ function WideNotSupported({ children, redirectTo = '/budget' }) {
 }
 
 function RouterBehaviors() {
-  const navigate = useNavigate();
-  const accounts = useAccounts();
-  const accountsLoaded = useSelector(
-    (state: State) => state.queries.accountsLoaded,
-  );
-  useEffect(() => {
-    // If there are no accounts, we want to redirect the user to
-    // the All Accounts screen which will prompt them to add an account
-    if (accountsLoaded && accounts.length === 0) {
-      navigate('/accounts');
-    }
-  }, [accountsLoaded, accounts]);
-
   const location = useLocation();
   const href = useHref(location);
   useEffect(() => {
@@ -91,6 +79,11 @@ export function FinancesApp() {
   const dispatch = useDispatch();
   const { t } = useTranslation();
 
+  const accounts = useAccounts();
+  const accountsLoaded = useSelector(
+    (state: State) => state.queries.accountsLoaded,
+  );
+
   const [lastUsedVersion, setLastUsedVersion] = useLocalPref(
     'flags.updateNotificationShownForVersion',
   );
@@ -180,7 +173,22 @@ export function FinancesApp() {
             <BankSyncStatus />
 
             <Routes>
-              <Route path="/" element={<Navigate to="/budget" replace />} />
+              <Route
+                path="/"
+                element={
+                  accountsLoaded ? (
+                    accounts.length > 0 ? (
+                      <Navigate to="/budget" replace />
+                    ) : (
+                      // If there are no accounts, we want to redirect the user to
+                      // the All Accounts screen which will prompt them to add an account
+                      <Navigate to="/accounts" replace />
+                    )
+                  ) : (
+                    <LoadingIndicator />
+                  )
+                }
+              />
 
               <Route path="/reports/*" element={<Reports />} />
 
diff --git a/upcoming-release-notes/3503.md b/upcoming-release-notes/3503.md
new file mode 100644
index 000000000..4ae2f6204
--- /dev/null
+++ b/upcoming-release-notes/3503.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [MatissJanis]
+---
+
+e2e: improve onboarding test stability.
-- 
GitLab