Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

UXW-2444 support opening Document cards in a new tab (#67057)

Denis Berezin32eb9a53ce35
diff --git a/e2e/support/helpers/e2e-misc-helpers.js b/e2e/support/helpers/e2e-misc-helpers.js
index 8b4a7c48138..cc3b3fa3674 100644
--- a/e2e/support/helpers/e2e-misc-helpers.js
+++ b/e2e/support/helpers/e2e-misc-helpers.js
@@ -431,3 +431,20 @@ export const goToAuthOverviewPage = () => {
.findByText("Overview") // auth overview page
.click();
};
+
+/**
+ * This function exists to work around custom dynamic anchor creation.
+ * @see https://github.com/metabase/metabase/blob/master/frontend/src/metabase/lib/dom.js#L301-L312
+ *
+ * WARNING: For the assertions to work, ensure that a click event occurs on an anchor element afterwards.
+ */
+export const onNextAnchorClick = (callback) => {
+ cy.window().then((window) => {
+ const originalClick = window.HTMLAnchorElement.prototype.click;
+
+ window.HTMLAnchorElement.prototype.click = function () {
+ callback(this);
+ window.HTMLAnchorElement.prototype.click = originalClick;
+ };
+ });
+};
diff --git a/e2e/test/scenarios/dashboard-cards/click-behavior.cy.spec.js b/e2e/test/scenarios/dashboard-cards/click-behavior.cy.spec.js
index 04f8ec26d89..e7837fe75ed 100644
--- a/e2e/test/scenarios/dashboard-cards/click-behavior.cy.spec.js
+++ b/e2e/test/scenarios/dashboard-cards/click-behavior.cy.spec.js
@@ -1021,7 +1021,7 @@ describe("scenarios > dashboard > dashboard cards > click behavior", () => {
H.saveDashboard({ waitMs: 250 });
- onNextAnchorClick((anchor) => {
+ H.onNextAnchorClick((anchor) => {
expect(anchor).to.have.attr("href", URL);
expect(anchor).to.have.attr("rel", "noopener");
expect(anchor).to.have.attr("target", "_blank");
@@ -1078,7 +1078,7 @@ describe("scenarios > dashboard > dashboard cards > click behavior", () => {
cy.button("Add filter").click();
});
- onNextAnchorClick((anchor) => {
+ H.onNextAnchorClick((anchor) => {
expect(anchor).to.have.attr("href", URL_WITH_FILLED_PARAMS);
expect(anchor).to.have.attr("rel", "noopener");
expect(anchor).to.have.attr("target", "_blank");
@@ -1641,7 +1641,7 @@ describe("scenarios > dashboard > dashboard cards > click behavior", () => {
cy.findByPlaceholderText("Search the list").type("Dell Adams");
cy.button("Update filter").click();
});
- onNextAnchorClick((anchor) => {
+ H.onNextAnchorClick((anchor) => {
expect(anchor).to.have.attr("href", URL_WITH_FILLED_PARAMS);
expect(anchor).to.have.attr("rel", "noopener");
expect(anchor).to.have.attr("target", "_blank");
@@ -1813,7 +1813,7 @@ describe("scenarios > dashboard > dashboard cards > click behavior", () => {
cy.findByPlaceholderText("Search the list").type("Dell Adams");
cy.button("Add filter").click();
});
- onNextAnchorClick((anchor) => {
+ H.onNextAnchorClick((anchor) => {
expect(anchor).to.have.attr("href", URL_WITH_FILLED_PARAMS);
expect(anchor).to.have.attr("rel", "noopener");
expect(anchor).to.have.attr("target", "_blank");
@@ -2753,23 +2753,6 @@ describe("scenarios > dashboard > dashboard cards > click behavior", () => {
});
});
-/**
- * This function exists to work around custom dynamic anchor creation.
- * @see https://github.com/metabase/metabase/blob/master/frontend/src/metabase/lib/dom.js#L301-L312
- *
- * WARNING: For the assertions to work, ensure that a click event occurs on an anchor element afterwards.
- */
-const onNextAnchorClick = (callback) => {
- cy.window().then((window) => {
- const originalClick = window.HTMLAnchorElement.prototype.click;
-
- window.HTMLAnchorElement.prototype.click = function () {
- callback(this);
- window.HTMLAnchorElement.prototype.click = originalClick;
- };
- });
-};
-
const clickLineChartPoint = () => {
// eslint-disable-next-line no-unsafe-element-filtering
H.cartesianChartCircle()
diff --git a/e2e/test/scenarios/documents/card-embed-node.cy.spec.ts b/e2e/test/scenarios/documents/card-embed-node.cy.spec.ts
index 2cafb35645d..4f2008a53f2 100644
--- a/e2e/test/scenarios/documents/card-embed-node.cy.spec.ts
+++ b/e2e/test/scenarios/documents/card-embed-node.cy.spec.ts
@@ -542,6 +542,97 @@ describe("documents card embed node custom logic", () => {
});
});
+ describe("navigating from cardEmbed", () => {
+ it("should open a question in a new tab when clicking title with ctrl/meta key", () => {
+ const macOSX = Cypress.platform === "darwin";
+
+ H.createDocument({
+ name: "Test Document",
+ document: DOCUMENT_WITH_TWO_CARDS,
+ collection_id: null,
+ alias: "document",
+ idAlias: "documentId",
+ });
+
+ H.visitDocument("@documentId");
+
+ // Wait for cards to load
+ H.getDocumentCard("Orders")
+ .should("be.visible")
+ .findByTestId("table-root")
+ .should("exist");
+
+ // Verify window.open was called
+ cy.on("uncaught:exception", (error) => {
+ expect(error.message.includes("expected '<a>' to have attribute")).to.be
+ .false;
+ });
+
+ H.onNextAnchorClick((anchor) => {
+ expect(anchor)
+ .to.have.attr("href")
+ .match(/\/question\//);
+ expect(anchor).to.have.attr("rel", "noopener");
+ expect(anchor).to.have.attr("target", "_blank");
+ });
+
+ // Click on the card title with ctrl/meta key
+ H.getDocumentCard("Orders").findByTestId("card-embed-title").click({
+ metaKey: macOSX,
+ ctrlKey: !macOSX,
+ });
+ });
+
+ it("should open drill-through action in a new tab when clicking with ctrl/meta key", () => {
+ const macOSX = Cypress.platform === "darwin";
+
+ H.createDocument({
+ name: "Test Document",
+ document: DOCUMENT_WITH_TWO_CARDS,
+ collection_id: null,
+ alias: "document",
+ idAlias: "documentId",
+ });
+
+ H.visitDocument("@documentId");
+
+ // Wait for cards to load
+ H.getDocumentCard("Orders, Count")
+ .should("be.visible")
+ .findByTestId("table-root")
+ .should("exist");
+
+ // Click on a table cell to trigger click actions menu
+ H.getDocumentCard("Orders, Count")
+ .findByTestId("table-body")
+ .findAllByTestId("cell-data")
+ .first()
+ .click();
+
+ // Verify window.open was called
+ cy.on("uncaught:exception", (error) => {
+ expect(error.message.includes("expected '<a>' to have attribute")).to.be
+ .false;
+ });
+
+ H.onNextAnchorClick((anchor) => {
+ expect(anchor)
+ .to.have.attr("href")
+ .match(/\/question/);
+ expect(anchor).to.have.attr("rel", "noopener");
+ expect(anchor).to.have.attr("target", "_blank");
+ });
+
+ // Wait for the popover to appear and click the first action with ctrl/meta key
+ H.popover().within(() => {
+ cy.findByText("See these Orders").should("be.visible").click({
+ metaKey: macOSX,
+ ctrlKey: !macOSX,
+ });
+ });
+ });
+ });
+
describe("deleting a cardEmbed", () => {
it("should allow you to remove a card if it is the first item in a docuemnt (UXW-2169)", () => {
cy.visit("/document/new");
diff --git a/frontend/src/metabase/documents/actions.ts b/frontend/src/metabase/documents/actions.ts
index 54cf4155be6..0a1599bb9f6 100644
--- a/frontend/src/metabase/documents/actions.ts
+++ b/frontend/src/metabase/documents/actions.ts
@@ -1,6 +1,7 @@
import { push } from "react-router-redux";
import { NAVIGATE_TO_NEW_CARD } from "metabase/dashboard/actions";
+import { openUrl } from "metabase/redux/app";
import type { Document } from "metabase-types/api";
import type { Dispatch } from "metabase-types/store";
@@ -22,5 +23,5 @@ export const navigateToCardFromDocument =
});
}
- dispatch(push(url));
+ dispatch(openUrl(url));
};