import replaceUrlsWithCdnUrls from "@insite/client-framework/Common/Utilities/replaceUrlsWithCdnUrls";

describe("relative /userfile urls in json should be prefixed with cdn", () => {
    const cdnPrefix = "HTTP://CDN.INSITE.COM";
    const relativeImagePath = "/userFIles/anImageValue.jpg";

    const runTest = (sourceObject: object, resultObject: object) => {
        expect(replaceUrlsWithCdnUrls(cdnPrefix, JSON.stringify(sourceObject))).toStrictEqual(
            JSON.stringify(resultObject),
        );
    };

    test("standard fields should be updated", () => {
        runTest(
            {
                key: relativeImagePath,
            },
            {
                key: `${cdnPrefix}${relativeImagePath}`.toLowerCase(),
            },
        );
    });
    test("escaped html should be updated", () => {
        runTest(
            {
                key: `<img src=\\\"${relativeImagePath}\\\"/> <style>.my-class: { background-image: url('${relativeImagePath}');}</style>`,
            },
            {
                key: `<img src=\\"${cdnPrefix.toLowerCase()}${relativeImagePath.toLowerCase()}\\\"/> <style>.my-class: { background-image: url(\'${cdnPrefix.toLowerCase()}${relativeImagePath.toLowerCase()}\');}</style>`,
            },
        );
    });

    test("unescaped urls should be updated", () => {
        runTest(
            {
                key: `<style>.my-class: { background-image: url(${relativeImagePath});}</style>`,
            },
            {
                key: `<style>.my-class: { background-image: url(${cdnPrefix.toLowerCase()}${relativeImagePath.toLowerCase()});}</style>`,
            },
        );
    });
});
