/** @jest-environment jsdom */

import getLinkElement, {
    appendLinkElement,
    getCanonical,
    getCurrentQueryString,
    getlinkElementList,
    getPrevCurrentNextQueryString,
    hasLinksWithRelPrevNext,
    removeLinkElement,
    updateCanonical,
} from "@insite/mobius/utilities/linkElementsManipulation";

describe("linkeElementsManipulation", () => {
    const p =
        "http://localhost:3000/api/v2/products?categoryid=d4bb9e73-3ee8-4cac-8eea-ac7e0034d6e7&amp;includesuggestions=true&amp;applypersonalization=true&amp;expand=attributes%2Cfacets%2CvariantTraits%2Cbadges&amp;includeattributes=includeOnProduct&amp;page=1";
    const n =
        "http://localhost:3000/api/v2/products?categoryid=d4bb9e73-3ee8-4cac-8eea-ac7e0034d6e7&amp;includesuggestions=true&amp;applypersonalization=true&amp;expand=attributes%2Cfacets%2CvariantTraits%2Cbadges&amp;includeattributes=includeOnProduct&amp;page=3";

    const c =
        "http://localhost:3000/api/v2/products?categoryid=d4bb9e73-3ee8-4cac-8eea-ac7e0034d6e7&amp;includesuggestions=true&amp;applypersonalization=true&amp;expand=attributes%2Cfacets%2CvariantTraits%2Cbadges&amp;includeattributes=includeOnProduct&amp;page=2";

    const currentPage = 1;

    const filterQuery = "query=testPromotions&pageSize=8";

    test("There are not links with relPrev and relnext", () => {
        expect(hasLinksWithRelPrevNext(p, n)).toEqual({ hasPrev: false, hasNext: false });
    });

    test("There are links with relPrev and relnext", () => {
        appendLinkElement("prev", p);
        appendLinkElement("next", n);
        expect(hasLinksWithRelPrevNext(p, n)).toEqual({ hasPrev: true, hasNext: true });
    });

    test("The link with relPrev has been removed", () => {
        removeLinkElement("prev", p);
        expect(hasLinksWithRelPrevNext(p, n)).toEqual({ hasPrev: false, hasNext: true });
    });

    test("returns an array with length 1", () => {
        expect(getlinkElementList()).toHaveLength(1);
    });

    test("There is a link with rel next ", () => {
        expect(getLinkElement("next", n)).toBeDefined();
    });

    test("Has current and  Next Query ", () => {
        expect(getPrevCurrentNextQueryString("", c, currentPage, filterQuery)).toEqual({
            prevQueryString: "",
            currentQueryString: "?query=testPromotions&page=1&pageSize=8",
            nextQueryString: "?query=testPromotions&page=2&pageSize=8",
        });
    });

    test("Has Canonical ", () => {
        expect(getCanonical()).toBeDefined();
    });

    test("Query string is null", () => {
        expect(getCurrentQueryString()).toEqual("");
    });

    test("Canonical is updated", () => {
        updateCanonical("?page=1");
        expect(document.querySelector('link[rel="canonical"]')).not.toBe(null);
    });
});
