import { classNames } from "@hesxenon/prelude/Dom.js"; import { describe, expect, it, render, select, selectAll, waitFor, } from "../utils/Test"; describe("uwc-toc", () => { const setup = Object.assign( ({ headings = setup.defaultHeadings, props = {}, containerProps = {}, }: { headings?: JSX.Node; props?: Partial>; containerProps?: Partial>; } = {}) => { const page = render(
{headings}
, ); return { toc: select(page, "uwc-toc"), container: select(page, "div.container"), }; }, { defaultHeadings: ( <>

Foo

Bar

Baz

), container: { tiny: { style: { height: "50px", overflow: "auto", }, }, } satisfies Record>, }, ); it("should create a list of all headings", () => { const { toc } = setup(); expect(selectAll(toc, "a").map((link) => link.innerText)).to.deep.eq([ "Foo", "Bar", "Baz", ]); }); it("should always scroll a heading into view, even if the url doesn't change", () => { const { toc, container } = setup({ containerProps: setup.container.tiny }); expect(container.scrollTop).to.eq(0); select(toc, "a").click(); expect(container.scrollTop).to.be.greaterThan(0); }); it("should be possible to specify which heading tags to consider", () => { const { toc } = setup({ headings: ( <>

Foo

Bar

Baz

), props: { headingselector: "h2,h3", }, }); expect(selectAll(toc, "a").map((link) => link.innerText)).to.deep.eq([ "Foo", "Bar", // "Baz", // not included because h4 isn't part of the selectors ]); }); it("should use existing title attributes over textContent", () => { const { toc } = setup({ headings: ( <>

Foo

Bar

Baz

), }); expect(selectAll(toc, "a").map((link) => link.innerText)).to.deep.eq([ "foo", "bar", "baz", ]); }); it.todo("should react to changes in heading elements"); it("should set [aria-current] on the entries according to the scroll position", async () => { const { toc, container } = setup({ containerProps: setup.container.tiny, }); await waitFor(() => expect(select(toc, "li")).to.include({ ariaCurrent: "false" }), ); container.scrollTop = 90; await waitFor(() => expect(select(toc, "li")).to.include({ ariaCurrent: "true" }), ); }); describe("links to headings", () => { it.todo("should be possible to turn off link creation with [nolinks]"); it.todo("should update the url based on the current scroll position"); it.todo("should only consider headings with ids"); }); });