Skip to content

Commit

Permalink
Links to metabase URLs should open in the same window in link cards (#…
Browse files Browse the repository at this point in the history
…41508)

* external link cards should apply the correct target attribute

* add unit tests for absolute and relative question links in link cards
  • Loading branch information
heypoom committed Apr 18, 2024
1 parent 4a3cdd0 commit e21201c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit e21201c

Please sign in to comment.