Skip to content

Commit

Permalink
Merge pull request galaxyproject#17426 from davelopez/23.1_encode_too…
Browse files Browse the repository at this point in the history
…l_link_consistently

[23.1] Encode tool link consistently
  • Loading branch information
mvdbeek authored Feb 8, 2024
2 parents 52933ed + d787727 commit cb97ef1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion client/src/components/Tool/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { getAppRoot } from "onload/loadConfig";
import { copy } from "utils/clipboard";

export function copyLink(toolId, message) {
copy(`${window.location.origin + getAppRoot()}root?tool_id=${toolId}`, message);
const link = `${window.location.origin + getAppRoot()}root?tool_id=${toolId}`;
// Encode the link to handle special characters in tool id
copy(encodeURI(link), message);
}

export function copyId(toolId, message) {
Expand Down
36 changes: 36 additions & 0 deletions client/src/components/Tool/utilities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { copyLink } from "./utilities";

const writeText = jest.fn();

Object.assign(navigator, {
clipboard: {
writeText,
},
});

describe("copyLink", () => {
beforeEach(() => {
(navigator.clipboard.writeText as jest.Mock).mockResolvedValue(undefined);
});

it("should copy the link to the clipboard", () => {
const toolId = "MyToolId";
copyLink(toolId);
expect(writeText).toHaveBeenCalledTimes(1);
expect(writeText).toHaveBeenCalledWith(expect.stringContaining(toolId));
});

it("should encode the tool id with spaces", () => {
const toolId = "My Tool Id";
copyLink(toolId);
expect(writeText).toHaveBeenCalledTimes(1);
expect(writeText).toHaveBeenCalledWith(expect.stringContaining("My%20Tool%20Id"));
});

it("should not encode the character '+' in the tool id", () => {
const toolId = "My Tool Id+1";
copyLink(toolId);
expect(writeText).toHaveBeenCalledTimes(1);
expect(writeText).toHaveBeenCalledWith(expect.stringContaining("My%20Tool%20Id+1"));
});
});

0 comments on commit cb97ef1

Please sign in to comment.