From 85811413fb7ce18cefae97b008c4806ae21c8259 Mon Sep 17 00:00:00 2001
From: Lewis Liu <develop@lewisl.net>
Date: Tue, 28 Jun 2016 16:16:43 -0700
Subject: [PATCH] Refactored fetch error handling into function, added tests

---
 frontend/src/metabase/dashboard/dashboard.js  | 17 ++++++----
 .../test/unit/dashboard/dashboard.spec.js     | 32 +++++++++++++++++++
 2 files changed, 42 insertions(+), 7 deletions(-)
 create mode 100644 frontend/test/unit/dashboard/dashboard.spec.js

diff --git a/frontend/src/metabase/dashboard/dashboard.js b/frontend/src/metabase/dashboard/dashboard.js
index 19de4d3317c..d3d0003ee1b 100644
--- a/frontend/src/metabase/dashboard/dashboard.js
+++ b/frontend/src/metabase/dashboard/dashboard.js
@@ -113,6 +113,15 @@ const updateDashcardId = createAction(UPDATE_DASHCARD_ID, (oldDashcardId, newDas
 const CLEAR_CARD_DATA = "CLEAR_CARD_DATA";
 export const clearCardData = createAction(CLEAR_CARD_DATA, (cardId, dashcardId) => ({ cardId, dashcardId }));
 
+export async function fetchDataOrError(dataPromise) {
+  try {
+    return await dataPromise;
+  }
+  catch (error) {
+    return { error };
+  }
+}
+
 export const fetchCardData = createThunkAction(FETCH_CARD_DATA, function(card, dashcard, clearExisting = false) {
     return async function(dispatch, getState) {
         if (clearExisting) {
@@ -152,13 +161,7 @@ export const fetchCardData = createThunkAction(FETCH_CARD_DATA, function(card, d
             }
         }, DATASET_SLOW_TIMEOUT);
 
-
-        try {
-          result = await MetabaseApi.dataset(datasetQuery);
-        }
-        catch (error) {
-          result = { error };
-        }
+        result = await fetchDataOrError(Promise.reject({data: {message: 'test'}}));
 
         clearTimeout(slowCardTimer);
         return { dashcard_id: dashcard.id, card_id: card.id, result };
diff --git a/frontend/test/unit/dashboard/dashboard.spec.js b/frontend/test/unit/dashboard/dashboard.spec.js
new file mode 100644
index 00000000000..9290c952069
--- /dev/null
+++ b/frontend/test/unit/dashboard/dashboard.spec.js
@@ -0,0 +1,32 @@
+import { fetchDataOrError } from 'metabase/dashboard/dashboard';
+
+describe("Dashboard", () => {
+    describe("fetchDataOrError()", () => {
+        it("should return data on successful fetch", async () => {
+            const data = {
+                series: [1, 2, 3]
+            };
+
+            const successfulFetch = Promise.resolve(data);
+
+            const result = await fetchDataOrError(successfulFetch);
+            expect(result.error).toBeUndefined();
+            expect(result).toEqual(data);
+        });
+
+        it("should return map with error key on failed fetch", async () => {
+            const error = {
+                status: 504,
+                statusText: "GATEWAY_TIMEOUT",
+                data: {
+                    message: "Failed to load resource: the server responded with a status of 504 (GATEWAY_TIMEOUT)"
+                }
+            };
+
+            const failedFetch = Promise.reject(error);
+
+            const result = await fetchDataOrError(failedFetch);
+            expect(result.error).toEqual(error);
+        });
+    });
+});
-- 
GitLab