From 23fe8057b18e2ad8f5eb1ea7088642987f4405a4 Mon Sep 17 00:00:00 2001
From: Nemanja Glumac <31325167+nemanjaglumac@users.noreply.github.com>
Date: Tue, 17 Nov 2020 16:19:29 +0100
Subject: [PATCH] #13649 Repro: Custom column from joined questions (#13813)

* Restore before each test

* Extract repro steps for #13000 into a helper function

* Add repro for #13649
---
 .../scenarios/question/notebook.cy.spec.js    | 195 +++++++++++-------
 1 file changed, 118 insertions(+), 77 deletions(-)

diff --git a/frontend/test/metabase/scenarios/question/notebook.cy.spec.js b/frontend/test/metabase/scenarios/question/notebook.cy.spec.js
index 771bd9454a3..96d969396f0 100644
--- a/frontend/test/metabase/scenarios/question/notebook.cy.spec.js
+++ b/frontend/test/metabase/scenarios/question/notebook.cy.spec.js
@@ -9,8 +9,10 @@ import {
 } from "__support__/cypress";
 
 describe("scenarios > question > notebook", () => {
-  before(restore);
-  beforeEach(signInAsAdmin);
+  beforeEach(() => {
+    restore();
+    signInAsAdmin();
+  });
 
   it.skip("shouldn't offer to save the question when there were no changes (metabase#13470)", () => {
     openOrdersTable();
@@ -149,83 +151,41 @@ describe("scenarios > question > notebook", () => {
     });
 
     it("should allow joins based on saved questions (metabase#13000)", () => {
-      cy.server();
+      // pass down a joined question alias
+      joinTwoSavedQuestions("13000");
+    });
 
-      withSampleDataset(({ ORDERS, PRODUCTS }) => {
-        cy.log("**-- Prepare Question 1 --**");
-        cy.request("POST", "/api/card", {
-          name: "Q1",
-          dataset_query: {
-            database: 1,
-            query: {
-              aggregation: ["sum", ["field-id", ORDERS.TOTAL]],
-              breakout: [["field-id", ORDERS.PRODUCT_ID]],
-              "source-table": 2,
-            },
-            type: "query",
-          },
-          display: "table",
-          visualization_settings: {},
-        }).then(({ body: { id: Q1_ID } }) => {
-          cy.log("**-- Prepare Question 2 --**");
-          cy.request("POST", "/api/card", {
-            name: "Q2",
-            dataset_query: {
-              database: 1,
-              query: {
-                aggregation: ["sum", ["field-id", PRODUCTS.RATING]],
-                breakout: [["field-id", PRODUCTS.ID]],
-                "source-table": 1,
-              },
-              type: "query",
-            },
-            display: "table",
-            visualization_settings: {},
-          }).then(({ body: { id: Q2_ID } }) => {
-            cy.log(
-              "**-- Create Question 3 based on 2 previously saved questions --**",
-            );
-            cy.request("POST", "/api/card", {
-              name: "Q3",
-              dataset_query: {
-                database: 1,
-                query: {
-                  joins: [
-                    {
-                      alias: "13000",
-                      condition: [
-                        "=",
-                        ["field-literal", "PRODUCT_ID", "type/Integer"],
-                        [
-                          "joined-field",
-                          "13000",
-                          ["field-literal", "ID", "type/BigInteger"],
-                        ],
-                      ],
-                      fields: "all",
-                      "source-table": `card__${Q2_ID}`,
-                    },
-                  ],
-                  "source-table": `card__${Q1_ID}`,
-                },
-                type: "query",
-              },
-              display: "table",
-              visualization_settings: {},
-            }).then(({ body: { id: Q3_ID } }) => {
-              cy.route("POST", `/api/card/${Q3_ID}/query`).as("cardQuery");
-              cy.visit(`/question/${Q3_ID}`);
-
-              cy.wait("@cardQuery");
-
-              cy.log("**Reported in v0.36.0**");
-              cy.get(".Icon-notebook").click();
-              cy.url().should("contain", "/notebook");
-              cy.findByText("Visualize").should("exist");
-            });
-          });
-        });
+    // NOTE: - This repro is really tightly coupled to the `joinTwoSavedQuestions()` function.
+    //       - Be extremely careful when changing any of the steps within that function.
+    //       - The alternative approach would have been to write one longer repro instead of two separate ones.
+    it.skip("joined questions should create custom column (metabase#13649)", () => {
+      // pass down a joined question alias
+      joinTwoSavedQuestions("13649");
+
+      // add a custom column on top of the steps from the #13000 repro which was simply asserting
+      // that a question could be made by joining two previously saved questions
+      cy.findByText("Custom column").click();
+      popover().within(() => {
+        cy.get("[contenteditable='true']").type(
+          // reference joined question by previously set alias
+          "[13649 → Sum of Rating] / [Sum of Rating]",
+        );
+        cy.findByPlaceholderText("Something nice and descriptive")
+          .click()
+          .type("Sum Divide");
+
+        cy.findAllByRole("button")
+          .contains("Done")
+          .should("not.be.disabled")
+          .click();
       });
+      cy.route("POST", "/api/dataset").as("visualization");
+      cy.findByText("Visualize").click();
+
+      cy.wait("@visualization").then(xhr => {
+        expect(xhr.response.body.error).not.to.exist;
+      });
+      cy.findByText("Sum Divide");
     });
 
     it("should show correct column title with foreign keys (metabase#11452)", () => {
@@ -368,3 +328,84 @@ describe("scenarios > question > notebook", () => {
     });
   });
 });
+
+// Extracted repro steps for #13000
+function joinTwoSavedQuestions(ALIAS = "Joined Question") {
+  cy.server();
+
+  withSampleDataset(({ ORDERS, PRODUCTS }) => {
+    cy.log("**-- Prepare Question 1 --**");
+    cy.request("POST", "/api/card", {
+      name: "Q1",
+      dataset_query: {
+        database: 1,
+        query: {
+          aggregation: ["sum", ["field-id", ORDERS.TOTAL]],
+          breakout: [["field-id", ORDERS.PRODUCT_ID]],
+          "source-table": 2,
+        },
+        type: "query",
+      },
+      display: "table",
+      visualization_settings: {},
+    }).then(({ body: { id: Q1_ID } }) => {
+      cy.log("**-- Prepare Question 2 --**");
+      cy.request("POST", "/api/card", {
+        name: "Q2",
+        dataset_query: {
+          database: 1,
+          query: {
+            aggregation: ["sum", ["field-id", PRODUCTS.RATING]],
+            breakout: [["field-id", PRODUCTS.ID]],
+            "source-table": 1,
+          },
+          type: "query",
+        },
+        display: "table",
+        visualization_settings: {},
+      }).then(({ body: { id: Q2_ID } }) => {
+        cy.log(
+          "**-- Create Question 3 based on 2 previously saved questions --**",
+        );
+        cy.request("POST", "/api/card", {
+          name: "Q3",
+          dataset_query: {
+            database: 1,
+            query: {
+              joins: [
+                {
+                  alias: ALIAS,
+                  condition: [
+                    "=",
+                    ["field-literal", "PRODUCT_ID", "type/Integer"],
+                    [
+                      "joined-field",
+                      ALIAS,
+                      ["field-literal", "ID", "type/BigInteger"],
+                    ],
+                  ],
+                  fields: "all",
+                  "source-table": `card__${Q2_ID}`,
+                },
+              ],
+              "source-table": `card__${Q1_ID}`,
+            },
+            type: "query",
+          },
+          display: "table",
+          visualization_settings: {},
+        }).then(({ body: { id: Q3_ID } }) => {
+          cy.route("POST", `/api/card/${Q3_ID}/query`).as("cardQuery");
+          cy.visit(`/question/${Q3_ID}`);
+
+          cy.wait("@cardQuery");
+
+          cy.log("**Reported in v0.36.0**");
+          cy.get(".Icon-notebook").click();
+          cy.url().should("contain", "/notebook");
+          cy.findByText("Visualize").should("exist");
+        });
+      });
+    });
+  });
+}
-- 
GitLab