<commit_before>import { setSources } from "../src/lazyload.setSources";
import expectExtend from "./lib/expectExtend";
import getFakeInstance from "./lib/getFakeInstance";
import { getExtendedSettings } from "../src/lazyload.defaults";

expectExtend(expect);

var img, div, settings;

beforeEach(() => {
    div = document.createElement("div");
    div.appendChild((img = document.createElement("img")));
    settings = getExtendedSettings();
});

afterEach(() => {
    div.removeChild(img);
    img = null;
    div = null;
    settings = null;
});

describe("setSources for image", () => {
    let img1 = "1.gif";
    let img200 = "200.gif";
    let img400 = "400.gif";

    test("...with initially empty src and srcset", () => {
        img.setAttribute("data-src", img200);
        img.setAttribute("data-srcset", img400);
        setSources(img, settings, getFakeInstance());
        expect(img).toHaveAttributeValue("src", img200);
        expect(img).toHaveAttributeValue("srcset", img400);
    });

    test("...with initial values in src and srcset", () => {
        img.setAttribute("data-src", img200);
        img.setAttribute("data-srcset", img400);
        img.setAttribute("src", img1);
        img.setAttribute("srcset", img1);
        setSources(img, settings, getFakeInstance());
        expect(img).toHaveAttributeValue("src", img200);
        expect(img).toHaveAttributeValue("srcset", img400);
    });
    test("...with initial values in src and srcset and empty data-*", () => {
        img.setAttribute("data-src", "");
        img.setAttribute("data-srcset", "");
        img.setAttribute("src", img200);
        img.setAttribute("srcset", img400);
        setSources(img, settings, getFakeInstance());
        expect(img).toHaveAttributeValue("src", img200);
        expect(img).toHaveAttributeValue("srcset", img400);
    });
});

describe("setSources for iframe", () => {
    let iframe;
    let srcToLoad = "http://www.google.it";
    let preloadedSrc = srcToLoad + "/doodle";

    beforeEach(() => {
        iframe = document.createElement("iframe");
    });
    test("...with initially empty src", () => {
        iframe.setAttribute("data-src", srcToLoad);
        setSources(iframe, settings, getFakeInstance());
        expect(iframe).toHaveAttributeValue("src", srcToLoad);
    });
    test("...with initial value in src", () => {
        iframe.setAttribute("data-src", srcToLoad);
        iframe.setAttribute("src", preloadedSrc);
        setSources(iframe, settings, getFakeInstance());
        expect(iframe).toHaveAttributeValue("src", srcToLoad);
    });
    test("...with initial value in src and empty data-src", () => {
        iframe.setAttribute("data-src", "");
        iframe.setAttribute("src", preloadedSrc);
        setSources(iframe, settings, getFakeInstance());
        expect(iframe).toHaveAttributeValue("src", preloadedSrc);
    });
});

describe("setSources for background image", () => {
    let element;
    let img100 = "100.gif";
    let img200 = "200.gif";

    beforeEach(() => {
        element = document.createElement("div");
    });

    test("...with initially empty style attribute", () => {
        element.setAttribute("data-src", img200);
        setSources(element, settings, getFakeInstance());
        // Test cheating: bug in JsDOM doesn't return the url("") with quotes inside
        expect(element.style.backgroundImage).toBe(`url(${img200})`);
    });
    test("...with initially present style attribute", () => {
        element.setAttribute("data-src", img100);
        element.style = {
            padding: "1px"
        };
        setSources(element, settings, getFakeInstance());
        // Test cheating: bug in JsDOM doesn't return the url("") with quotes inside
        expect(element.style.backgroundImage).toBe(`url(${img100})`);
    });
    test("...with initially present style and background", () => {
        element.setAttribute("data-src", img200);
        element.style = {
            padding: "1px",
            backgroundImage: "url(" + img100 + ")"
        };
        setSources(element, settings, getFakeInstance());
        // Test cheating: bug in JsDOM doesn't return the url("") with quotes inside
        expect(element.style.backgroundImage).toBe(`url(${img200})`);
    });
});

