Unverified Commit cb97ef10 authored by Marius van den Beek's avatar Marius van den Beek Committed by GitHub
Browse files

Merge pull request #17426 from davelopez/23.1_encode_tool_link_consistently

[23.1] Encode tool link consistently
parents 52933ed5 d7877275
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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) {
+36 −0
Original line number Diff line number Diff line
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"));
    });
});