From 56e788636661b3c57fcb5d42cf4cd5a1ba80dc13 Mon Sep 17 00:00:00 2001
From: Alexander Polyankin <alexander.polyankin@metabase.com>
Date: Tue, 21 Jun 2022 20:44:59 +0300
Subject: [PATCH] Fix a race condition in loading questions for pinned items
 (#23466)

---
 .../PinnedQuestionLoader.tsx                  |  2 +-
 .../collection-pinned-overview.cy.spec.js     | 43 +++++++++++++++++--
 2 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/frontend/src/metabase/collections/components/PinnedQuestionCard/PinnedQuestionLoader.tsx b/frontend/src/metabase/collections/components/PinnedQuestionCard/PinnedQuestionLoader.tsx
index 6e3200658b3..77ad043f68f 100644
--- a/frontend/src/metabase/collections/components/PinnedQuestionCard/PinnedQuestionLoader.tsx
+++ b/frontend/src/metabase/collections/components/PinnedQuestionCard/PinnedQuestionLoader.tsx
@@ -45,7 +45,7 @@ const PinnedQuestionLoader = ({
   return (
     <Questions.Loader id={id} loadingAndErrorWrapper={false}>
       {({ loading, question: card }: QuestionLoaderProps) => {
-        if (loading) {
+        if (loading || !card.dataset_query) {
           return children({ loading: true });
         }
 
diff --git a/frontend/test/metabase/scenarios/collections/collection-pinned-overview.cy.spec.js b/frontend/test/metabase/scenarios/collections/collection-pinned-overview.cy.spec.js
index 82fc0b5b0a9..2be71536409 100644
--- a/frontend/test/metabase/scenarios/collections/collection-pinned-overview.cy.spec.js
+++ b/frontend/test/metabase/scenarios/collections/collection-pinned-overview.cy.spec.js
@@ -1,18 +1,38 @@
 import { popover, restore } from "__support__/e2e/cypress";
+import { SAMPLE_DATABASE } from "__support__/e2e/cypress_sample_database";
+
+const { ORDERS, ORDERS_ID } = SAMPLE_DATABASE;
 
 const DASHBOARD_NAME = "Orders in a dashboard";
 const QUESTION_NAME = "Orders, Count";
 const MODEL_NAME = "Orders";
 
+const PIVOT_QUESTION_DETAILS = {
+  name: "Pivot table",
+  display: "pivot",
+  query: {
+    "source-table": ORDERS_ID,
+    breakout: [["field", ORDERS.CREATED_AT, { "temporal-unit": "month" }]],
+    aggregation: [["count"]],
+  },
+  visualization_settings: {
+    "table.pivot_column": "CREATED_AT",
+    "table.cell_column": "count",
+    "pivot_table.column_split": {
+      rows: [["field", ORDERS.CREATED_AT, { "temporal-unit": "month" }]],
+      columns: [],
+      values: [["aggregation", 0]],
+    },
+  },
+};
+
 describe("scenarios > collection pinned items overview", () => {
   beforeEach(() => {
     restore();
     cy.signInAsAdmin();
 
-    cy.intercept("POST", `/api/card/*/query`).as("getCardQuery");
-    cy.intercept("GET", "/api/collection/*/items?pinned_state=is_pinned*").as(
-      "getPinnedItems",
-    );
+    cy.intercept("POST", `/api/card/**/query`).as("getCardQuery");
+    cy.intercept("GET", "/api/**/items?pinned_state*").as("getPinnedItems");
   });
 
   it("should be able to pin a dashboard", () => {
@@ -42,6 +62,21 @@ describe("scenarios > collection pinned items overview", () => {
     });
   });
 
+  it("should be able to pin a pivot table", () => {
+    cy.createQuestion(PIVOT_QUESTION_DETAILS).then(({ body: { id } }) => {
+      cy.request("PUT", `/api/card/${id}`, { collection_position: 1 });
+    });
+
+    openRootCollection();
+    cy.wait("@getCardQuery");
+
+    getPinnedSection().within(() => {
+      cy.findByText(PIVOT_QUESTION_DETAILS.name).should("be.visible");
+      cy.findByText("Created At: Month").should("be.visible");
+      cy.findByText("Count").should("be.visible");
+    });
+  });
+
   it("should be able to pin a model", () => {
     cy.request("PUT", "/api/card/1", { dataset: true });
 
-- 
GitLab