<commit_before>import { parseDOM } from "htmlparser2";
import type { Element } from "domhandler";
import {
    getSiblings,
    hasAttrib,
    nextElementSibling,
    prevElementSibling,
} from "./traversal";

describe("traversal", () => {
    describe("getSiblings", () => {
        it("returns an element's siblings", () => {
            const dom = parseDOM("<div><h1></h1><p><p><p></div>")[0] as Element;

            expect(getSiblings(dom.children[1])).toHaveLength(4);
        });

        it("returns a root element's siblings", () => {
            const dom = parseDOM("<h1></h1><p><p><p>") as Element[];

            expect(getSiblings(dom[2])).toHaveLength(4);
        });
    });

    describe("hasAttrib", () => {
        it("doesn't throw on text nodes", () => {
            const [firstNode] = parseDOM("textnode");

            expect(() =>
                hasAttrib(firstNode as never, "some-attrib")
            ).not.toThrow();
        });

        it("returns `false` for Object prototype properties", () => {
            const dom = parseDOM(
                "<div><h1></h1>test<p></p></div>"
            )[0] as Element;

            expect(hasAttrib(dom, "constructor")).toBeFalsy();
        });
    });

    describe("nextElementSibling", () => {
        it("return Element if found", () => {
            const dom = parseDOM(
                "<div><h1></h1>test<p></p></div>"
            )[0] as Element;
            const firstNode = dom.children[0];

            const next = nextElementSibling(firstNode);
            expect(next?.tagName).toBe("p");
        });
        it("return null if not found", () => {
            const dom = parseDOM("<div><p></p>test</div>")[0] as Element;
            const firstNode = dom.children[0];

            expect(nextElementSibling(firstNode)).toBeNull();
        });
        it("does not ignore script tags", () => {
            const dom = parseDOM(
                "<div><p></p><script></script><p></div>"
            )[0] as Element;
            const firstNode = dom.children[0];

            const next = nextElementSibling(firstNode);
            expect(next?.tagName).toBe("script");
        });
    });

    describe("prevElementSibling", () => {
        it("return Element if found", () => {
            const dom = parseDOM(
                "<div><h1></h1>test<p></p></div>"
            )[0] as Element;
            const lastNode = dom.children[2];

            const prev = prevElementSibling(lastNode);
            expect(prev?.tagName).toBe("h1");
        });
        it("return null if not found", () => {
            const dom = parseDOM("<div>test<p></p></div>")[0] as Element;
            const lastNode = dom.children[1];

            expect(nextElementSibling(lastNode)).toBeNull();
        });
        it("does not ignore script tags", () => {
            const dom = parseDOM(
                "<div><p></p><script></script><p></p></div>"
            )[0] as Element;
            const lastNode = dom.children[2];

            const prev = prevElementSibling(lastNode);
            expect(prev?.tagName).toBe("script");
        });
    });
});<commit_msg>test(traversal): Call expected fn<commit_after>import { parseDOM } from "htmlparser2";
import type { Element } from "domhandler";
import {
    getSiblings,
    hasAttrib,
    nextElementSibling,
    prevElementSibling,
} from "./traversal";

describe("traversal", () => {
    describe("getSiblings", () => {
        it("returns an element's siblings", () => {
            const dom = parseDOM("<div><h1></h1><p><p><p></div>")[0] as Element;

            expect(getSiblings(dom.children[1])).toHaveLength(4);
        });

        it("returns a root element's siblings", () => {
            const dom = parseDOM("<h1></h1><p><p><p>") as Element[];

            expect(getSiblings(dom[2])).toHaveLength(4);
        });
    });

    describe("hasAttrib", () => {
        it("doesn't throw on text nodes", () => {
            const [firstNode] = parseDOM("textnode");

            expect(() =>
                hasAttrib(firstNode as never, "some-attrib")
            ).not.toThrow();
        });

        it("returns `false` for Object prototype properties", () => {
            const dom = parseDOM(
                "<div><h1></h1>test<p></p></div>"
            )[0] as Element;

            expect(hasAttrib(dom, "constructor")).toBeFalsy();
        });
    });

    describe("nextElementSibling", () => {
        it("return Element if found", () => {
            const dom = parseDOM(
                "<div><h1></h1>test<p></p></div>"
            )[0] as Element;
            const firstNode = dom.children[0];

            const next = nextElementSibling(firstNode);
            expect(next).toHaveProperty("tagName", "p");
        });
        it("return null if not found", () => {
            const dom = parseDOM("<div><p></p>test</div>")[0] as Element;
            const firstNode = dom.children[0];

            expect(nextElementSibling(firstNode)).toBeNull();
        });
        it("does not ignore script tags", () => {
            const dom = parseDOM(
                "<div><p></p><script></script><p></div>"
            )[0] as Element;
            const firstNode = dom.children[0];

            const next = nextElementSibling(firstNode);
            expect(next).toHaveProperty("tagName", "script");
        });
    });

    describe("prevElementSibling", () => {
        it("return Element if found", () => {
            const dom = parseDOM(
                "<div><h1></h1>test<p></p></div>"
            )[0] as Element;
            const lastNode = dom.children[2];

            const prev = prevElementSibling(lastNode);
            expect(prev).toHaveProperty("tagName", "h1");
        });
        it("return null if not found", () => {
            const dom = parseDOM("<div>test<p></p></div>")[0] as Element;
            const lastNode = dom.children[1];

            expect(prevElementSibling(lastNode)).toBeNull();
        });
        it("does not ignore script tags", () => {
            const dom = parseDOM(
                "<div><p></p><script></script><p></p></div>"
            )[0] as Element;
            const lastNode = dom.children[2];

            const prev = prevElementSibling(lastNode);
            expect(prev).toHaveProperty("tagName", "script");
        });
    });
});
{'fraction_non_alphanumeric': 0.18020144790683035, 'fraction_numerical': 0.009442870632672332, 'mean_word_length': 1.9948162111215835, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7281805', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1833, 'n_words': 353}