insta-toc
Version:
Simultaneously generate, update, and maintain a table of contents for your notes in real time.
56 lines (51 loc) • 2.05 kB
text/typescript
import { describe, expect, test, vi } from "vitest";
import { TocToolbarState } from "../src/svelte/components/codeblock/TocToolbarState.svelte.ts";
import type { TocBlockItem } from "../src/types";
describe("TocToolbarState", () => {
test("syncFoldsFromItems preserves existing fold state without persisting and prunes removed keys", () => {
// Arrange
const persistedFoldStateSpy = vi.fn();
const persistTocCollapsedSpy = vi.fn();
const toolbarState = new TocToolbarState(persistedFoldStateSpy, persistTocCollapsedSpy);
const items: TocBlockItem[] = [ {
key: "note.md:0",
text: "Parent",
href: "note#parent",
children: [ {
key: "note.md:1",
text: "Child",
href: "note#child",
children: [],
foldKey: null,
initialCollapsed: false
} ],
foldKey: "note.md::1",
initialCollapsed: false
}, {
key: "note.md:2",
text: "Second parent",
href: "note#second-parent",
children: [ {
key: "note.md:3",
text: "Nested child",
href: "note#nested-child",
children: [],
foldKey: null,
initialCollapsed: false
} ],
foldKey: "note.md::2",
initialCollapsed: false
} ];
toolbarState.setCollapsed("note.md::1", true);
persistedFoldStateSpy.mockClear();
// Act
toolbarState.syncFoldsFromItems(items);
toolbarState.syncFoldsFromItems([ items[1] ]);
// Assert
expect(toolbarState.foldsByKey.has("note.md::1")).toBe(false);
expect(toolbarState.foldsByKey.has("note.md::2")).toBe(true);
expect(toolbarState.isCollapsed("note.md::2")).toBe(false);
expect(persistedFoldStateSpy).not.toHaveBeenCalled();
expect(persistTocCollapsedSpy).not.toHaveBeenCalled();
});
});