Skip to content
Snippets Groups Projects
Unverified Commit e21201cb authored by Phoomparin Mano's avatar Phoomparin Mano Committed by GitHub
Browse files

Links to metabase URLs should open in the same window in link cards (#41508)

* external link cards should apply the correct target attribute

* add unit tests for absolute and relative question links in link cards
parent 4a3cdd0e
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ import _ from "underscore";
import TippyPopover from "metabase/components/Popover/TippyPopover";
import Search from "metabase/entities/search";
import { useToggle } from "metabase/hooks/use-toggle";
import { getUrlTarget } from "metabase/lib/dom";
import { SearchResults } from "metabase/nav/components/search/SearchResults";
import type {
LinkCardSettings,
......@@ -181,7 +182,11 @@ function LinkVizInner({
data-testid="custom-view-text-link"
fade={isEditingParameter}
>
<ExternalLink href={url ?? ""} target="_blank" rel="noreferrer">
<ExternalLink
href={url ?? ""}
target={getUrlTarget(url)}
rel="noreferrer"
>
<UrlLinkDisplay url={url} />
</ExternalLink>
</DisplayLinkCardWrapper>
......
......@@ -159,6 +159,41 @@ describe("LinkViz", () => {
expect(screen.getByText("https://example23.com")).toBeInTheDocument();
expect(screen.getByRole("link")).toHaveAttribute("target", "_blank");
});
it("should open absolute links to question in the same tab", () => {
const dashcard = createMockLinkDashboardCard({
url: "http://localhost/question/1-example",
});
setup({
isEditing: false,
dashcard,
settings: dashcard.visualization_settings as LinkCardVizSettings,
});
expect(window.location.origin).toBe("http://localhost");
expect(
screen.getByText("http://localhost/question/1-example"),
).toBeInTheDocument();
expect(screen.getByRole("link")).toHaveAttribute("target", "_self");
});
it("should open relative links to question in the same tab", () => {
const dashcard = createMockLinkDashboardCard({
url: "question/2-example",
});
setup({
isEditing: false,
dashcard,
settings: dashcard.visualization_settings as LinkCardVizSettings,
});
expect(screen.getByText("question/2-example")).toBeInTheDocument();
expect(screen.getByRole("link")).toHaveAttribute("target", "_self");
});
});
describe("entity links", () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment