From 9f9f9f05aefd9c52588dea881b72a0e8f3824a69 Mon Sep 17 00:00:00 2001
From: Alexander Polyankin <alexander.polyankin@metabase.com>
Date: Wed, 4 May 2022 17:13:59 +0300
Subject: [PATCH] Fix cancelling the native query via shortcut (#22415)

---
 .../components/NativeQueryEditor.jsx          | 11 ++++--
 ...27-cancel-native-query-shortcut.cy.spec.js | 38 +++++++++++++++++++
 2 files changed, 46 insertions(+), 3 deletions(-)
 create mode 100644 frontend/test/metabase/scenarios/visualizations/reproductions/11727-cancel-native-query-shortcut.cy.spec.js

diff --git a/frontend/src/metabase/query_builder/components/NativeQueryEditor.jsx b/frontend/src/metabase/query_builder/components/NativeQueryEditor.jsx
index e1187d42db7..baf590adbcf 100644
--- a/frontend/src/metabase/query_builder/components/NativeQueryEditor.jsx
+++ b/frontend/src/metabase/query_builder/components/NativeQueryEditor.jsx
@@ -177,9 +177,14 @@ export default class NativeQueryEditor extends Component {
   }, 100);
 
   handleKeyDown = e => {
-    const ENTER_KEY = 13;
-    if (e.keyCode === ENTER_KEY && (e.metaKey || e.ctrlKey)) {
-      this.runQuery();
+    const { isRunning, cancelQuery } = this.props;
+
+    if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
+      if (isRunning) {
+        cancelQuery();
+      } else {
+        this.runQuery();
+      }
     }
   };
 
diff --git a/frontend/test/metabase/scenarios/visualizations/reproductions/11727-cancel-native-query-shortcut.cy.spec.js b/frontend/test/metabase/scenarios/visualizations/reproductions/11727-cancel-native-query-shortcut.cy.spec.js
new file mode 100644
index 00000000000..6239daf6851
--- /dev/null
+++ b/frontend/test/metabase/scenarios/visualizations/reproductions/11727-cancel-native-query-shortcut.cy.spec.js
@@ -0,0 +1,38 @@
+import {
+  restore,
+  withDatabase,
+  adhocQuestionHash,
+} from "__support__/e2e/cypress";
+
+const PG_DB_ID = 2;
+
+const questionDetails = {
+  dataset_query: {
+    type: "native",
+    database: PG_DB_ID,
+    native: {
+      query: "SELECT pg_sleep(10)",
+    },
+  },
+};
+
+describe("issue 11727", () => {
+  beforeEach(() => {
+    restore("postgres-12");
+    cy.signInAsAdmin();
+    cy.intercept("GET", "/api/database").as("getDatabases");
+  });
+
+  it("should cancel the native query via the keyboard shortcut (metabase#11727)", () => {
+    withDatabase(PG_DB_ID, () => {
+      cy.visit(`/question#` + adhocQuestionHash(questionDetails));
+      cy.wait("@getDatabases");
+
+      cy.findByText("Doing science...").should("be.visible");
+      cy.get("body").type("{cmd}{enter}");
+      cy.findByText("Here's where your results will appear").should(
+        "be.visible",
+      );
+    });
+  });
+});
-- 
GitLab