describe("setSources for video", () => {
    let source1, source2, video;<commit_msg>Extended and adapted setSourcesTests<commit_after>import {
    setSources,
    setBackgroundFromDataSrc,
    setBackgroundFromDataBg
} from "../src/lazyload.setSources";
import expectExtend from "./lib/expectExtend";
import getFakeInstance from "./lib/getFakeInstance";
import { getExtendedSettings } from "../src/lazyload.defaults";

expectExtend(expect);

var img, div, settings;

beforeEach(() => {
    div = document.createElement("div");
    div.appendChild((img = document.createElement("img")));
    settings = getExtendedSettings();
});

afterEach(() => {
    div.removeChild(img);
    img = null;
    div = null;
    settings = null;
});

describe("setSources for image", () => {
    let img1 = "1.gif";
    let img200 = "200.gif";
    let img400 = "400.gif";

    test("...with initially empty src and srcset", () => {
        img.setAttribute("data-src", img200);
        img.setAttribute("data-srcset", img400);
        setSources(img, settings, getFakeInstance());
        expect(img).toHaveAttributeValue("src", img200);
        expect(img).toHaveAttributeValue("srcset", img400);
    });

    test("...with initial values in src and srcset", () => {
        img.setAttribute("data-src", img200);
        img.setAttribute("data-srcset", img400);
        img.setAttribute("src", img1);
        img.setAttribute("srcset", img1);
        setSources(img, settings, getFakeInstance());
        expect(img).toHaveAttributeValue("src", img200);
        expect(img).toHaveAttributeValue("srcset", img400);
    });
    test("...with initial values in src and srcset and empty data-*", () => {
        img.setAttribute("data-src", "");
        img.setAttribute("data-srcset", "");
        img.setAttribute("src", img200);
        img.setAttribute("srcset", img400);
        setSources(img, settings, getFakeInstance());
        expect(img).toHaveAttributeValue("src", img200);
        expect(img).toHaveAttributeValue("srcset", img400);
    });
});

describe("setSources for iframe", () => {
    let iframe;
    let srcToLoad = "http://www.google.it";
    let preloadedSrc = srcToLoad + "/doodle";

    beforeEach(() => {
        iframe = document.createElement("iframe");
    });
    test("...with initially empty src", () => {
        iframe.setAttribute("data-src", srcToLoad);
        setSources(iframe, settings, getFakeInstance());
        expect(iframe).toHaveAttributeValue("src", srcToLoad);
    });
    test("...with initial value in src", () => {
        iframe.setAttribute("data-src", srcToLoad);
        iframe.setAttribute("src", preloadedSrc);
        setSources(iframe, settings, getFakeInstance());
        expect(iframe).toHaveAttributeValue("src", srcToLoad);
    });
    test("...with initial value in src and empty data-src", () => {
        iframe.setAttribute("data-src", "");
        iframe.setAttribute("src", preloadedSrc);
        setSources(iframe, settings, getFakeInstance());
        expect(iframe).toHaveAttributeValue("src", preloadedSrc);
    });
});

describe("setSources for background image with data-src", () => {
    let element;
    let img100 = "100.gif";
    let img200 = "200.gif";

    beforeEach(() => {
        element = document.createElement("div");
        element.llTempImage = document.createElement("img");
    });

    test("...with initially empty style attribute", () => {
        element.setAttribute("data-src", img200);
        setBackgroundFromDataSrc(element, settings, getFakeInstance());
        // Test cheating: bug in JsDOM doesn't return the url("") with quotes inside
        expect(element.style.backgroundImage).toBe(`url(${img200})`);
    });
    test("...with initially present style attribute", () => {
        element.setAttribute("data-src", img100);
        element.style = {
            padding: "1px"
        };
        setBackgroundFromDataSrc(element, settings, getFakeInstance());
        // Test cheating: bug in JsDOM doesn't return the url("") with quotes inside
        expect(element.style.backgroundImage).toBe(`url(${img100})`);
    });
    test("...with initially present style and background", () => {
        element.setAttribute("data-src", img200);
        element.style = {
            padding: "1px",
            backgroundImage: `url(${img100})`
        };
        setBackgroundFromDataSrc(element, settings, getFakeInstance());
        // Test cheating: bug in JsDOM doesn't return the url("") with quotes inside
        expect(element.style.backgroundImage).toBe(`url(${img200})`);
    });
});

describe("setSources for background image with data-bg", () => {
    let element;
    let img100 = "100.gif";
    let img200 = "200.gif";

    beforeEach(() => {
        element = document.createElement("div");
    });

    test("...with initially empty style attribute", () => {
        element.setAttribute("data-bg", `url(${img200})`);
        setBackgroundFromDataBg(element, settings, getFakeInstance());
        // Test cheating: bug in JsDOM doesn't return the url("") with quotes inside
        expect(element.style.backgroundImage).toBe(`url(${img200})`);
    });
    test("...with initially present style attribute", () => {
        element.setAttribute("data-bg", `url(${img100})`);
        element.style = {
            padding: "1px"
        };
        setBackgroundFromDataBg(element, settings, getFakeInstance());
        // Test cheating: bug in JsDOM doesn't return the url("") with quotes inside
        expect(element.style.backgroundImage).toBe(`url(${img100})`);
    });
    test("...with initially present style and background", () => {
        element.setAttribute("data-bg", `url(${img200})`);
        element.style = {
            padding: "1px",
            backgroundImage: `url(${img100})`
        };
        setBackgroundFromDataBg(element, settings, getFakeInstance());
        // Test cheating: bug in JsDOM doesn't return the url("") with quotes inside
        expect(element.style.backgroundImage).toBe(`url(${img200})`);
    });
});

describe("setSources for video", () => {
    let source1, source2, video;
{'fraction_non_alphanumeric': 0.16570605187319884, 'fraction_numerical': 0.020461095100864555, 'mean_word_length': 3.0811446491571934, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '914255', 'n_tokens_mistral': 3067, 'n_tokens_neox': 2906, 'n_words': 636}