From 05100d3a0e5bc6c2fa00288677e413cd7fb05e20 Mon Sep 17 00:00:00 2001
From: Nemanja Glumac <31325167+nemanjaglumac@users.noreply.github.com>
Date: Fri, 8 Apr 2022 23:34:32 +0200
Subject: [PATCH] [E2E] Deprecate `openNotebookEditor` helper (#21552)

* Programatically open a "New > Question" data selector screen

* Deprecate `openNotebookEditor` which relied on UI

* Let Metabase choose the visualization for each query
---
 .../helpers/e2e-ad-hoc-question-helpers.js    | 20 +++++++++++++++++++
 .../e2e/helpers/e2e-misc-helpers.js           | 19 ------------------
 .../notebook/notebook.cy.spec.js              |  4 ++--
 .../binning/binning-reproductions.cy.spec.js  |  4 ++--
 .../scenarios/joins/joins-2.cy.spec.js        |  4 ++--
 .../scenarios/question/new.cy.spec.js         |  4 ++--
 .../scenarios/question/operators.cy.spec.js   | 12 +++++------
 .../reproductions/4482-temporal-min-max.js    |  8 ++------
 ...ions-not-in-saved-questions-immediately.js |  4 ++--
 .../scenarios/smoketest/admin.cy.spec.js      |  6 +++---
 .../smoketest/admin_setup.cy.spec.js          |  6 +++---
 .../scenarios/smoketest/user.cy.spec.js       |  4 ++--
 12 files changed, 46 insertions(+), 49 deletions(-)

diff --git a/frontend/test/__support__/e2e/helpers/e2e-ad-hoc-question-helpers.js b/frontend/test/__support__/e2e/helpers/e2e-ad-hoc-question-helpers.js
index 5f52fa7594c..df262f33bbf 100644
--- a/frontend/test/__support__/e2e/helpers/e2e-ad-hoc-question-helpers.js
+++ b/frontend/test/__support__/e2e/helpers/e2e-ad-hoc-question-helpers.js
@@ -15,6 +15,26 @@ export function adhocQuestionHash(question) {
   return btoa(unescape(encodeURIComponent(JSON.stringify(question))));
 }
 
+/**
+ * This is the query generated by clicking "New" and then choosing the (gui) "Question".
+ */
+export function startNewQuestion() {
+  const newQuestionQuery = {
+    creationType: "custom_question",
+    dataset_query: {
+      database: null,
+      query: {
+        "source-table": null,
+      },
+      type: "query",
+    },
+    visualization_settings: {},
+  };
+
+  const hash = adhocQuestionHash(newQuestionQuery);
+
+  cy.visit("/question/notebook#" + hash);
+}
 /**
  * Visit any valid query in an ad-hoc manner.
  *
diff --git a/frontend/test/__support__/e2e/helpers/e2e-misc-helpers.js b/frontend/test/__support__/e2e/helpers/e2e-misc-helpers.js
index d9da22d4a0b..6f3f5828b12 100644
--- a/frontend/test/__support__/e2e/helpers/e2e-misc-helpers.js
+++ b/frontend/test/__support__/e2e/helpers/e2e-misc-helpers.js
@@ -43,25 +43,6 @@ export function openNativeEditor({
     .should("be.visible");
 }
 
-/**
- * Open notebook editor.
- *
- * @param {object} options
- * @param {boolean} [options.fromCurrentPage] - Open notebook editor from current location
- * @example
- * openNotebookEditor({ fromCurrentPage: true })
- */
-export function openNotebookEditor({ fromCurrentPage } = {}) {
-  if (!fromCurrentPage) {
-    cy.visit("/");
-  }
-
-  cy.findByText("New").click();
-  cy.findByText("Question")
-    .should("be.visible")
-    .click();
-}
-
 /**
  * Executes native query and waits for the results to load.
  * Makes sure that the question is not "dirty" after the query successfully ran.
diff --git a/frontend/test/metabase-visual/notebook/notebook.cy.spec.js b/frontend/test/metabase-visual/notebook/notebook.cy.spec.js
index a1ed4424e24..cd74b5b790a 100644
--- a/frontend/test/metabase-visual/notebook/notebook.cy.spec.js
+++ b/frontend/test/metabase-visual/notebook/notebook.cy.spec.js
@@ -1,5 +1,5 @@
 import _ from "underscore";
-import { restore, popover, openNotebookEditor } from "__support__/e2e/cypress";
+import { restore, popover, startNewQuestion } from "__support__/e2e/cypress";
 
 describe("visual tests > notebook > major UI elements", () => {
   const VIEWPORT_WIDTH = 2500;
@@ -120,7 +120,7 @@ describe("visual tests > notebook", () => {
   });
 
   it("data picker", () => {
-    openNotebookEditor();
+    startNewQuestion();
     cy.findByText("Sample Database");
     cy.percySnapshot();
   });
diff --git a/frontend/test/metabase/scenarios/binning/binning-reproductions.cy.spec.js b/frontend/test/metabase/scenarios/binning/binning-reproductions.cy.spec.js
index a860e2a8736..2694382d71a 100644
--- a/frontend/test/metabase/scenarios/binning/binning-reproductions.cy.spec.js
+++ b/frontend/test/metabase/scenarios/binning/binning-reproductions.cy.spec.js
@@ -5,7 +5,7 @@ import {
   visitQuestionAdhoc,
   changeBinningForDimension,
   getBinningButtonForDimension,
-  openNotebookEditor,
+  startNewQuestion,
   summarize,
 } from "__support__/e2e/cypress";
 
@@ -165,7 +165,7 @@ describe("binning related reproductions", () => {
 
     // it is essential for this repro to find question following these exact steps
     // (for example, visiting `/collection/root` would yield different result)
-    openNotebookEditor();
+    startNewQuestion();
     cy.findByText("Saved Questions").click();
     cy.findByText("11439").click();
     visualize();
diff --git a/frontend/test/metabase/scenarios/joins/joins-2.cy.spec.js b/frontend/test/metabase/scenarios/joins/joins-2.cy.spec.js
index ea420139461..bf254726a93 100644
--- a/frontend/test/metabase/scenarios/joins/joins-2.cy.spec.js
+++ b/frontend/test/metabase/scenarios/joins/joins-2.cy.spec.js
@@ -3,7 +3,7 @@ import {
   openOrdersTable,
   popover,
   visualize,
-  openNotebookEditor,
+  startNewQuestion,
   enterCustomColumnDetails,
   visitQuestionAdhoc,
   summarize,
@@ -295,7 +295,7 @@ describe("scenarios > question > joined questions", () => {
       });
 
       // Join two previously saved questions
-      openNotebookEditor();
+      startNewQuestion();
       cy.findByText("Saved Questions").click();
 
       cy.findByText("12928_Q1").click();
diff --git a/frontend/test/metabase/scenarios/question/new.cy.spec.js b/frontend/test/metabase/scenarios/question/new.cy.spec.js
index f13448ceea3..97cc371a40e 100644
--- a/frontend/test/metabase/scenarios/question/new.cy.spec.js
+++ b/frontend/test/metabase/scenarios/question/new.cy.spec.js
@@ -7,7 +7,7 @@ import {
   restore,
   visualize,
   summarize,
-  openNotebookEditor,
+  startNewQuestion,
   visitQuestion,
 } from "__support__/e2e/cypress";
 
@@ -345,7 +345,7 @@ describe("scenarios > question > new", () => {
 
   describe("ask a (custom) question", () => {
     it("should load orders table", () => {
-      openNotebookEditor();
+      startNewQuestion();
       cy.contains("Sample Database").click();
       cy.contains("Orders").click();
 
diff --git a/frontend/test/metabase/scenarios/question/operators.cy.spec.js b/frontend/test/metabase/scenarios/question/operators.cy.spec.js
index 87b41cbe83f..b0c7e47d263 100644
--- a/frontend/test/metabase/scenarios/question/operators.cy.spec.js
+++ b/frontend/test/metabase/scenarios/question/operators.cy.spec.js
@@ -1,4 +1,4 @@
-import { restore, popover, openNotebookEditor } from "__support__/e2e/cypress";
+import { restore, popover, startNewQuestion } from "__support__/e2e/cypress";
 
 describe("operators in questions", () => {
   beforeEach(() => {
@@ -60,7 +60,7 @@ describe("operators in questions", () => {
 
   describe("fields have proper operators", () => {
     it("text operators", () => {
-      openNotebookEditor();
+      startNewQuestion();
       cy.contains("Sample Database").click();
       cy.contains("Products").click();
       cy.findByText("Add filters to narrow your answer").click();
@@ -77,7 +77,7 @@ describe("operators in questions", () => {
     });
 
     it("number operators", () => {
-      openNotebookEditor();
+      startNewQuestion();
       cy.contains("Sample Database").click();
       cy.contains("Products").click();
       cy.findByText("Add filters to narrow your answer").click();
@@ -94,7 +94,7 @@ describe("operators in questions", () => {
     });
 
     it("date operators", () => {
-      openNotebookEditor();
+      startNewQuestion();
       cy.contains("Sample Database").click();
       cy.contains("Products").click();
       cy.findByText("Add filters to narrow your answer").click();
@@ -111,7 +111,7 @@ describe("operators in questions", () => {
     });
 
     it("id operators", () => {
-      openNotebookEditor();
+      startNewQuestion();
       cy.contains("Sample Database").click();
       cy.contains("Products").click();
       cy.findByText("Add filters to narrow your answer").click();
@@ -128,7 +128,7 @@ describe("operators in questions", () => {
     });
 
     it("geo operators", () => {
-      openNotebookEditor();
+      startNewQuestion();
       cy.contains("Sample Database").click();
       cy.contains("People").click();
       cy.findByText("Add filters to narrow your answer").click();
diff --git a/frontend/test/metabase/scenarios/question/reproductions/4482-temporal-min-max.js b/frontend/test/metabase/scenarios/question/reproductions/4482-temporal-min-max.js
index e0bdc6e40ff..11f4bef8bbc 100644
--- a/frontend/test/metabase/scenarios/question/reproductions/4482-temporal-min-max.js
+++ b/frontend/test/metabase/scenarios/question/reproductions/4482-temporal-min-max.js
@@ -1,8 +1,4 @@
-import {
-  restore,
-  visualize,
-  openNotebookEditor,
-} from "__support__/e2e/cypress";
+import { restore, visualize, startNewQuestion } from "__support__/e2e/cypress";
 
 export function issue4482() {
   describe("issue 4482", () => {
@@ -10,7 +6,7 @@ export function issue4482() {
       restore();
       cy.signInAsAdmin();
 
-      openNotebookEditor();
+      startNewQuestion();
       cy.contains("Sample Database").click();
       cy.contains("Products").click();
     });
diff --git a/frontend/test/metabase/scenarios/question/reproductions/9027-new-questions-not-in-saved-questions-immediately.js b/frontend/test/metabase/scenarios/question/reproductions/9027-new-questions-not-in-saved-questions-immediately.js
index c2f8d59f738..64e0d91b1f9 100644
--- a/frontend/test/metabase/scenarios/question/reproductions/9027-new-questions-not-in-saved-questions-immediately.js
+++ b/frontend/test/metabase/scenarios/question/reproductions/9027-new-questions-not-in-saved-questions-immediately.js
@@ -2,7 +2,7 @@ import {
   restore,
   popover,
   openNativeEditor,
-  openNotebookEditor,
+  startNewQuestion,
   openNavigationSidebar,
   navigationSidebar,
 } from "__support__/e2e/cypress";
@@ -45,7 +45,7 @@ export function issue9027() {
     questionName,
     exists = true,
   ) {
-    openNotebookEditor({ fromCurrentPage: true });
+    startNewQuestion();
     cy.findByText("Saved Questions").click();
 
     cy.findByText(questionName).should(exists ? "exist" : "not.exist");
diff --git a/frontend/test/metabase/scenarios/smoketest/admin.cy.spec.js b/frontend/test/metabase/scenarios/smoketest/admin.cy.spec.js
index 6f29c284f0c..df71e90a5b6 100644
--- a/frontend/test/metabase/scenarios/smoketest/admin.cy.spec.js
+++ b/frontend/test/metabase/scenarios/smoketest/admin.cy.spec.js
@@ -2,7 +2,7 @@ import {
   restore,
   sidebar,
   visualize,
-  openNotebookEditor,
+  startNewQuestion,
   summarize,
 } from "__support__/e2e/cypress";
 import { USERS } from "__support__/e2e/cypress_data";
@@ -155,7 +155,7 @@ describe("metabase-smoketest > admin", () => {
     });
 
     it.skip("should add a simple JOINed question as admin", () => {
-      openNotebookEditor();
+      startNewQuestion();
       cy.findByTextEnsureVisible("Sample Database").click();
       cy.findByTextEnsureVisible("Orders").click();
 
@@ -189,7 +189,7 @@ describe("metabase-smoketest > admin", () => {
     });
 
     it("should add a question with a default line visualization as admin", () => {
-      openNotebookEditor();
+      startNewQuestion();
       cy.findByTextEnsureVisible("Sample Database").click();
       cy.findByTextEnsureVisible("Orders").click();
 
diff --git a/frontend/test/metabase/scenarios/smoketest/admin_setup.cy.spec.js b/frontend/test/metabase/scenarios/smoketest/admin_setup.cy.spec.js
index 6380ba8765e..d8978c953ff 100644
--- a/frontend/test/metabase/scenarios/smoketest/admin_setup.cy.spec.js
+++ b/frontend/test/metabase/scenarios/smoketest/admin_setup.cy.spec.js
@@ -6,7 +6,7 @@ import {
   openPeopleTable,
   visualize,
   navigationSidebar,
-  openNotebookEditor,
+  startNewQuestion,
   openNavigationSidebar,
   visitQuestion,
 } from "__support__/e2e/cypress";
@@ -797,7 +797,7 @@ describe("smoketest > admin_setup", () => {
 
       // Normal user cannot make an SQL query
 
-      openNotebookEditor({ fromCurrentPage: true });
+      startNewQuestion();
 
       cy.signOut();
       cy.signIn("nocollection");
@@ -887,7 +887,7 @@ describe("smoketest > admin_setup", () => {
 
       cy.icon("pencil");
 
-      openNotebookEditor({ fromCurrentPage: true });
+      startNewQuestion();
       cy.findByTextEnsureVisible("Sample Database").click();
       cy.findByTextEnsureVisible("People").click();
       cy.findByText("Save").click();
diff --git a/frontend/test/metabase/scenarios/smoketest/user.cy.spec.js b/frontend/test/metabase/scenarios/smoketest/user.cy.spec.js
index 4e73fab58a0..c7272d82bcc 100644
--- a/frontend/test/metabase/scenarios/smoketest/user.cy.spec.js
+++ b/frontend/test/metabase/scenarios/smoketest/user.cy.spec.js
@@ -3,7 +3,7 @@ import {
   sidebar,
   popover,
   visualize,
-  openNotebookEditor,
+  startNewQuestion,
   summarize,
 } from "__support__/e2e/cypress";
 
@@ -13,7 +13,7 @@ describe("smoketest > user", () => {
   beforeEach(cy.signInAsNormalUser);
 
   it("should be able to ask a custom question", () => {
-    openNotebookEditor();
+    startNewQuestion();
     cy.findByTextEnsureVisible("Sample Database").click();
     cy.findByTextEnsureVisible("Products").click();
     cy.findByText("Add filters to narrow your answer").click();
-- 
GitLab