From 20c6fcccffb84790de5e1285649573e7389cc4d3 Mon Sep 17 00:00:00 2001
From: Uladzimir Havenchyk <125459446+uladzimirdev@users.noreply.github.com>
Date: Fri, 6 Oct 2023 12:41:06 +0300
Subject: [PATCH] [jest] Add global catch for unmocked http requests (#34277)

---
 .../SaveQuestionModal.unit.spec.tsx           | 48 ++++++++++---------
 .../ModelDetailPage/ModelDetailPage.tsx       |  4 +-
 frontend/test/jest-setup-env.js               |  8 ++++
 3 files changed, 36 insertions(+), 24 deletions(-)

diff --git a/frontend/src/metabase/containers/SaveQuestionModal.unit.spec.tsx b/frontend/src/metabase/containers/SaveQuestionModal.unit.spec.tsx
index 799f036c1d6..9bd8bf4a816 100644
--- a/frontend/src/metabase/containers/SaveQuestionModal.unit.spec.tsx
+++ b/frontend/src/metabase/containers/SaveQuestionModal.unit.spec.tsx
@@ -11,7 +11,10 @@ import {
 import { setupEnterpriseTest } from "__support__/enterprise";
 import { mockSettings } from "__support__/settings";
 import type { CollectionEndpoints } from "__support__/server-mocks";
-import { setupCollectionsEndpoints } from "__support__/server-mocks";
+import {
+  setupCollectionsEndpoints,
+  setupCollectionByIdEndpoint,
+} from "__support__/server-mocks";
 import {
   createMockQueryBuilderState,
   createMockState,
@@ -35,27 +38,24 @@ const metadata = createMockMetadata({
   databases: [createSampleDatabase()],
 });
 
-const TEST_COLLECTIONS = [
-  {
-    can_write: false,
-    effective_ancestors: [],
-    effective_location: null,
-    id: "root",
-    name: "Our analytics",
-    parent_id: null,
-  },
-  {
-    archived: false,
-    can_write: true,
-    description: null,
-    id: 1,
-    location: "/",
-    name: "Bobby Tables's Personal Collection",
-    namespace: null,
-    personal_owner_id: 100,
-    slug: "bobby_tables_s_personal_collection",
-  },
-];
+const BOBBY_TEST_COLLECTION = createMockCollection({
+  archived: false,
+  can_write: true,
+  description: null,
+  id: 1,
+  location: "/",
+  name: "Bobby Tables's Personal Collection",
+  personal_owner_id: 100,
+});
+
+const ROOT_TEST_COLLECTION = createMockCollection({
+  ...ROOT_COLLECTION,
+  can_write: false,
+  effective_ancestors: [],
+  id: "root",
+});
+
+const TEST_COLLECTIONS = [ROOT_TEST_COLLECTION, BOBBY_TEST_COLLECTION];
 
 const setup = async (
   question: Question,
@@ -76,7 +76,8 @@ const setup = async (
     setupCollectionsEndpoints(collectionEndpoints);
   } else {
     fetchMock.get("path:/api/collection", TEST_COLLECTIONS);
-    fetchMock.get("path:/api/collection/root", TEST_COLLECTIONS);
+    fetchMock.get("path:/api/collection/root", ROOT_TEST_COLLECTION);
+    setupCollectionByIdEndpoint({ collections: [BOBBY_TEST_COLLECTION] });
   }
 
   const settings = mockSettings({ "enable-query-caching": isCachingEnabled });
@@ -714,6 +715,7 @@ describe("SaveQuestionModal", () => {
             rootCollection: COLLECTION.ROOT,
           },
         });
+        setupCollectionByIdEndpoint({ collections: [COLLECTION.PARENT] });
       });
 
       it("should create collection inside nested folder", async () => {
diff --git a/frontend/src/metabase/models/containers/ModelDetailPage/ModelDetailPage.tsx b/frontend/src/metabase/models/containers/ModelDetailPage/ModelDetailPage.tsx
index 2013df5996d..7bd6afd64c0 100644
--- a/frontend/src/metabase/models/containers/ModelDetailPage/ModelDetailPage.tsx
+++ b/frontend/src/metabase/models/containers/ModelDetailPage/ModelDetailPage.tsx
@@ -111,7 +111,9 @@ function ModelDetailPage({
     const card = model.card();
     const isModel = model.isDataset();
     if (isModel) {
-      loadMetadataForCard(card);
+      if (model.database()) {
+        loadMetadataForCard(card);
+      }
     } else {
       onChangeLocation(Urls.question(card));
     }
diff --git a/frontend/test/jest-setup-env.js b/frontend/test/jest-setup-env.js
index f55aa3bf5e3..a604ed7e52f 100644
--- a/frontend/test/jest-setup-env.js
+++ b/frontend/test/jest-setup-env.js
@@ -2,4 +2,12 @@ import fetchMock from "fetch-mock";
 
 beforeEach(() => {
   fetchMock.restore();
+  fetchMock.catch((url, request) => {
+    const errorMessage = `Caught unmocked ${request.method} request to: ${url}`;
+
+    Promise.reject(errorMessage);
+
+    // consider all not mocked requests are broken
+    return 500;
+  });
 });
-- 
GitLab