describe("RowLock", () => {
    let fixture, row1, row2;

    function makeRow(label) {
        const li = document.createElement("li");
        li.className = "applet-list-entry row-3col";
        li.innerHTML = `
            <a class="row-title" href="#">${label}</a>
            <span class="row-body">body</span>
            <time class="row-ts">ts</time>
        `;
        return li;
    }

    beforeEach(() => {
        RowLock._testReset();
        fixture = document.createElement("ul");
        fixture.className = "applet-list";
        row1 = makeRow("Row 1");
        row2 = makeRow("Row 2");
        fixture.appendChild(row1);
        fixture.appendChild(row2);
        document.body.appendChild(fixture);
        RowLock._init();
    });

    afterEach(() => {
        RowLock._testReset();
        fixture.remove();
    });

    it("clicking a row adds .row-locked", () => {
        row1.click();
        expect(row1.classList.contains("row-locked")).toBe(true);
    });

    it("clicking the same row again clears the lock", () => {
        row1.click();
        row1.click();
        expect(row1.classList.contains("row-locked")).toBe(false);
    });

    it("clicking a different row moves the lock", () => {
        row1.click();
        row2.click();
        expect(row1.classList.contains("row-locked")).toBe(false);
        expect(row2.classList.contains("row-locked")).toBe(true);
    });

    it("clicking outside any row clears the lock", () => {
        row1.click();
        document.body.click();
        expect(row1.classList.contains("row-locked")).toBe(false);
    });

    it("clicking a child element of the row still locks the row", () => {
        const body = row1.querySelector(".row-body");
        body.click();
        expect(row1.classList.contains("row-locked")).toBe(true);
    });

    it("only one row carries .row-locked at a time", () => {
        row1.click();
        row2.click();
        const locked = document.querySelectorAll(".applet-list-entry.row-3col.row-locked");
        expect(locked.length).toBe(1);
    });
});
