# test.web/presentation/selectors/section.selector.test.ts

> 76 lines of code and 0 definitions.

Tree: Codebase Testing
Language: typescript
Canonical: https://banes-lab.com/anatomy/testing#file-testing-test-web-presentation-selectors-section-selector-test-ts
Source text: https://banes-lab.com/source/testing/test.web/presentation/selectors/section.selector.test.ts.txt

Listed in [test.web/presentation/selectors](https://banes-lab.com/api/source/testing/test.web/presentation/selectors.md).

## Contained in

- [test.web/presentation/selectors](https://banes-lab.com/anatomy/testing/folder-testing-test-web-presentation-selectors.md)

## Source

```typescript
import "@banes-lab/web/presentation/records/home.record.ts";
import "@banes-lab/web/presentation/records/ontology.record.ts";
import { HOME_PAGE, ONTOLOGY_PAGE } from "@banes-lab/web/core/ids/page.ids.ts";
import { describe, expect, it } from "vitest";
import { rowRanges, sectionSeedsOf } from "@banes-lab/web/presentation/selectors/section.selector.ts";
import type { SectionSeed } from "@banes-lab/web/types/page.types.ts";
import { loadCompletePage } from "@banes-lab/web/domain/registries/page.registry.ts";
import { missingSection } from "@banes-lab/web/configuration/strings/report.strings.ts";
import { refOfAnchor } from "@banes-lab/web/domain/converters/ontology.converter.ts";
import { registerRecords } from "@banes-lab/web/domain/registries/record.registry.ts";
import { renderSectionPage } from "@banes-lab/web/presentation/renderers/page.shell.renderer.ts";

await loadCompletePage(HOME_PAGE);
await loadCompletePage(ONTOLOGY_PAGE);

const WIDE = 1_000_000_000;

const subjectOf = function subjectOf(seed: SectionSeed): Parameters<typeof renderSectionPage>[0] {
    return { alternates: { json: "", markdown: "" }, description: "", name: seed.name, parts: [], path: "", seed, title: "" };
};

describe("rowRanges", () => {
    it("packs rows into ranges under the budget, a row over the budget alone in its range", () => {
        expect(rowRanges([3, 3, 3], 10)).toStrictEqual([[0, 3]]);
        expect(rowRanges([4, 4, 4], 8)).toStrictEqual([
            [0, 2],
            [2, 3],
        ]);
        expect(rowRanges([20, 1], 8)).toStrictEqual([
            [0, 1],
            [1, 2],
        ]);
    });
});

describe("sectionSeedsOf", () => {
    it("lists every section of a page's explorer tabs that holds rows and is no published record", () => {
        const seeds = sectionSeedsOf(ONTOLOGY_PAGE, WIDE);
        expect(seeds.length).toBeGreaterThan(0);
        expect(seeds.every((seed) => seed.records > 0 && seed.page === ONTOLOGY_PAGE && seed.parts === 1)).toBe(true);
        const [first] = seeds;
        const ref = refOfAnchor(first?.section ?? "");
        if (ref === null) {
            throw new Error("the first section names no record");
        }
        registerRecords([ref]);
        expect(sectionSeedsOf(ONTOLOGY_PAGE, WIDE).some((seed) => seed.section === first?.section)).toBe(false);
    });

    it("splits a section whose rows pass the budget into numbered parts that cover every row once", () => {
        const [seed] = sectionSeedsOf(ONTOLOGY_PAGE, WIDE).filter((candidate) => candidate.records > 1);
        if (seed === undefined) {
            throw new Error(ONTOLOGY_PAGE);
        }
        const parts = sectionSeedsOf(ONTOLOGY_PAGE, 1).filter((candidate) => candidate.section === seed.section);
        expect(parts).toHaveLength(seed.records);
        expect(parts.map((part) => part.rows)).toStrictEqual(parts.map((_, index) => [index, index + 1]));
        expect(parts.map((part) => part.part)).toStrictEqual(parts.map((_, index) => index + 1));
    });

    it("draws a section page with its tab link, the tab title and its rows, and refuses an unknown section", () => {
        const [seed] = sectionSeedsOf(ONTOLOGY_PAGE, WIDE);
        if (seed === undefined) {
            throw new Error(ONTOLOGY_PAGE);
        }
        const page = renderSectionPage(subjectOf(seed));
        expect(page.querySelector("h1")?.textContent).toBe(seed.tabTitle);
        expect(page.querySelector("nav a")?.getAttribute("href")).toBe(seed.tabPath);
        expect(page.querySelector(`[id="${seed.section}"]`)).not.toBeNull();
        expect(page.querySelectorAll(".record")).toHaveLength(seed.records);
        expect(page.querySelector(".section-parts")).toBeNull();
        const sliced = renderSectionPage({ ...subjectOf({ ...seed, rows: [0, 1] }), parts: [{ label: "a", path: "/a" }, { label: "b", path: "/b" }] });
        expect(sliced.querySelectorAll(".record")).toHaveLength(1);
        expect(sliced.querySelectorAll(".section-parts a")).toHaveLength(2);
        expect(() => renderSectionPage(subjectOf({ ...seed, section: "none" }))).toThrow(
            missingSection(seed.page, seed.tab, "none"),
        );
    });

    it("lists nothing for a page without explorer tabs", () => {
        expect(sectionSeedsOf(HOME_PAGE, WIDE)).toStrictEqual([]);
        expect(sectionSeedsOf("no-such-page", WIDE)).toStrictEqual([]);
    });
});
```
