Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
/**
* Regression test for #10689: a newly added board column is persisted but not rendered until the
* view is re-entered, and a subsequent column reorder then deletes it again.
*/
import $ from "jquery";
import { render } from "preact";
import { useCallback, useEffect, useState } from "preact/hooks";
import { act } from "preact/test-utils";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { Modal as BootstrapModal, Tooltip } from "bootstrap";
import appContext from "../../../components/app_context";
import Component from "../../../components/component";
import contextMenu from "../../../menus/context_menu";
import dialog from "../../../services/dialog";
import server from "../../../services/server";
import toast from "../../../services/toast";
import ws from "../../../services/ws";
import FBranch from "../../../entities/fbranch";
import froca from "../../../services/froca";
import attributes from "../../../services/attributes";
import branches from "../../../services/branches";
import { executeBulkActions } from "../../../services/bulk_action";
import LoadResults from "../../../services/load_results";
import noteAttributeCache from "../../../services/note_attribute_cache";
import searchService from "../../../services/search";
import { buildNote } from "../../../test/easy-froca";
import { highlightedTexts } from "../../../test/highlights";
import { ParentComponent } from "../../react/react_utils";
import BoardView, { BoardViewData, loadBoardEditing } from ".";
import { getNoteTypeOptions, type NoteTypeOption } from "../../../services/note_types";
import { collectShortcutHints } from "../../../services/shortcut_hints";
import BoardApi, { getPendingWrites } from "./api";
import { RAIL_EXIT_MS } from "./card_toolbar";
import { DEFAULT_COLUMN_ICON } from "./columns";
import { TREE_CLIPBOARD_TYPE } from "../../react/hooks";
// Stands in for the server: by the time the bulk action resolves, the notes carry the new value,
// which is what makes the old column empty rather than merely renamed.
vi.mock("../../../services/i18n", () => ({
// i18next is never initialised under test, so a stock name the board writes would be undefined.
// What is interpolated is carried along, for the strings a test is about.
t: (key: string, opts?: Record<string, unknown>) =>
opts ? `${key}:${JSON.stringify(opts)}` : key,
// Awaited by whatever waits for the catalogue; a mock without it rejects where it is read.
translationsInitializedPromise: $.Deferred().resolve()
}));
// The picker is a Bootstrap dropdown portalled to the page, which happy-dom does not open. What the
// board answers for is the icon it shows and what it does with a pick, so the button stands in for
// the picker and hands one over when it is pressed.
// What a card can be made from. The listing itself is the app's and is tested there; the board is
// handed a short list of it, the four it offers by default and one it does not.
// Hoisted with the mock, which is lifted above everything a test file declares.
const layout = vi.hoisted(() => ({ onMobile: false }));
// Spread rather than replaced: the board reads far more of this than the one export a test steers.
vi.mock("../../../services/utils", async (importOriginal) => ({
...(await importOriginal<typeof import("../../../services/utils")>()),
isMobile: () => layout.onMobile
}));
vi.mock("../../../services/note_types", async (importOriginal) => ({
...(await importOriginal<typeof import("../../../services/note_types")>()),
getNoteTypeOptions: vi.fn(async () => {
templateReads++;
return NOTE_TYPE_OPTIONS;
}),
noteTypeOptionGroupTitle: (group: string) => `group:${group}`,
default: {}
}));
/** How many times the board has asked what a card can be made from. */
let templateReads = 0;
const NOTE_TYPE_OPTIONS = [
[ "type:text:text/html", "Text", "text", "text/html", "bx bx-note" ],
[ "type:code:text/x-markdown", "Markdown", "code", "text/x-markdown", "bx bx-code" ],
[ "type:canvas:application/json", "Canvas", "canvas", "application/json", "bx bx-pen" ],
[ "type:spreadsheet:application/json", "Spreadsheet", "spreadsheet", "application/json",
"bx bx-table" ],
[ "type:mermaid:text/mermaid", "Mermaid", "mermaid", "text/mermaid", "bx bx-selection" ]
].map(([ id, title, type, mime, icon ]) => ({
id, title, icon, group: "type" as const, options: { type, mime }
})) as NoteTypeOption[];
vi.mock("../../react/IconPicker", () => ({
IconPickerButton: ({ className, icon, onSelect, onOpened, onClosed }: {
className?: string, icon: string, onSelect: (picked: string) => void,
onOpened?: () => void, onClosed?: () => void
}) => (
<span className={className}>
<button className={icon} onClick={() => {
onOpened?.();
onSelect(PICKED_ICON);
onClosed?.();
}} />
{/* The picker opening and closing on their own, for what the editor does in between.
Not buttons: a host counting the buttons its picker draws would count these too. */}
<span className="picker-open" onClick={() => onOpened?.()} />
<span className="picker-close" onClick={() => onClosed?.()} />
</span>
)
}));
/** What the picker hands over when it is pressed under test. */
const PICKED_ICON = "bx bx-star";
vi.mock("../../../services/bulk_action", () => ({
executeBulkActions: vi.fn(async (
noteIds: string[],
actions: { name: string, labelName?: string, labelValue?: string }[]
) => {
for (const noteId of noteIds) {
for (const attribute of froca.getNoteFromCache(noteId)?.getAttributes() ?? []) {
for (const action of actions) {
if (action.name === "updateLabelValue" && attribute.name === action.labelName) {
attribute.value = action.labelValue ?? "";
}
}
}
}
})
}));
vi.mock("../../../services/search", () => ({
default: {
searchInSubtree: vi.fn(),
searchForNoteIds: vi.fn(async () => []),
searchForNotes: vi.fn(async () => [])
}
}));
const searchInSubtree = vi.mocked(searchService.searchInSubtree);
/** What a card is made from until another template is picked: the first the board offers. */
function textTemplate() {
return expect.objectContaining({ id: "type:text:text/html" });
}
/** Drains the async chain inside `refresh()` (getBoardData → setByColumn/setColumns). */
async function flush() {
await new Promise((resolve) => setTimeout(resolve));
}
/** Lets one animation frame run. */
async function nextFrame() {
await new Promise((resolve) => requestAnimationFrame(resolve));
}
/** Fills a field the way typing does, so that what watches the field hears about it. */
async function type(field: HTMLInputElement | HTMLTextAreaElement, value: string) {
await act(async () => {
field.value = value;
field.dispatchEvent(new Event("input", { bubbles: true }));
});
}
const saved: BoardViewData[] = [];
/**
* Mirrors how `useViewModeConfig` feeds the board: `saveConfig` publishes a *new wrapper* around the
* config it was handed, so the `viewConfig` prop only changes identity if the caller passed a new
* object. That is exactly the condition the board's refresh effect depends on.
*/
function Harness({ note, noteIds, initialConfig, isReadOnly }: { note: ReturnType<typeof buildNote>, noteIds: string[], initialConfig: BoardViewData, isReadOnly?: boolean }) {
const [ state, setState ] = useState<{ config: BoardViewData }>({ config: initialConfig });
const saveConfig = useCallback((config: BoardViewData) => {
saved.push(config);
setState({ config });
}, []);
// `useViewModeConfig` restores the config of whichever note it is handed, so a board shown
// after another starts from its own. The board itself is not remounted, which is the point.
useEffect(() => {
setState({ config: initialConfig });
}, [ note ]);
return (
<BoardView
note={note}
notePath={`root/${note.noteId}`}
noteIds={noteIds}
highlightedTokens={null}
viewConfig={state.config}
saveConfig={saveConfig}
media="screen"
onReady={() => {}}
isReadOnly={isReadOnly}
/>
);
}
/** Files a fresh note under the board, the way an import or another client's write reaches it. */
function addCard(board: ReturnType<typeof buildNote>, status: string) {
return fileCard(board, buildNote({ title: "Added", "#status": status }));
}
/** Takes a card off the board, the way carrying it to another one does. */
function unfileCard(board: ReturnType<typeof buildNote>, card: ReturnType<typeof buildNote>) {
delete froca.branches[`${board.noteId}_${card.noteId}`];
delete board.childToBranch[card.noteId];
board.children = board.children.filter(noteId => noteId !== card.noteId);
}
/** Puts a note the test already holds under the board, for one made before it is filed. */
function fileCard(board: ReturnType<typeof buildNote>, card: ReturnType<typeof buildNote>) {
const branchId = `${board.noteId}_${card.noteId}`;
froca.branches[branchId] = new FBranch(froca, {
branchId,
notePosition: 100,
fromSearchNote: false,
noteId: card.noteId,
parentNoteId: board.noteId
});
board.addChild(card.noteId, branchId, false);
return card;
}
/** Opens the title editor the way the keyboard does, the menu entry needing a rendered menu. */
function startEditingTitle(column: HTMLElement) {
column.querySelector("h3")
?.dispatchEvent(new KeyboardEvent("keydown", { key: "F2", bubbles: true }));
}
/** The icon class each column header wears, which is the class its picker button carries. */
function columnIcons(container: HTMLElement) {
return [ ...container.querySelectorAll(".board-column h3 > .column-icon button") ]
.map(el => [ ...el.classList ].filter(name => name.startsWith("bx")).join(" "));
}
/**
* A stand-in note context, with the two calls every mounted board makes of one: it publishes its
* columns into the context for the right pane to list, and clears them as it goes.
*/
function contextStub(context: object) {
return { setContextData: () => {}, clearContextData: () => {}, ...context };
}
function columnTitles(container: HTMLElement) {
return [ ...container.querySelectorAll(".board-column h3 .title") ].map(el => el.textContent);
}
// An editable board draws once its editing code has loaded, which the app fetches on demand.
beforeAll(() => loadBoardEditing());
describe("Collapsed board columns", () => {
let container: HTMLElement | undefined;
afterEach(() => {
vi.useRealTimers();
saved.length = 0;
if (container) {
render(null, container);
container.remove();
container = undefined;
}
});
/** A board whose first column is stored collapsed and holds two cards. */
async function setup({ keepCollapsed = false, hideAddColumn = false } = {}) {
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
...(hideAddColumn ? { "#board:hideAddColumn": "true" } : {}),
children: [
{ id: "card1", title: "First", "#status": "To Do" },
{ id: "card2", title: "Second", "#status": "To Do" },
{ id: "card3", title: "Third", "#status": "Done" }
]
});
const host = new Component();
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [
{ value: "To Do", collapsed: true, ...(keepCollapsed
? { keepCollapsed: true }
: {}) },
{ value: "Done" }
] }}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
return { mountPoint };
}
const columnAt = (container: HTMLElement, index: number) =>
[ ...container.querySelectorAll(".board-column") ][index] as HTMLElement;
const isCollapsed = (container: HTMLElement, index: number) =>
columnAt(container, index).classList.contains("collapsed");
// How many cards the column draws. A strip holds its own in the page without drawing them,
// which is what the reader sees and what the keyboard and a drag both go by.
const cardCount = (container: HTMLElement, index: number) =>
isCollapsed(container, index)
? 0
: columnAt(container, index).querySelectorAll(".board-note").length;
/** Selects a column the way a click on it does, press and release included. */
async function select(container: HTMLElement, index: number) {
const column = columnAt(container, index);
await act(async () => {
column.dispatchEvent(new Event("pointerdown", { bubbles: true }));
document.dispatchEvent(new Event("pointerup", { bubbles: true }));
column.dispatchEvent(new Event("click", { bubbles: true }));
});
}
/** Takes hold of the header and drags it, which is how a column is moved. */
async function dragHeader(container: HTMLElement, index: number) {
const header = columnAt(container, index).querySelector("h3");
await act(async () => {
header?.dispatchEvent(new Event("pointerdown", { bubbles: true }));
header?.dispatchEvent(new Event("focusin", { bubbles: true }));
header?.dispatchEvent(new Event("dragstart", { bubbles: true }));
});
}
it("draws a stored collapsed column as a strip without its cards", async () => {
const { mountPoint } = await setup();
expect(isCollapsed(mountPoint, 0)).toBe(true);
expect(cardCount(mountPoint, 0)).toBe(0);
// The count is what the strip reports in place of the cards.
expect(columnAt(mountPoint, 0).querySelector(".counter-badge")?.textContent).toBe("2");
// Never drawn at all, so a reader who keeps a long column closed pays nothing for it.
expect(columnAt(mountPoint, 0).querySelector(".board-column-content")).toBeNull();
// Every other column is untouched.
expect(isCollapsed(mountPoint, 1)).toBe(false);
expect(cardCount(mountPoint, 1)).toBe(1);
});
it("opens the column when it is selected and closes it when another one is", async () => {
const { mountPoint } = await setup({ keepCollapsed: true });
await select(mountPoint, 0);
expect(isCollapsed(mountPoint, 0)).toBe(false);
expect(cardCount(mountPoint, 0)).toBe(2);
await select(mountPoint, 1);
expect(isCollapsed(mountPoint, 0)).toBe(true);
expect(cardCount(mountPoint, 0)).toBe(0);
// Held in the page once they have been drawn once, the stylesheet keeping them off the
// screen: taking them out again is what makes a column of thousands close in three frames.
expect(columnAt(mountPoint, 0)
.querySelectorAll(":scope > .board-column-content > .board-note")).toHaveLength(2);
});
/**
* The strip is what the reader takes hold of to move the column, so opening it under the
* pointer would both hide what is being dragged and drag the opened column instead.
*/
it("stays closed while its header is dragged", async () => {
const { mountPoint } = await setup();
await dragHeader(mountPoint, 0);
expect(isCollapsed(mountPoint, 0)).toBe(true);
// The drag ends without a `pointerup`, and the column still opens on the next click.
await act(async () => {
columnAt(mountPoint, 0).querySelector("h3")
?.dispatchEvent(new Event("dragend", { bubbles: true }));
});
await select(mountPoint, 0);
expect(isCollapsed(mountPoint, 0)).toBe(false);
});
/**
* A collapsed column draws none of its cards, so nothing but the header is left to announce.
* It says it opens something and what state that is in, rather than reading as a heading with
* no way past it.
*/
it("announces the strip as a control that opens the column", async () => {
const { mountPoint } = await setup();
const header = () => columnAt(mountPoint, 0).querySelector("h3");
expect(header()?.getAttribute("role")).toBe("button");
expect(header()?.getAttribute("aria-expanded")).toBe("false");
// The key is announced in both states.
expect(header()?.getAttribute("aria-keyshortcuts")).toBe("Space");
// Open, it is a heading again: Space is a board shortcut there, not a button press.
await select(mountPoint, 0);
expect(header()?.getAttribute("role")).toBeNull();
expect(header()?.getAttribute("aria-expanded")).toBeNull();
expect(header()?.getAttribute("aria-keyshortcuts")).toBe("Space");
// A column that was never collapsed says nothing either way.
expect(columnAt(mountPoint, 1).querySelector("h3")?.getAttribute("role")).toBeNull();
});
/** The header is walked onto without opening the column, which Space is for. */
it("leaves the column closed when focus reaches its header", async () => {
const { mountPoint } = await setup();
await act(async () => {
columnAt(mountPoint, 0).querySelector("h3")
?.dispatchEvent(new Event("focusin", { bubbles: true }));
});
expect(isCollapsed(mountPoint, 0)).toBe(true);
});
it("closes an open column when focus reaches another one", async () => {
const { mountPoint } = await setup({ keepCollapsed: true });
await select(mountPoint, 0);
expect(isCollapsed(mountPoint, 0)).toBe(false);
await act(async () => {
columnAt(mountPoint, 1).querySelector("h3")
?.dispatchEvent(new Event("focusin", { bubbles: true }));
});
expect(isCollapsed(mountPoint, 0)).toBe(true);
});
/**
* The limit dialog, the icon picker and the context menu all render outside the column, so
* focus leaving it is not a reason to close it. Only another column being selected is.
*/
it("stays open while a control rendered outside it is used", async () => {
const { mountPoint } = await setup({ keepCollapsed: true });
await select(mountPoint, 0);
expect(isCollapsed(mountPoint, 0)).toBe(false);
// What opening a portalled dialog does to the column: focus goes somewhere else entirely.
const elsewhere = document.createElement("button");
document.body.appendChild(elsewhere);
await act(async () => {
columnAt(mountPoint, 0)
.dispatchEvent(new Event("focusout", { bubbles: true }));
elsewhere.dispatchEvent(new Event("pointerdown", { bubbles: true }));
});
elsewhere.remove();
expect(isCollapsed(mountPoint, 0)).toBe(false);
});
/**
* A column collapsed by hand is opened by hand, the way it is in most boards: the stored flag
* goes with the open, so the column does not shut again the moment another one is selected.
*/
it("opens a collapsed column for good, clearing the stored flag", async () => {
const { mountPoint } = await setup();
await select(mountPoint, 0);
expect(isCollapsed(mountPoint, 0)).toBe(false);
expect(saved.at(-1)?.columns?.[0]).toEqual({ value: "To Do" });
// Selecting another one no longer takes it back.
await select(mountPoint, 1);
expect(isCollapsed(mountPoint, 0)).toBe(false);
});
/**
* The cards are laid out again on every frame of the widening, so they are held unpainted
* until it is over. A column opened to take a dragged card shows them at once.
*/
it("holds the cards unpainted while the column widens", async () => {
const { mountPoint } = await setup();
await select(mountPoint, 0);
const column = columnAt(mountPoint, 0);
expect(column.classList.contains("expanding")).toBe(true);
// Drawn all the same, so the board can hand focus to one of them.
expect(cardCount(mountPoint, 0)).toBe(2);
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 350));
});
expect(columnAt(mountPoint, 0).classList.contains("expanding")).toBe(false);
});
/** The reverse of the collapse, for the open the reader asked for. */
it("marks an open the reader asked for", async () => {
const { mountPoint } = await setup();
expect(columnAt(mountPoint, 0).classList.contains("quick-expand")).toBe(false);
await select(mountPoint, 0);
expect(columnAt(mountPoint, 0).classList.contains("quick-expand")).toBe(true);
});
/**
* A peek closes behind the pointer, with the columns beside it shifting under it, so it eases
* shut; a collapse the reader asked for surprises nobody and runs faster.
*/
it("marks a collapse the reader asked for, and leaves a peek closing unmarked", async () => {
const { mountPoint } = await setup({ keepCollapsed: true });
await select(mountPoint, 0);
await act(async () => {
columnAt(mountPoint, 0).querySelector("h3")
?.dispatchEvent(new MouseEvent("dblclick", { bubbles: true, detail: 2 }));
await flush();
});
expect(isCollapsed(mountPoint, 0)).toBe(true);
expect(columnAt(mountPoint, 0).classList.contains("quick-collapse")).toBe(true);
// Opened again, what closes it is the peek, which is not the reader asking for it.
await select(mountPoint, 0);
expect(columnAt(mountPoint, 0).classList.contains("quick-collapse")).toBe(false);
await select(mountPoint, 1);
expect(isCollapsed(mountPoint, 0)).toBe(true);
expect(columnAt(mountPoint, 0).classList.contains("quick-collapse")).toBe(false);
});
/**
* The reader is looking at the open column when the entry is unchecked, so the column stays
* open: shutting it the moment another one is selected would read as the entry doing nothing.
*/
it("keeps a peeked column open once it is no longer kept collapsed", async () => {
const { mountPoint } = await setup({ keepCollapsed: true });
await select(mountPoint, 0);
expect(isCollapsed(mountPoint, 0)).toBe(false);
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
columnAt(mountPoint, 0).querySelector("h3")
?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
const entry = (show.mock.calls.at(-1)?.[0].items ?? []).find(item =>
item && "uiIcon" in item && item.uiIcon === "bx bx-lock-alt");
if (!entry || !("handler" in entry)) throw new Error("expected a keep-collapsed entry");
await act(async () => {
entry.handler?.(entry, {} as never);
await flush();
});
show.mockRestore();
expect(saved.at(-1)?.columns?.[0]).toEqual({ value: "To Do" });
await select(mountPoint, 1);
expect(isCollapsed(mountPoint, 0)).toBe(false);
});
/**
* The board's own menu, for a press on the ground the columns stand on. Expanding shows every
* column at once; a column that is kept collapsed is only peeked, and closes as the reader
* settles on one column, which is what a single peek does.
*/
it("collapses and expands every column from the board's menu", async () => {
const { mountPoint } = await setup({ keepCollapsed: true });
const board = mountPoint.querySelector<HTMLElement>(".board-view-container");
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
// With a column open, which the collapse has to close along with the rest.
await select(mountPoint, 1);
board?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
const entries = (show.mock.calls.at(-1)?.[0].items ?? []).flatMap(item =>
item && "uiIcon" in item && "handler" in item
? [ { icon: item.uiIcon, handler: item.handler } ]
: []);
expect(entries.map(entry => entry.icon)).toEqual([
"bx bx-columns", "bx bx-collapse-alt", "bx bx-expand-alt",
"bx bx-archive", "bx bx-cog"
]);
const entry = (icon: string) => entries.find(item => item.icon === icon)?.handler;
await act(async () => {
entry("bx bx-collapse-alt")?.({} as never, {} as never);
await flush();
});
expect(isCollapsed(mountPoint, 0)).toBe(true);
expect(isCollapsed(mountPoint, 1)).toBe(true);
expect(saved.at(-1)?.columns?.map(column => column.collapsed)).toEqual([ true, true ]);
await act(async () => {
entry("bx bx-expand-alt")?.({} as never, {} as never);
await flush();
});
expect(isCollapsed(mountPoint, 0)).toBe(false);
expect(isCollapsed(mountPoint, 1)).toBe(false);
// The one kept collapsed keeps the flag, and shuts again as soon as a column is selected.
expect(saved.at(-1)?.columns?.map(column => column.collapsed))
.toEqual([ true, undefined ]);
await select(mountPoint, 1);
expect(isCollapsed(mountPoint, 0)).toBe(true);
expect(isCollapsed(mountPoint, 1)).toBe(false);
show.mockRestore();
});
/**
* The overlay group holds the same two actions as the board menu, followed by the
* shortcut-hints button.
*/
it("collapses and expands every column from the overlay group", async () => {
const { mountPoint } = await setup({ keepCollapsed: true });
const group = mountPoint.querySelector<HTMLElement>(".board-overlay-controls");
const buttons = [ ...(group?.querySelectorAll<HTMLButtonElement>("button") ?? []) ];
expect(buttons.map(button => button.getAttribute("aria-label"))).toEqual([
"board_view.collapse-all-columns", "board_view.expand-all-columns",
"shortcut_hints.show_button"
]);
await act(async () => {
buttons[0]?.click();
await flush();
});
expect(isCollapsed(mountPoint, 0)).toBe(true);
expect(isCollapsed(mountPoint, 1)).toBe(true);
await act(async () => {
buttons[1]?.click();
await flush();
});
expect(isCollapsed(mountPoint, 0)).toBe(false);
expect(isCollapsed(mountPoint, 1)).toBe(false);
});
/**
* A press on a card is focus arriving before it is a click, and while the whole board is peeked
* every column reads as inactive. The column pressed in keeps its peek; the rest give theirs up.
*/
it("settles a board-wide peek on the column pressed in", async () => {
const { mountPoint } = await setup({ keepCollapsed: true });
const board = mountPoint.querySelector<HTMLElement>(".board-view-container");
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
board?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
const expand = (show.mock.calls.at(-1)?.[0].items ?? []).find(item =>
item && "uiIcon" in item && item.uiIcon === "bx bx-expand-alt");
if (!expand || !("handler" in expand)) throw new Error("expected an expand entry");
await act(async () => {
expand.handler?.(expand, {} as never);
await flush();
});
expect(isCollapsed(mountPoint, 0)).toBe(false);
// Focus landing on one of its cards, which is what a press on a card does first.
await act(async () => {
columnAt(mountPoint, 0).querySelector(".board-note")
?.dispatchEvent(new Event("focusin", { bubbles: true }));
await flush();
});
expect(isCollapsed(mountPoint, 0)).toBe(false);
// And the peek is this column's alone: focus reaching another one closes it as usual.
await act(async () => {
columnAt(mountPoint, 1).querySelector("h3")
?.dispatchEvent(new Event("focusin", { bubbles: true }));
await flush();
});
expect(isCollapsed(mountPoint, 0)).toBe(true);
show.mockRestore();
});
/**
* The board's own menu offers what the board is as well as what it does: an editor for a new
* column, and the two settings that decide which columns and cards it draws at all.
*/
it("opens the column editor and toggles what the board shows, from its menu", async () => {
const { mountPoint } = await setup();
const board = mountPoint.querySelector<HTMLElement>(".board-view-container");
const archived = vi.spyOn(BoardApi.prototype, "setArchivedShown")
.mockResolvedValue(undefined);
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
board?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
const items = show.mock.calls.at(-1)?.[0].items ?? [];
const entry = (icon: string) => {
const found = items.find(item => item && "uiIcon" in item && item.uiIcon === icon);
if (!found || !("handler" in found)) throw new Error(`expected a ${icon} entry`);
return found;
};
// Archived notes are not shown, so the entry is not ticked, and pressing it asks for them.
// The inbox column is turned on in the properties dialog rather than here.
expect("checked" in entry("bx bx-archive") && entry("bx bx-archive").checked)
.toBe(false);
entry("bx bx-archive").handler?.(entry("bx bx-archive"), {} as never);
expect(archived).toHaveBeenCalledWith(true);
// The same editor the slot at the end of the board opens.
expect(mountPoint.querySelector(".board-add-column input")).toBeNull();
await act(async () => {
entry("bx bx-columns").handler?.(entry("bx bx-columns"), {} as never);
await flush();
});
expect(mountPoint.querySelector(".board-add-column input")).toBeTruthy();
show.mockRestore();
archived.mockRestore();
});
it("leaves out the add-column button when hidden, and still opens its editor from the menu",
async () => {
const { mountPoint } = await setup({ hideAddColumn: true });
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
expect(mountPoint.querySelector(".board-view-container")).not.toBeNull();
expect(mountPoint.querySelector(".board-add-column")).toBeNull();
mountPoint.querySelector(".board-view-container")
?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
const items = show.mock.calls.at(-1)?.[0].items ?? [];
const addColumn = items.find(item =>
item && "uiIcon" in item && item.uiIcon === "bx bx-columns");
if (!addColumn || !("handler" in addColumn)) {
throw new Error("expected an add-column entry");
}
await act(async () => {
addColumn.handler?.(addColumn, {} as never);
await flush();
});
const editor = mountPoint.querySelector<HTMLInputElement>(".board-add-column input");
if (!editor) throw new Error("expected the add-column editor to be open");
await act(async () => {
editor.dispatchEvent(new FocusEvent("blur"));
editor.dispatchEvent(new FocusEvent("focusout", { bubbles: true }));
await flush();
});
expect(mountPoint.querySelector(".board-add-column")).toBeNull();
show.mockRestore();
});
/** A column and a card answer for their own presses, and the board does not speak over them. */
it("leaves a press inside a column to the column", async () => {
const { mountPoint } = await setup();
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
columnAt(mountPoint, 1).querySelector(".board-column-content")
?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
expect(show).not.toHaveBeenCalled();
show.mockRestore();
});
/** The first of the two clicks opens the strip; collapsing it again would undo that. */
it("leaves a strip open when it is double clicked open", async () => {
const { mountPoint } = await setup();
const header = columnAt(mountPoint, 0).querySelector("h3");
await act(async () => {
header?.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, detail: 1 }));
header?.dispatchEvent(new MouseEvent("click", { bubbles: true, detail: 1 }));
await flush();
});
await act(async () => {
header?.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, detail: 2 }));
header?.dispatchEvent(new MouseEvent("dblclick", { bubbles: true, detail: 2 }));
await flush();
});
expect(isCollapsed(mountPoint, 0)).toBe(false);
});
it("keeps the flag through an open where the column is kept collapsed", async () => {
const { mountPoint } = await setup({ keepCollapsed: true });
await select(mountPoint, 0);
expect(isCollapsed(mountPoint, 0)).toBe(false);
expect(saved).toEqual([]);
});
/**
* The menu is opened from the column, so that column is the open one. Storing the flag alone
* would change nothing on screen and leave the reader with no sign the entry did anything.
*/
it("collapses the column when Space is pressed on its heading", async () => {
const { mountPoint } = await setup();
const header = columnAt(mountPoint, 1).querySelector<HTMLElement>("h3");
expect(header).not.toBeNull();
header?.focus();
await act(async () => {
header?.dispatchEvent(new KeyboardEvent("keydown", { key: " ", bubbles: true }));
await flush();
});
expect(isCollapsed(mountPoint, 1)).toBe(true);
expect(saved.at(-1)?.columns?.[1]).toEqual({ value: "Done", collapsed: true });
// The strip's header keeps the focus, so Space opens the column again.
const strip = columnAt(mountPoint, 1).querySelector("h3");
expect(document.activeElement).toBe(strip);
expect(strip?.getAttribute("aria-expanded")).toBe("false");
});
it("closes the column as soon as the menu entry collapses it", async () => {
const { mountPoint } = await setup();
const column = columnAt(mountPoint, 1);
expect(isCollapsed(mountPoint, 1)).toBe(false);
await select(mountPoint, 1);
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
column.querySelector("h3")
?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
const entry = (show.mock.calls.at(-1)?.[0].items ?? []).find(item =>
item && "uiIcon" in item && item.uiIcon === "bx bx-collapse-horizontal");
if (!entry || !("handler" in entry)) throw new Error("expected a collapse entry");
await act(async () => {
entry.handler?.(entry, {} as never);
await flush();
});
show.mockRestore();
expect(isCollapsed(mountPoint, 1)).toBe(true);
expect(saved.at(-1)?.columns?.[1]).toEqual({ value: "Done", collapsed: true });
});
/**
* The board is not drawn afresh for another note, so the column opened on one is still named
* in its state when the next arrives. A board of its own storing a column under that name
* would be drawn open, against what it stores.
*/
it("does not carry an open column over to another board", async () => {
const { mountPoint } = await setup();
await select(mountPoint, 0);
expect(isCollapsed(mountPoint, 0)).toBe(false);
const other = buildNote({
title: "Other board",
"#collection": "",
"#viewType": "board",
children: [ { title: "Fourth", "#status": "To Do" } ]
});
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={other}
noteIds={[ ...other.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do", collapsed: true } ] }}
/>
</ParentComponent.Provider>,
mountPoint
);
await flush();
});
await act(async () => { await flush(); });
expect(isCollapsed(mountPoint, 0)).toBe(true);
expect(cardCount(mountPoint, 0)).toBe(0);
});
it("offers no icon picker or title editor while collapsed", async () => {
const { mountPoint } = await setup();
const collapsed = columnAt(mountPoint, 0);
expect(collapsed.querySelector(".column-icon button")).toBeNull();
startEditingTitle(collapsed);
await act(async () => { await flush(); });
expect(collapsed.querySelector("input")).toBeNull();
// Both come back with the column.
await select(mountPoint, 0);
expect(columnAt(mountPoint, 0).querySelector(".column-icon button")).not.toBeNull();
});
});
describe("A board in a tab the reader is not looking at", () => {
let container: HTMLElement | undefined;
afterEach(() => {
saved.length = 0;
if (container) {
render(null, container);
container.remove();
container = undefined;
}
});
/**
* Every mounted board hears every change, so a card renamed once would redraw the board in each
* tab it is open in. A board nobody is looking at remembers the change instead.
*/
it("does not redraw for a change while its tab is in the background", async () => {
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
children: [
{ id: "one", title: "First", "#status": "To Do" },
{ id: "two", title: "Second", "#status": "Done" }
]
});
// The context the board belongs to, reached the way `useNoteContext` reaches it: from the
// nearest ancestor carrying one. The board compares main contexts, so the tab is what the
// stub stands for and a split pane of it would answer with the same one.
const tab = {};
const otherTab = {};
let shown: object = tab;
const host = new Component();
Object.assign(host, { noteContext: contextStub({ getMainContext: () => tab }) });
const previousTabManager = appContext.tabManager;
appContext.tabManager = { getActiveMainContext: () => shown } as never;
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
const draw = async () => {
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" }, { value: "Done" } ] }}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
};
const columnOf = (title: string) => [ ...mountPoint.querySelectorAll(".board-column") ]
.find(column => [ ...column.querySelectorAll(".board-note") ]
.some(card => card.textContent?.includes(title)))
?.getAttribute("data-column");
await draw();
expect(columnOf("First")).toBe("To Do");
// The card moves column while another tab is the one being looked at.
shown = otherTab;
for (const attribute of froca.getNoteFromCache("one")?.getAttributes() ?? []) {
if (attribute.name === "status") attribute.value = "Done";
}
await draw();
expect(columnOf("First")).toBe("To Do");
// Looked at again, it catches up with what it missed.
shown = tab;
await draw();
expect(columnOf("First")).toBe("Done");
appContext.tabManager = previousTabManager;
});
/**
* `getActiveMainContext` names one pane across the whole app, so a board sharing a tab with the
* focused pane is on screen and redraws: a card dropped onto it from another split appears at
* once rather than waiting for something to force a refresh.
*/
it("redraws for a change while it is the split the reader is not focused on", async () => {
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
children: [
{ id: "split1", title: "First", "#status": "To Do" },
{ id: "split2", title: "Second", "#status": "Done" }
]
});
// One tab, and the board is in a pane of it that does not hold the focus.
const tab = {};
const host = new Component();
Object.assign(host, { noteContext: contextStub({ getMainContext: () => tab }) });
const previousTabManager = appContext.tabManager;
appContext.tabManager = { getActiveMainContext: () => tab } as never;
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
const draw = async () => {
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" }, { value: "Done" } ] }}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
};
const columnOf = (title: string) => [ ...mountPoint.querySelectorAll(".board-column") ]
.find(column => [ ...column.querySelectorAll(".board-note") ]
.some(card => card.textContent?.includes(title)))
?.getAttribute("data-column");
await draw();
expect(columnOf("First")).toBe("To Do");
for (const attribute of froca.getNoteFromCache("split1")?.getAttributes() ?? []) {
if (attribute.name === "status") attribute.value = "Done";
}
await draw();
expect(columnOf("First")).toBe("Done");
appContext.tabManager = previousTabManager;
});
});
describe("Board column creation", () => {
let container: HTMLElement | undefined;
afterEach(() => {
saved.length = 0;
if (container) {
render(null, container);
container.remove();
container = undefined;
}
});
async function setup() {
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
children: [
{ id: "card1", title: "First", "#status": "To Do" },
{ id: "card2", title: "Second", "#status": "Done" }
]
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness note={note} noteIds={[ "card1", "card2" ]} initialConfig={{ columns: [ { value: "To Do" }, { value: "Done" } ] }} />
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => {
await flush();
});
return { note, container: mountPoint };
}
/** Clicks "Add column", types a name and commits it with Enter, as the user would. */
async function addColumn(container: HTMLElement, name: string) {
await act(async () => {
container.querySelector<HTMLElement>(".board-add-column")?.click();
await flush();
});
const input = container.querySelector<HTMLInputElement>(".board-add-column input");
if (!input) throw new Error("expected an inline editor for the new column");
await act(async () => {
input.focus();
input.value = name;
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
// The save re-publishes the config, which re-runs the (async) board refresh; that lands in a
// later tick than the one `act` flushed above.
await act(async () => {
await flush();
});
}
/**
* A column is added at the right end of the board, past its edge on a board of any width, and
* the editor waiting for the next one goes with it.
*/
it("scrolls the board to its end as a column is added", async () => {
const { container } = await setup();
const board = container.querySelector<HTMLElement>(".board-view-container");
const slot = container.querySelector<HTMLElement>(".board-add-column");
if (!board || !slot) throw new Error("expected a board with an add-column button");
// happy-dom lays nothing out, so the width the board scrolls within is stood in for.
const widen = (width: number) => Object.defineProperty(board, "scrollWidth", {
value: width, configurable: true, writable: true
});
widen(1200);
await act(async () => {
slot.click();
await flush();
});
const input = slot.querySelector<HTMLInputElement>("input");
if (!input) throw new Error("expected an inline editor for the new column");
// The board grows by the column the write adds, and the scroll follows that rather than
// the write, which returns before the column is drawn.
board.scrollLeft = 0;
widen(1450);
await act(async () => {
input.value = "In Progress";
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
await act(async () => { await flush(); });
expect(columnTitles(container)).toEqual([ "To Do", "Done", "In Progress" ]);
expect(board.scrollLeft).toBe(1450);
// And it fades in where it landed, once: a redraw of the board must not play it again.
const added = container.querySelectorAll<HTMLElement>(".board-column")[2];
expect(added.classList.contains("appearing")).toBe(true);
expect(container.querySelectorAll(".board-column.appearing")).toHaveLength(1);
await act(async () => {
added.dispatchEvent(new AnimationEvent("animationend", {
animationName: "board-item-appear", bubbles: true
}));
await flush();
});
expect(added.classList.contains("appearing")).toBe(false);
});
it("renders a newly added column without leaving the view", async () => {
const { container } = await setup();
expect(columnTitles(container)).toEqual([ "To Do", "Done" ]);
await addColumn(container, "In Progress");
expect(saved.at(-1)?.columns?.map(c => c.value)).toEqual([ "To Do", "Done", "In Progress" ]);
expect(columnTitles(container)).toEqual([ "To Do", "Done", "In Progress" ]);
});
it("sets every collapsed title upright once a column is named in CJK", async () => {
const { container } = await setup();
const board = container.querySelector<HTMLElement>(".board-view-container");
if (!board) throw new Error("expected the board container");
expect(board.classList.contains("cjk-titles")).toBe(false);
await addColumn(container, "English 中文 あい");
expect(columnTitles(container)).toContain("English 中文 あい");
expect(board.classList.contains("cjk-titles")).toBe(true);
});
});
describe("Board column rename", () => {
let container: HTMLElement | undefined;
afterEach(() => {
saved.length = 0;
if (container) {
render(null, container);
container.remove();
container = undefined;
}
});
const DEFAULT_CONFIG: BoardViewData = {
columns: [ { value: "To Do" }, { value: "Doing" }, { value: "Done" } ]
};
async function setup(config: BoardViewData = DEFAULT_CONFIG, includeArchived = false) {
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
...(includeArchived ? { "#includeArchived": "true" } : {}),
"#label:status(inheritable)":
"promoted,alias=Status,single,select,options=To Do;Doing;Done",
children: [
{ title: "First", "#status": "To Do" },
{ title: "Second", "#status": "Doing" },
{ title: "Third", "#status": "Done" }
]
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
const host = new Component();
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={config}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
// The host is what the board's own event handlers hang off, for a test that sends one.
return { note, container: mountPoint, host };
}
/**
* What a second tab on the same board sees while the first is writing. The record is shared, so
* the column being carried is resolved away here too; putting that on disk would commit an
* answer the notes have not given, and the tab making the write persists it once it lands.
*/
it("leaves the stored columns alone while a write on the board is running", async () => {
const { note } = await setup();
saved.length = 0;
const writes = getPendingWrites(`${note.noteId}|status`);
writes.renames.set("Done", undefined);
writes.inFlight = 1;
const second = document.createElement("div");
document.body.appendChild(second);
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={DEFAULT_CONFIG}
/>
</ParentComponent.Provider>,
second
);
});
await act(async () => { await flush(); });
expect(saved).toEqual([]);
second.remove();
});
/**
* A record outlasts the write that made it wherever the definition keeps naming the old value,
* which a board that cannot write its own definition does for good. The board still has to
* bring what it can reach into line, so only a running write holds persistence back.
*/
/**
* A board that keeps a column list is left to keep it: written on every refresh instead, a
* client reading the board while another changes it resolves a name the change has already
* taken away and writes it back as a column of its own.
*/
it("leaves a stored column list alone, and writes one only for a board with none", async () => {
const { note } = await setup();
saved.length = 0;
const draw = async (initialConfig: BoardViewData) => {
const mountPoint = document.createElement("div");
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={initialConfig}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
mountPoint.remove();
};
await draw(DEFAULT_CONFIG);
expect(saved).toEqual([]);
await draw({});
expect(saved.at(-1)?.columns?.map(column => column.value))
.toEqual([ "To Do", "Doing", "Done" ]);
});
it("offers its keys as contextual shortcut hints", async () => {
const host = new Component();
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
children: [ { title: "First", "#status": "To Do" } ]
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={DEFAULT_CONFIG}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
const sections = collectShortcutHints(host);
expect(sections.map(section => section.titleKey)).toEqual([
"board_view.hints.navigation",
"board_view.hints.editing",
"board_view.hints.moving",
"board_view.hints.selection"
]);
// Every key the board answers for is spoken for, and none it does not.
expect(sections.flatMap(section => section.hints)).toEqual([
{ keys: [ "Up", "Down" ], labelKey: "board_view.hints.navigate_items" },
{ keys: [ "Left", "Right" ], labelKey: "board_view.hints.navigate_columns" },
{ keys: [ "Home", "End" ], labelKey: "board_view.hints.first_last_item" },
{ keys: [ "Enter", "Shift+Enter" ], labelKey: "board_view.hints.insert_item" },
{
keys: [ "Ctrl+Enter", "Ctrl+Shift+Enter" ],
labelKey: "board_view.hints.insert_column"
},
{ keys: [ "Space" ], labelKey: "board_view.hints.open_item" },
{ keys: [ "Space" ], labelKey: "board_view.hints.toggle_column" },
{ keys: [ "F2" ], labelKey: "board_view.hints.rename" },
{ keys: [ "Delete" ], labelKey: "board_view.hints.remove_item" },
{ keys: [ "Shift+Delete" ], labelKey: "board_view.hints.delete_item" },
{ keys: [ "Delete" ], labelKey: "board_view.hints.remove_column" },
{ keys: [ "Ctrl+Up", "Ctrl+Down" ], labelKey: "board_view.hints.move_item" },
{ keys: [ "Ctrl+Home", "Ctrl+End" ], labelKey: "board_view.hints.move_within" },
{ keys: [ "Ctrl+Left", "Ctrl+Right" ], labelKey: "board_view.hints.move_across" },
{
keys: [ "Ctrl+Shift+Left", "Ctrl+Shift+Right" ],
labelKey: "board_view.hints.move_to_end_column"
},
{
keys: [ "Ctrl+Alt+Left", "Ctrl+Alt+Right" ],
labelKey: "board_view.hints.move_column"
},
{
keys: [ "Ctrl+Alt+Home", "Ctrl+Alt+End" ],
labelKey: "board_view.hints.move_column_to_edge"
},
{ keys: [ "Ctrl+Space" ], labelKey: "board_view.hints.toggle_selection" },
{
keys: [ "Shift+Down", "Shift+Up" ],
labelKey: "board_view.hints.extend_selection"
},
{ keys: [ "Ctrl+A" ], labelKey: "board_view.hints.select_column" },
{ keys: [ "Escape" ], labelKey: "board_view.hints.clear_selection" }
]);
});
/**
* A view is handed its record when it first draws the board and holds it while it is mounted.
* A record dropped for being empty would leave this view reading one nothing writes into, and
* a column another tab is deleting would come back.
*/
it("keeps a drawn view on the record later writes go into", async () => {
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
"#label:status(inheritable)":
"promoted,alias=Status,single,select,options=To Do;Doing;Done",
children: [ { title: "First", "#status": "To Do" } ]
});
const host = new Component();
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
// A fresh list each time, so the refresh the board hangs off runs again.
const draw = async () => {
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={DEFAULT_CONFIG}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
};
await draw();
expect(columnTitles(mountPoint)).toEqual([ "To Do", "Doing", "Done" ]);
// What another tab deleting a column leaves behind, after this view has been drawing.
getPendingWrites(`${note.noteId}|status`).renames.set("Done", undefined);
await draw();
expect(columnTitles(mountPoint)).toEqual([ "To Do", "Doing" ]);
});
it("collapses the column when its header is double clicked", async () => {
const { container } = await setup();
const [ first ] = [ ...container.querySelectorAll<HTMLElement>(".board-column") ];
await act(async () => {
first.querySelector("h3")
?.dispatchEvent(new MouseEvent("dblclick", { bubbles: true }));
await flush();
});
expect(first.classList.contains("collapsed")).toBe(true);
expect(saved.at(-1)?.columns?.[0]).toEqual({ value: "To Do", collapsed: true });
// The title editor is left to F2 and to the menu.
expect(first.querySelector("h3 input")).toBeNull();
});
it("hints at the double click and Space on an open column's heading", async () => {
const { container } = await setup();
const heading = container.querySelector(".board-column h3");
expect(heading).not.toBeNull();
// `useStaticTooltip` installs one only where there is a title to show.
expect(heading && Tooltip.getInstance(heading)).not.toBeNull();
});
/** The tooltip is set on the element, which is where `useStaticTooltip` reads it back from. */
function badgeTooltip(container: HTMLElement, index: number) {
const badge = [ ...container.querySelectorAll(".board-column .counter-badge") ][index];
// Bootstrap moves the attribute aside once it takes the tooltip over.
return badge?.getAttribute("title") ?? badge?.getAttribute("data-bs-original-title");
}
it("counts the cards a column holds, and says so when hovered", async () => {
const { container } = await setup();
expect([ ...container.querySelectorAll(".board-column .counter-badge") ]
.map(el => el.textContent)).toEqual([ "1", "1", "1" ]);
expect(badgeTooltip(container, 0)).toBe('board_view.card-count:{"count":1}');
});
/**
* Archived cards are among a column's only while the board is showing them; where it is not,
* there are none to count and the badge says how many cards there are and no more.
*/
it("tells the archived cards apart from the rest where the board shows them", async () => {
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
"#includeArchived": "true",
"#label:status(inheritable)":
"promoted,alias=Status,single,select,options=To Do",
children: [
{ title: "First", "#status": "To Do" },
{ title: "Second", "#status": "To Do" },
{ title: "Old", "#status": "To Do", "#archived": "" }
]
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" } ] }}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
expect(badgeTooltip(mountPoint, 0))
.toBe('board_view.card-count-with-archived:{"count":2,"archived":1}');
});
/** A column told how much it should hold says so, and says when it is holding more. */
it("shows the limit beside the count, and marks a column over it", async () => {
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
"#label:status(inheritable)":
"promoted,alias=Status,single,select,options=To Do;Done",
children: [
{ title: "First", "#status": "To Do" },
{ title: "Second", "#status": "To Do" },
{ title: "Third", "#status": "Done" }
]
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [
{ value: "To Do", limit: 1 }, { value: "Done", limit: 4 }
] }}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
const badges = [ ...mountPoint.querySelectorAll(".board-column .counter-badge") ];
expect(badges.map(el => el.textContent)).toEqual([ "2/1", "1/4" ]);
// Only the one holding more than it should is marked, badge and body alike.
expect(badges.map(el => el.classList.contains("over-limit"))).toEqual([ true, false ]);
expect([ ...mountPoint.querySelectorAll(".board-column") ]
.map(el => el.classList.contains("over-limit"))).toEqual([ true, false ]);
// The one over its limit says so on hover, on a line of its own.
expect(badgeTooltip(mountPoint, 0)).toContain("board_view.card-count-over-limit");
expect(badgeTooltip(mountPoint, 1)).not.toContain("board_view.card-count-over-limit");
});
/**
* The badge is what the reader sees; the tooltip follows it through the memoised config. The
* attribute is only the fallback before Bootstrap takes the tooltip over, and Bootstrap
* restores its own copy of it on dispose, so it is not what this asserts.
*/
it("follows the count when a card leaves the column", async () => {
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
"#label:status(inheritable)":
"promoted,alias=Status,single,select,options=To Do;Done",
children: [
{ title: "First", "#status": "To Do" },
{ title: "Second", "#status": "To Do" }
]
});
const host = new Component();
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
const draw = async () => {
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" }, { value: "Done" } ] }}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
};
const counts = () =>
[ ...mountPoint.querySelectorAll(".counter-badge") ].map(el => el.textContent);
await draw();
expect(counts()).toEqual([ "2", "0" ]);
// What a move leaves behind: the card now carries the other column's value.
const [ moved ] = [ ...note.getChildNoteIds() ];
for (const attribute of froca.getNoteFromCache(moved)?.getAttributes() ?? []) {
if (attribute.name === "status") attribute.value = "Done";
}
await draw();
expect(counts()).toEqual([ "1", "1" ]);
});
/** Renames the middle column, so a slot that is not the last one has to survive. */
async function renameSecondColumn(container: HTMLElement, newName: string) {
return renameColumnAt(container, 1, newName);
}
async function renameColumnAt(container: HTMLElement, index: number, newName: string) {
const column = container.querySelectorAll<HTMLElement>(".board-column")[index];
await act(async () => {
startEditingTitle(column);
await flush();
});
const input = column.querySelector<HTMLInputElement>("h3 input");
if (!input) throw new Error("expected an inline editor for the column title");
await act(async () => {
input.focus();
input.value = newName;
input.blur();
await flush();
});
await act(async () => { await flush(); });
}
/**
* Two columns under one name would be merged by the rename, which a reader who has forgotten
* the other column does not expect. The name is refused, and the editor stays open on it.
*/
it("refuses a name another column already has and keeps the editor open", async () => {
const { container } = await setup();
const put = vi.spyOn(server, "put").mockResolvedValue(undefined);
const message = vi.spyOn(toast, "showMessage").mockReturnValue(undefined);
await renameColumnAt(container, 1, "Done");
expect(message).toHaveBeenCalledWith(
"board_view.column-name-taken:{\"column\":\"Done\"}", undefined, "bx bx-duplicate");
expect(put).not.toHaveBeenCalled();
const columns = container.querySelectorAll<HTMLElement>(".board-column");
expect(columns).toHaveLength(3);
// The column being renamed shows the editor in place of its title.
expect(columnTitles(container)).toEqual([ "To Do", "Done" ]);
expect(columns[1].querySelector<HTMLInputElement>("h3 input")?.value).toBe("Done");
});
/**
* The cards, the stored columns and the definition are renamed together by the server, so that
* no client reads the board while they disagree and writes the old name back. What the board
* does from here is ask for that, and write none of the three itself.
*/
it("asks the server to rename the column, and writes nothing of its own", async () => {
const { container } = await setup();
const put = vi.spyOn(server, "put").mockResolvedValue(undefined);
expect(columnTitles(container)).toEqual([ "To Do", "Doing", "Done" ]);
await renameSecondColumn(container, "In Progress");
expect(put).toHaveBeenCalledWith(expect.stringMatching(/board\/rename-column$/),
expect.objectContaining({
attribute: "status", oldValue: "Doing", newValue: "In Progress"
}));
expect(saved).toEqual([]);
});
/**
* `NoteList` renders the view unkeyed, so the board the user moves to is the same component
* instance. A rename still pending on the one they left must not rewrite a column here that
* happens to carry the old name.
*/
it("does not carry a pending rename over to the next board", async () => {
const { container } = await setup();
await renameSecondColumn(container, "In Progress");
const other = buildNote({
title: "Other board",
"#collection": "",
"#viewType": "board",
"#label:status(inheritable)":
"promoted,alias=Status,single,select,options=Doing;Shipped",
children: [
{ title: "Fourth", "#status": "Doing" },
{ title: "Fifth", "#status": "Shipped" }
]
});
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={other}
noteIds={[ ...other.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "Doing" }, { value: "Shipped" } ] }}
/>
</ParentComponent.Provider>,
container
);
await flush();
});
await act(async () => { await flush(); });
expect(columnTitles(container)).toEqual([ "Doing", "Shipped" ]);
});
it("shows each column's icon, defaulting it, and keeps it while editing", async () => {
const { container } = await setup({
columns: [
{ value: "To Do" },
{ value: "Doing", icon: "bx bx-run" },
{ value: "Done" }
]
});
expect(columnIcons(container))
.toEqual([ DEFAULT_COLUMN_ICON, "bx bx-run", DEFAULT_COLUMN_ICON ]);
// The editor covers the title and its button, so the icon is still the one on the left.
const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
await act(async () => {
startEditingTitle(column);
await flush();
});
expect(column.querySelector("h3 input")).toBeTruthy();
expect(column.querySelector("h3 > .column-icon button")?.className).toContain("bx bx-run");
});
it("tints only the columns given a colour that has a hue", async () => {
const { container } = await setup({
columns: [
{ value: "To Do", color: "#4d99e6" },
{ value: "Doing", color: "#808080" },
{ value: "Done" }
]
});
const columns = [ ...container.querySelectorAll<HTMLElement>(".board-column") ];
const hues = columns
.map(column => column.style.getPropertyValue("--board-column-custom-hue"));
expect(Math.round(Number(hues[0]))).toBe(210);
// Grey has no hue of its own, leaving the column as plain as one with no colour.
expect(hues.slice(1)).toEqual([ "", "" ]);
expect(columns.map(column => column.classList.contains("with-hue")))
.toEqual([ true, false, false ]);
});
it("hides an archived column, unless the board is set to show archived notes", async () => {
const archivedConfig: BoardViewData = {
columns: [ { value: "To Do" }, { value: "Doing", archived: true }, { value: "Done" } ]
};
const { container: hidden } = await setup(archivedConfig);
expect(columnTitles(hidden)).toEqual([ "To Do", "Done" ]);
// The column is only out of sight; nothing has been written out of the config.
expect(saved.every(config => config.columns?.some(col => col.value === "Doing")))
.toBe(true);
render(null, hidden);
hidden.remove();
const { container: shown } = await setup(archivedConfig, true);
expect(columnTitles(shown)).toEqual([ "To Do", "Doing", "Done" ]);
expect([ ...shown.querySelectorAll(".board-column") ]
.map(column => column.classList.contains("board-column-archived")))
.toEqual([ false, true, false ]);
});
/**
* The editor belongs to the column rather than to the button below it, so the header's menu
* can raise the same one instead of a second way of making a card.
*/
it("opens the new-item editor from the menu, as the button below the column does", async () => {
const { container } = await setup();
const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
expect(column.querySelector(".board-new-item textarea")).toBeNull();
// Raised the way the header raises it, then the entry is invoked as the menu would.
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
column.querySelector("h3")?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
const entry = (show.mock.calls.at(-1)?.[0].items ?? [])
.find(item => item && "uiIcon" in item && item.uiIcon === "bx bx-plus");
if (!entry || !("handler" in entry)) throw new Error("expected a new-item entry");
await act(async () => {
entry.handler?.(entry, {} as never);
await flush();
});
expect(column.querySelector(".board-new-item.editing textarea")).toBeTruthy();
show.mockRestore();
});
/** The column menu's `onSelectAll` calls `selection.selectAll` for the same cards as Ctrl+A. */
it("picks out every card in the column from the menu", async () => {
const { container } = await setup();
const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
const picked = () => [ ...container.querySelectorAll(".board-note.selected") ]
.map(element => element.getAttribute("data-note-id"));
expect(picked()).toEqual([]);
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
column.querySelector("h3")?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
const entry = (show.mock.calls.at(-1)?.[0].items ?? [])
.find(item => item && "uiIcon" in item && item.uiIcon === "bx bx-selection");
if (!entry || !("handler" in entry)) throw new Error("expected a select-all entry");
await act(async () => {
entry.handler?.(entry, {} as never);
await flush();
});
expect(picked()).toEqual(
[ ...column.querySelectorAll(".board-note") ].map(e => e.getAttribute("data-note-id")));
show.mockRestore();
});
it("keeps the new-item slot out of the column's scrolling body", async () => {
const { container } = await setup();
const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
const slot = column.querySelector<HTMLElement>(".board-new-item");
expect(slot).toBeTruthy();
expect(slot?.closest(".board-column-content")).toBeNull();
expect(slot?.parentElement).toBe(column);
});
/**
* Cards are added in runs, so the editor stays where it is with an empty field rather than
* closing and having to be opened again for the next one.
*/
it("clears the new-item editor on Enter and leaves it open", async () => {
const { container } = await setup();
const created = vi.spyOn(BoardApi.prototype, "createNewItem")
.mockResolvedValue(undefined);
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
if (!editor) throw new Error("expected the new-item editor to be open");
editor.value = "First";
await act(async () => {
editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
expect(created).toHaveBeenCalledWith("Doing", "First", "bottom", undefined, textTemplate());
expect(slot?.querySelector("textarea")).toBe(editor);
expect(editor.value).toBe("");
expect(document.activeElement).toBe(editor);
// The one already saved is not written a second time by the one that follows it.
editor.value = "Second";
await act(async () => {
editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
expect(created).toHaveBeenCalledTimes(2);
expect(created).toHaveBeenLastCalledWith("Doing", "Second", "bottom", undefined, textTemplate());
});
/**
* A card is added at the end of its column, which on a full column is out of sight, so it is
* scrolled to and faded in rather than appearing wherever the reader is not looking.
*/
it("reveals the card the editor just made", async () => {
const { note, container } = await setup();
const card = container.querySelector<HTMLElement>(".board-note");
const noteId = card?.getAttribute("data-note-id");
const column = card?.closest<HTMLElement>(".board-column");
if (!card || !noteId || !column) throw new Error("expected a card to stand in for the new one");
// happy-dom lays nothing out, so the scrollable height has to be stood in for.
const content = column.querySelector<HTMLElement>(".board-column-content");
if (!content) throw new Error("expected a scrollable column body");
Object.defineProperty(content, "scrollHeight",
{ value: 500, configurable: true, writable: true });
expect(content.scrollTop).toBe(0);
vi.spyOn(BoardApi.prototype, "createNewItem").mockResolvedValue(noteId);
const slot = column.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
if (!editor) throw new Error("expected the new-item editor to be open");
editor.value = "Fresh";
await act(async () => {
editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
expect(card.classList.contains("appearing")).toBe(true);
expect(content.scrollTop).toBe(500);
// Shown once: the card stays the one just made, and a redraw of the column must not play
// the reveal again.
await act(async () => {
card.dispatchEvent(new AnimationEvent("animationend", {
animationName: "board-item-appear", bubbles: true
}));
await flush();
});
expect(card.classList.contains("appearing")).toBe(false);
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={DEFAULT_CONFIG}
/>
</ParentComponent.Provider>,
container
);
await flush();
});
expect(card.classList.contains("appearing")).toBe(false);
// The cards that were already there are left alone.
const others = [ ...container.querySelectorAll(".board-note") ]
.filter(other => other !== card);
expect(others.some(other => other.classList.contains("appearing"))).toBe(false);
});
/**
* The field a card is named in stands where the card goes and holds what it is called, so the
* card is drawn into the place the field was keeping for it: it fades in there rather than
* opening out of a gap the column makes after closing the one the field left.
*/
it("keeps the field standing until the card it made takes its place", async () => {
const { note, container } = await setup();
const made = buildNote({ title: "Made", "#status": "Doing" });
let answer: (noteId: string) => void = () => {};
vi.spyOn(BoardApi.prototype, "createNewItemBefore")
.mockReturnValue(new Promise((resolve) => { answer = resolve; }));
const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
const standIn = column.querySelector<HTMLElement>(".board-note");
if (!standIn) throw new Error("expected a card in the column");
await act(async () => {
standIn.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
const field = column
.querySelector<HTMLTextAreaElement>(".board-new-item.inserting textarea");
if (!field) throw new Error("expected the field for a new card to be open");
field.value = "Made";
field.dispatchEvent(new Event("input", { bubbles: true }));
await act(async () => {
field.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
// The write is still in flight, so the field stands, and holds what it is about to become.
await act(async () => {
answer(made.noteId);
await flush();
});
expect(column.querySelector(".board-new-item.inserting")).toBeTruthy();
expect(field.value).toBe("Made");
fileCard(note, made);
await redraw(note, container);
expect(column.querySelector(".board-new-item.inserting")).toBeNull();
// Faded in where the field stood, and not opened out of nothing.
const card = drawn(container, made.noteId);
expect(card.classList.contains("appearing")).toBe(true);
expect(card.style.height).toBe("");
});
/** A read of the templates the test itself answers, in the order it chooses. */
interface Answer {
resolve: (options: NoteTypeOption[]) => void;
reject: (error: Error) => void;
}
/** What the pill in the editor a card is named in reads, which is the board's own template. */
async function pillNames(container: HTMLElement) {
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
return slot?.querySelector(".template-pill button")?.textContent ?? "";
}
/**
* An attribute change as the server reports one: the row is tracked as well as carried.
*
* A label taken away is reported as a change like any other, the row read from the database
* rather than from becca and so carrying what the label was.
*/
function attributeChanged(name: string, noteId = "someNote", isDeleted = false) {
const results = new LoadResults([ {
entityName: "attributes",
entityId: "attr1",
entity: { attributeId: "attr1", noteId, type: "label", name, isDeleted }
} as never ]);
results.addAttribute("attr1", "other");
return results;
}
/**
* Names a card in the field a press of Enter opens below the given one, which is what makes it.
*
* Answers with the field, which is taken down as the card is made.
*/
async function nameCardBeside(card: HTMLElement, title: string) {
await act(async () => {
card.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
const field = card.parentElement
?.querySelector<HTMLTextAreaElement>(".board-new-item.inserting textarea");
if (!field) throw new Error("expected the field for a new card to be open");
field.value = title;
await act(async () => {
field.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
return field;
}
/**
* Takes a dialog down as Bootstrap would.
*
* Its own teardown waits on a transition that happy-dom never runs, so a dialog left open keeps
* the focus trap Bootstrap put on the page: every editor the tests after it open would lose the
* focus the moment it was given.
*/
async function dismiss(dialog?: HTMLElement) {
await act(async () => {
dialog?.dispatchEvent(new Event("hidden.bs.modal", { bubbles: true }));
await flush();
});
BootstrapModal.getInstance(dialog as HTMLElement)?.dispose();
dialog?.remove();
document.querySelector(".modal-backdrop")?.remove();
document.body.classList.remove("modal-open");
}
/** The element standing for a note, which a move draws afresh. */
function drawn(container: HTMLElement, noteId: string) {
const element = [ ...container.querySelectorAll<HTMLElement>(".board-note") ]
.find(card => card.getAttribute("data-note-id") === noteId);
if (!element) throw new Error(`expected ${noteId} to be drawn`);
return element;
}
/**
* A card that arrives from somewhere else, a move between columns above all, mounts as a new
* element in the column that draws it. Only a card the column itself made is shown: the rest
* are already where they were put.
*/
it("shows only the card the column made, not one that merely arrived", async () => {
const { note, container } = await setup();
withBoxes();
try {
addCard(note, "Doing");
await redraw(note, container);
const arrived = [ ...container.querySelectorAll<HTMLElement>(".board-note") ]
.find(card => card.textContent?.includes("Added"));
if (!arrived) throw new Error("expected the card that arrived to be drawn");
expect(arrived.classList.contains("appearing")).toBe(false);
expect(arrived.style.height).toBe("");
expect(arrived.style.overflow).toBe("");
} finally {
dropBoxes();
}
});
/** happy-dom lays nothing out, and a child with no offset parent is one `useFlip` skips. */
function withBoxes() {
for (const [ property, read ] of Object.entries({
offsetParent(this: HTMLElement) { return this.parentElement; },
offsetHeight() { return 34; },
offsetTop() { return 0; }
})) {
Object.defineProperty(HTMLElement.prototype, property,
{ configurable: true, get: read as () => unknown });
}
}
function dropBoxes() {
for (const property of [ "offsetParent", "offsetHeight", "offsetTop" ]) {
delete (HTMLElement.prototype as unknown as Record<string, unknown>)[property];
}
}
/** Draws the board again, which is how a card filed under it reaches the column. */
async function redraw(note: ReturnType<typeof buildNote>, container: HTMLElement) {
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={DEFAULT_CONFIG}
/>
</ParentComponent.Provider>,
container
);
await flush();
});
await act(async () => { await flush(); });
}
it("reveals a card inserted next to another one, without leaving its place", async () => {
const { note, container } = await setup();
// Two more cards in the same column, so the one standing in for the insert has a card on
// either side and the column is brought to it rather than run to either end.
addCard(note, "Doing");
addCard(note, "Doing");
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={DEFAULT_CONFIG}
/>
</ParentComponent.Provider>,
container
);
await flush();
});
await act(async () => { await flush(); });
const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
const [ first, middle, last ] = column.querySelectorAll<HTMLElement>(".board-note");
const noteId = middle?.getAttribute("data-note-id");
if (!first || !middle || !last || !noteId) {
throw new Error("expected a column of three cards");
}
const scrolled = vi.fn();
Object.defineProperty(middle, "scrollIntoView",
{ value: scrolled, configurable: true, writable: true });
const content = column.querySelector<HTMLElement>(".board-column-content");
Object.defineProperty(content, "scrollHeight",
{ value: 500, configurable: true, writable: true });
vi.spyOn(BoardApi.prototype, "createNewItemBefore").mockResolvedValue(noteId);
await nameCardBeside(last, "Typed");
expect(middle.classList.contains("appearing")).toBe(true);
expect(scrolled).toHaveBeenCalled();
expect(content?.scrollTop).toBe(0);
});
/**
* A heading has no card to insert beside, so Enter and Shift+Enter reach the ends of the
* column instead.
*/
it("opens the field at the head of the column for Enter on its heading", async () => {
const { container } = await setup();
const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
const header = column.querySelector<HTMLElement>("h3");
const content = column.querySelector<HTMLElement>(".board-column-content");
if (!header || !content) throw new Error("expected a column with a heading");
await act(async () => {
header.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
const drawn = [ ...content.querySelectorAll(".board-note, .board-new-item") ];
expect(drawn[0]?.classList.contains("board-new-item")).toBe(true);
expect(drawn[1]?.classList.contains("board-note")).toBe(true);
});
it("opens the field at the foot of the column for Shift+Enter on its heading", async () => {
const { container } = await setup();
const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
const header = column.querySelector<HTMLElement>("h3");
if (!header) throw new Error("expected a column with a heading");
await act(async () => {
header.dispatchEvent(new KeyboardEvent(
"keydown", { key: "Enter", shiftKey: true, bubbles: true }));
await flush();
});
// The footer's own field, drawn outside the cards rather than among them.
expect(column.querySelector(".board-column-content .board-new-item")).toBeNull();
expect(column.querySelector(".board-new-item.editing")).toBeTruthy();
});
/**
* The same field wherever it stands, so a card inserted between two others is named, iconed and
* made from a template the way a card added below the column is.
*/
it("inserts with the field the column's own footer uses", async () => {
const { container } = await setup();
const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
const card = column.querySelector<HTMLElement>(".board-note");
if (!card) throw new Error("expected a card in the column");
await act(async () => {
card.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
const field = column.querySelector<HTMLElement>(".board-new-item.inserting");
expect(field?.querySelector(".title-editor-icon button")).toBeTruthy();
expect(field?.querySelector(".template-pill button")?.textContent).toContain("Text");
expect(field?.querySelector("textarea")?.placeholder).toBeTruthy();
// Escape takes it down again, having made nothing.
const editor = field?.querySelector<HTMLTextAreaElement>("textarea");
await act(async () => {
editor?.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
await flush();
});
expect(column.querySelector(".board-new-item.inserting")).toBeNull();
});
/**
* The field a card is named in among the others stands for the card being made, so it is taken
* down as the card lands and the card is left focused: the arrow keys carry on from there.
*/
it("leaves the card it made focused, and a card added in the footer unfocused", async () => {
const { container } = await setup();
const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
const card = column.querySelector<HTMLElement>(".board-note");
const noteId = card?.getAttribute("data-note-id");
if (!card || !noteId) throw new Error("expected a card in the column");
// The stand-in for the card just made, which is what the board draws and focuses.
vi.spyOn(BoardApi.prototype, "createNewItemBefore").mockResolvedValue(noteId);
await nameCardBeside(card, "Typed");
expect(column.querySelector(".board-new-item.inserting")).toBeNull();
expect(document.activeElement).toBe(card);
// The footer's own editor keeps focus instead, so a run of cards can be typed into it.
vi.spyOn(BoardApi.prototype, "createNewItem").mockResolvedValue(noteId);
const slot = column.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
if (!editor) throw new Error("expected the new-item editor to be open");
editor.value = "Another";
await act(async () => {
editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
expect(document.activeElement).toBe(editor);
});
/**
* The editor used to hand focus back to whatever held it before it opened, which for a card
* whose editor was opened for it is a different card: that one lit up on the way past.
*/
it("closes a card editor onto its own card, not the one focus came from", async () => {
const { container } = await setup();
const columns = container.querySelectorAll<HTMLElement>(".board-column");
const from = columns[0].querySelector<HTMLElement>(".board-note");
const edited = columns[1].querySelector<HTMLElement>(".board-note");
if (!from || !edited) throw new Error("expected a card in each of two columns");
from.focus();
const cameFrom = vi.spyOn(from, "focus");
await act(async () => {
edited.dispatchEvent(new KeyboardEvent("keydown", { key: "F2", bubbles: true }));
await flush();
});
const editor = edited.querySelector<HTMLTextAreaElement>("textarea");
if (!editor) throw new Error("expected the card editor to be open");
await act(async () => {
editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
await flush();
});
expect(document.activeElement).toBe(edited);
expect(cameFrom).not.toHaveBeenCalled();
});
/**
* The footer editor stays open between cards, so it is left behind with something typed in it
* often enough that saving on the way out would make cards nobody asked for.
*/
it("gives up what is typed in the new-item editor when it loses focus", async () => {
const { container } = await setup();
// Cleared: the spy outlives the test that first stood it up, and its calls with it.
const created = vi.spyOn(BoardApi.prototype, "createNewItem")
.mockResolvedValue(undefined);
created.mockClear();
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
if (!editor) throw new Error("expected the new-item editor to be open");
editor.value = "Half a thought";
await act(async () => {
editor.dispatchEvent(new FocusEvent("blur"));
editor.dispatchEvent(new FocusEvent("focusout", { bubbles: true }));
await flush();
});
expect(created).not.toHaveBeenCalled();
expect(slot?.querySelector("textarea")).toBeNull();
expect(slot?.classList.contains("editing")).toBe(false);
// What was typed is waiting in the editor when it is opened again.
await act(async () => {
slot?.click();
await flush();
});
expect(slot?.querySelector<HTMLTextAreaElement>("textarea")?.value)
.toBe("Half a thought");
});
/**
* Columns are added in runs as a board is set up, so the editor stays where it is with an empty
* field rather than closing and having to be opened again for the next one.
*/
it("clears the add-column editor on Enter, and closes it when it loses focus", async () => {
const { container } = await setup();
const added = vi.spyOn(BoardApi.prototype, "addNewColumn").mockResolvedValue(true);
const slot = container.querySelector<HTMLElement>(".board-add-column");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLInputElement>("input");
if (!editor) throw new Error("expected the add-column editor to be open");
editor.value = "Blocked";
await act(async () => {
editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
expect(added).toHaveBeenCalledWith("Blocked", false, undefined);
expect(slot?.querySelector("input")).toBe(editor);
expect(editor.value).toBe("");
expect(document.activeElement).toBe(editor);
// Left alone, it goes back to being a button, and nothing is added from what it held.
added.mockClear();
editor.value = "Half a name";
await act(async () => {
editor.dispatchEvent(new FocusEvent("blur"));
editor.dispatchEvent(new FocusEvent("focusout", { bubbles: true }));
await flush();
});
expect(added).not.toHaveBeenCalled();
expect(slot?.querySelector("input")).toBeNull();
expect(slot?.classList.contains("editing")).toBe(false);
});
/**
* The button beside the field changes face as the field is typed into, and the field takes its
* value from a prop: a render that fed the old one back took the first character with it.
*/
it("keeps what is typed into the add-column field", async () => {
const { container } = await setup();
const slot = container.querySelector<HTMLElement>(".board-add-column");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLInputElement>("input");
if (!editor) throw new Error("expected the add-column editor to be open");
await type(editor, "B");
expect(editor.value).toBe("B");
await type(editor, "Blocked");
expect(editor.value).toBe("Blocked");
});
/**
* The same picker the column's own heading carries, so a column is given its icon as it is
* named. It is kept for the next column, as the card editor keeps its own.
*/
it("makes a column with the icon picked in the editor, and keeps it for the next", async () => {
const { container } = await setup();
const added = vi.spyOn(BoardApi.prototype, "addNewColumn").mockResolvedValue(true);
added.mockClear();
const slot = container.querySelector<HTMLElement>(".board-add-column");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLInputElement>("input");
if (!editor) throw new Error("expected the add-column editor to be open");
// What a column is drawn with, until something else is picked.
expect(slot?.querySelector(".title-editor-icon button")?.className)
.toContain(DEFAULT_COLUMN_ICON);
await act(async () => {
slot?.querySelector<HTMLElement>(".title-editor-icon button")?.click();
await flush();
});
await type(editor, "Starred");
await act(async () => {
editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
expect(added).toHaveBeenCalledWith("Starred", false, PICKED_ICON);
expect(editor.value).toBe("");
expect(slot?.querySelector(".title-editor-icon button")?.className).toContain(PICKED_ICON);
});
/** The same two ends a card's button offers, named for a board that runs across. */
it("offers both ends of the board from the add-column button", async () => {
const { container } = await setup();
const added = vi.spyOn(BoardApi.prototype, "addNewColumn").mockResolvedValue(true);
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
const slot = container.querySelector<HTMLElement>(".board-add-column");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLInputElement>("input");
if (!editor) throw new Error("expected the add-column editor to be open");
// Nothing typed is nothing to create, so there is no button standing there at all.
expect(slot?.querySelector(".title-editor-submit")).toBeNull();
await type(editor, "Blocked");
const button = slot?.querySelector<HTMLElement>(".title-editor-submit");
if (!button) throw new Error("expected the button beside the field");
button.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
const opened = show.mock.calls.at(-1)?.[0];
const entries = (opened?.items ?? []).flatMap(item =>
item && "title" in item && "handler" in item
? [ { title: item.title, icon: "uiIcon" in item ? item.uiIcon : undefined,
shortcut: "shortcut" in item ? item.shortcut : undefined,
handler: item.handler } ]
: []);
expect(entries.map(entry => [ entry.title, entry.icon, entry.shortcut ])).toEqual([
[ "board_view.create-column-at-start", "bx bx-horizontal-left", "Shift+Enter" ],
[ "board_view.create-column-at-end", "bx bx-horizontal-right", "Enter" ]
]);
// The button stands at the board's right edge, where a menu drawn rightwards is pushed
// back over it.
expect(opened?.orientation).toBe("left");
await act(async () => {
entries[0]?.handler?.({} as never, {} as never);
await flush();
});
expect(added).toHaveBeenCalledWith("Blocked", true, undefined);
expect(editor.value).toBe("");
// Shift+Enter says the same thing from the field itself.
added.mockClear();
await type(editor, "Also first");
await act(async () => {
editor.dispatchEvent(new KeyboardEvent("keydown",
{ key: "Enter", shiftKey: true, bubbles: true }));
await flush();
});
expect(added).toHaveBeenCalledWith("Also first", true, undefined);
show.mockRestore();
});
/**
* The same as Enter, for a reader who would rather press something: pressing it must not take
* focus out of the field first, since losing focus is what closes the editor.
*/
it("makes a card from the button inside the new-item editor, and clears it", async () => {
const { container } = await setup();
const created = vi.spyOn(BoardApi.prototype, "createNewItem")
.mockResolvedValue(undefined);
created.mockClear();
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
const submit = slot?.querySelector<HTMLElement>(".title-editor-submit");
if (!editor || !submit) throw new Error("expected the editor and its button");
await type(editor, "From the button");
await act(async () => {
submit.dispatchEvent(new MouseEvent("click", { bubbles: true }));
await flush();
});
expect(created).toHaveBeenCalledWith("Doing", "From the button", "bottom", undefined, textTemplate());
expect(editor.value).toBe("");
expect(slot?.querySelector("textarea")).toBe(editor);
expect(document.activeElement).toBe(editor);
});
/**
* The pill at the foot of the new-card field names what the card will be made from. It offers
* what the board offers, and picking one is remembered: the next card is made from it, and so
* is the next editor opened.
*/
it("makes a card from the template picked in the pill, and remembers it", async () => {
const { container } = await setup();
const created = vi.spyOn(BoardApi.prototype, "createNewItem")
.mockResolvedValue(undefined);
created.mockClear();
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
const pill = slot?.querySelector<HTMLElement>(".template-pill button");
if (!editor || !pill) throw new Error("expected the editor and its template pill");
// The first the board offers, until something else is picked.
expect(pill.textContent).toContain("Text");
await act(async () => {
pill.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true }));
$(pill.closest(".dropdown") as HTMLElement).children("button:not([aria-expanded=true])").trigger("click");
await flush();
});
// The menu is portalled to the page, and the boards this file rendered before left theirs
// behind, so it is the last one that belongs to the board under test.
const menu = [ ...document.querySelectorAll<HTMLElement>(".template-pill") ].at(-1);
const items = [ ...(menu?.querySelectorAll<HTMLElement>(".dropdown-item") ?? []) ];
expect(items.map(item => item.textContent?.trim())).toEqual([
"Text", "Markdown", "Canvas", "Spreadsheet", "board_view.more-templates"
]);
// The one a card would be made from is ticked, rather than standing out by its background.
expect(items.map(item => !!item.querySelector(".menu-trailing-icon")))
.toEqual([ true, false, false, false, false ]);
expect(items.some(item => item.classList.contains("active"))).toBe(false);
await act(async () => {
items[2].click();
await flush();
});
// Stored on the board, so the next editor opens on it as well.
expect(saved.at(-1)?.template).toBe("type:canvas:application/json");
// Picking closes the menu, as any dropdown item click does; open it again to read the tick.
await act(async () => {
pill.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true }));
$(pill.closest(".dropdown") as HTMLElement).children("button:not([aria-expanded=true])").trigger("click");
await flush();
});
const reopened = [ ...document.querySelectorAll<HTMLElement>(".template-pill") ].at(-1);
expect([ ...(reopened?.querySelectorAll(".dropdown-item") ?? []) ]
.map(item => !!item.querySelector(".menu-trailing-icon")))
.toEqual([ false, false, true, false, false ]);
await type(editor, "Drawn");
await act(async () => {
editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
expect(created).toHaveBeenCalledWith("Doing", "Drawn", "bottom", undefined,
expect.objectContaining({ id: "type:canvas:application/json" }));
expect(slot?.querySelector(".template-pill button")?.textContent).toContain("Canvas");
});
/**
* Opening the pill's menu takes focus off the field: Bootstrap focuses the toggle it opens
* from. Losing focus is what closes this editor, so the editor would take the menu down with
* itself the moment it was opened, and the toggle would then find nothing to open.
*/
it("stays open while the template pill's menu has the focus", async () => {
const { container } = await setup();
const created = vi.spyOn(BoardApi.prototype, "createNewItem")
.mockResolvedValue(undefined);
created.mockClear();
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
const pill = slot?.querySelector<HTMLElement>(".template-pill button");
if (!editor || !pill) throw new Error("expected the editor and its pill");
await type(editor, "Still here");
await act(async () => {
pill.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true }));
$(pill.closest(".dropdown") as HTMLElement).children("button:not([aria-expanded=true])").trigger("click");
editor.dispatchEvent(new FocusEvent("blur"));
editor.dispatchEvent(new FocusEvent("focusout", { bubbles: true }));
await flush();
});
expect(slot?.querySelector("textarea")).toBe(editor);
expect(editor.value).toBe("Still here");
expect(created).not.toHaveBeenCalled();
// Once the menu is done with, the field has the focus again.
await act(async () => {
$(pill.closest(".dropdown") as HTMLElement).children("button[aria-expanded=true]").trigger("click");
await flush();
});
expect(document.activeElement).toBe(editor);
});
/**
* A board outlives the templates it read: one made while it stands would never be offered, and
* one deleted would go on being offered. A template is a note carrying `#template`, so a change
* to one of those attributes is what sends the board back for the list.
*/
it("reads the templates again when one is made or taken away", async () => {
const { host } = await setup();
const before = templateReads;
// A change to something else leaves the list where it is.
await act(async () => {
await host.handleEvent("entitiesReloaded", { loadResults: attributeChanged("status") });
await flush();
});
expect(templateReads).toBe(before);
await act(async () => {
await host.handleEvent("entitiesReloaded", { loadResults: attributeChanged("template") });
await flush();
});
expect(templateReads).toBe(before + 1);
// And a note that stops being a template, which is the same row with `isDeleted` set.
await act(async () => {
await host.handleEvent("entitiesReloaded",
{ loadResults: attributeChanged("template", "someNote", true) });
await flush();
});
expect(templateReads).toBe(before + 2);
});
/**
* A template's icon is what the picker and the pill show, and an icon is a label on the note
* rather than part of the note row: the change is reported as an attribute changing, which is
* not what a reloaded note reads as. The label is not always `iconClass`, so any label on a
* note the board offers sends it back for the list. Notes it offers nothing from are its own.
*/
it("reads the templates again when one it offers is given another icon", async () => {
const reading = vi.mocked(getNoteTypeOptions);
const stockAnswer = reading.getMockImplementation();
const template = {
id: "template:tmpl1",
title: "My template",
icon: "bx bx-star",
group: "user",
options: { type: "text", mime: "text/html", templateNoteId: "tmpl1" }
} as NoteTypeOption;
reading.mockImplementation(async () => [ ...NOTE_TYPE_OPTIONS, template ]);
try {
const { host } = await setup();
const before = reading.mock.calls.length;
// An icon given to a note the board offers nothing from is not its business.
await act(async () => {
await host.handleEvent("entitiesReloaded",
{ loadResults: attributeChanged("iconClass") });
await flush();
});
expect(reading.mock.calls.length).toBe(before);
await act(async () => {
await host.handleEvent("entitiesReloaded",
{ loadResults: attributeChanged("iconClass", "tmpl1") });
await flush();
});
expect(reading.mock.calls.length).toBe(before + 1);
// And an icon taken away again, which is the same row with `isDeleted` set.
await act(async () => {
await host.handleEvent("entitiesReloaded",
{ loadResults: attributeChanged("iconClass", "tmpl1", true) });
await flush();
});
expect(reading.mock.calls.length).toBe(before + 2);
// A note with no icon of its own wears its workspace's, which is another label again.
await act(async () => {
await host.handleEvent("entitiesReloaded",
{ loadResults: attributeChanged("workspaceIconClass", "tmpl1") });
await flush();
});
expect(reading.mock.calls.length).toBe(before + 3);
} finally {
reading.mockImplementation(stockAnswer ?? (async () => NOTE_TYPE_OPTIONS));
}
});
/**
* Two reads can be in flight at once: the one the board makes as it arrives, and one a template
* being made or deleted asks for. The server need not answer in the order it was asked, so the
* answer to the older read is not what the board is left offering.
*/
it("keeps the newest reading of what a card can be made from", async () => {
const answers: Answer[] = [];
const reading = vi.mocked(getNoteTypeOptions);
const stockAnswer = reading.getMockImplementation();
reading.mockImplementation(() =>
new Promise((resolve, reject) => { answers.push({ resolve, reject }); }));
try {
// Both reads are made, and neither has been answered.
const { host, container } = await setup();
await act(async () => {
await host.handleEvent("entitiesReloaded",
{ loadResults: attributeChanged("template") });
await flush();
});
expect(answers.length).toBe(2);
// The newer read is answered first, and the older one after it, with nothing at all.
await act(async () => {
answers[1].resolve(NOTE_TYPE_OPTIONS);
answers[0].resolve([]);
await flush();
});
// The pill names what a card would be made from, which the older answer has none of.
expect(await pillNames(container)).toContain("Text");
} finally {
reading.mockImplementation(stockAnswer ?? (async () => NOTE_TYPE_OPTIONS));
}
});
/**
* A read that fails leaves no answer at all, so what an older read answered is what the board
* has: it is taken rather than thrown away for a newer read that came back with nothing.
*/
it("takes an older answer when the newer read fails", async () => {
const answers: Answer[] = [];
const reading = vi.mocked(getNoteTypeOptions);
const stockAnswer = reading.getMockImplementation();
reading.mockImplementation(() =>
new Promise((resolve, reject) => { answers.push({ resolve, reject }); }));
try {
const { host, container } = await setup();
await act(async () => {
await host.handleEvent("entitiesReloaded",
{ loadResults: attributeChanged("template") });
await flush();
});
expect(answers.length).toBe(2);
await act(async () => {
answers[1].reject(new Error("offline"));
answers[0].resolve(NOTE_TYPE_OPTIONS);
await flush();
});
expect(await pillNames(container)).toContain("Text");
} finally {
reading.mockImplementation(stockAnswer ?? (async () => NOTE_TYPE_OPTIONS));
}
});
/** The pill's last entry leads where the board's own menu does: how the board is set up. */
it("opens the board's properties from the pill, listing what it offers in order", async () => {
const { container } = await setup();
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const pill = slot?.querySelector<HTMLElement>(".template-pill button");
if (!pill) throw new Error("expected the template pill");
await act(async () => {
pill.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true }));
$(pill.closest(".dropdown") as HTMLElement).children("button:not([aria-expanded=true])").trigger("click");
await flush();
});
const menus = [ ...document.querySelectorAll<HTMLElement>(".template-pill") ];
const more = [ ...(menus.at(-1)?.querySelectorAll<HTMLElement>(".dropdown-item") ?? []) ]
.find(item => item.textContent?.includes("more-templates"));
if (!more) throw new Error("expected a way to the dialog");
await act(async () => {
more.click();
await flush();
});
// The card reads what a note can be made from as it is drawn, which takes a moment.
await act(async () => { await flush(); });
// The last of them: a dialog portalled to the page outlives the board that drew it here.
const dialog = [ ...document.querySelectorAll<HTMLElement>(".board-properties-dialog") ]
.at(-1);
expect(dialog).toBeTruthy();
// What the board offers, in the order it stores them, each carried by a grip of its own.
const templates = dialog?.querySelector<HTMLElement>(".template-selection-card");
const named = [ ...(templates?.querySelectorAll(".template-selection-name") ?? []) ]
.map(element => element.textContent);
expect(named).toEqual([ "Text", "Markdown", "Canvas", "Spreadsheet" ]);
expect(templates?.querySelectorAll(".tn-sortable-grip").length).toBe(4);
expect(templates?.querySelectorAll(".tn-card-adder").length).toBe(2);
// And the attributes the cards show, which the same dialog arranges.
expect(dialog?.querySelector(".promoted-attributes-card")).toBeTruthy();
await dismiss(dialog);
});
/**
* The icon stands inside the field, at its head, and is what the card is made with. It is kept
* between cards, unlike the title: a run of cards is often a run of the same kind of card.
*/
it("makes a card with the icon picked in the editor, and keeps it for the next", async () => {
const { container } = await setup();
const created = vi.spyOn(BoardApi.prototype, "createNewItem")
.mockResolvedValue(undefined);
created.mockClear();
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
const picker = slot?.querySelector<HTMLElement>(".title-editor-icon");
if (!editor || !picker) throw new Error("expected the editor and its icon");
// What a text note is drawn with, until something else is picked.
expect(picker.querySelector("button")?.className).toContain("bx bx-note");
await pickIcon(slot);
expect(slot?.querySelector(".title-editor-icon button")?.className)
.toContain(PICKED_ICON);
await type(editor, "Starred");
await act(async () => {
editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
expect(created).toHaveBeenCalledWith("Doing", "Starred", "bottom", PICKED_ICON, textTemplate());
// The title goes, the icon stays.
expect(editor.value).toBe("");
expect(slot?.querySelector(".title-editor-icon button")?.className)
.toContain(PICKED_ICON);
});
/**
* The icon follows the template while it is still the template's own. One the reader picked
* stands: the template changing under it says nothing about the icon they chose.
*/
it("moves the card's icon to the template picked, unless one was picked by hand", async () => {
const { container } = await setup();
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const shownIcon = () => slot?.querySelector(".title-editor-icon button")?.className ?? "";
// The icon of the template the board opens on, rather than a stock one.
expect(shownIcon()).toContain("bx bx-note");
await pickTemplate(slot, 2);
expect(shownIcon()).toContain("bx bx-pen");
await pickIcon(slot);
await pickTemplate(slot, 1);
expect(shownIcon()).toContain(PICKED_ICON);
});
/** Picks one of the templates the pill offers, by where it stands in its menu. */
async function pickTemplate(slot: HTMLElement | null | undefined, index: number) {
const pill = slot?.querySelector<HTMLElement>(".template-pill button");
if (!pill) throw new Error("expected the template pill");
await act(async () => {
pill.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true }));
$(pill.closest(".dropdown") as HTMLElement).children("button:not([aria-expanded=true])").trigger("click");
await flush();
});
// The menu is portalled to the page, and the boards rendered before left theirs behind, so
// the last one is the board under test.
const menu = [ ...document.querySelectorAll<HTMLElement>(".template-pill") ].at(-1);
const items = [ ...(menu?.querySelectorAll<HTMLElement>(".dropdown-item") ?? []) ];
await act(async () => {
items[index].click();
await flush();
});
}
/**
* The picker takes focus with it, and losing focus is what closes the editor: it would take the
* picker down with itself the moment it was opened.
*/
it("stays open while the icon picker has the focus", async () => {
const { container } = await setup();
const created = vi.spyOn(BoardApi.prototype, "createNewItem")
.mockResolvedValue(undefined);
// The spy outlives the test that made it, and with it what it was called with there.
created.mockClear();
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
if (!editor) throw new Error("expected the new-item editor to be open");
await type(editor, "Still here");
await act(async () => {
slot?.querySelector<HTMLElement>(".picker-open")?.click();
editor.dispatchEvent(new FocusEvent("blur"));
editor.dispatchEvent(new FocusEvent("focusout", { bubbles: true }));
await flush();
});
expect(slot?.querySelector("textarea")).toBe(editor);
expect(editor.value).toBe("Still here");
expect(created).not.toHaveBeenCalled();
// The blur that would have ended the edit is spent, so the field is focused again.
await act(async () => {
slot?.querySelector<HTMLElement>(".picker-close")?.click();
await flush();
});
expect(document.activeElement).toBe(editor);
});
/** Renaming a card makes nothing, so the button that creates is not offered beside it. */
it("offers no create button while a card's title is edited", async () => {
const { container } = await setup();
const card = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-note");
if (!card) throw new Error("expected a card");
await act(async () => {
card.dispatchEvent(new KeyboardEvent("keydown", { key: "F2", bubbles: true }));
await flush();
});
const editor = card.querySelector<HTMLTextAreaElement>("textarea");
if (!editor) throw new Error("expected the title editor");
await type(editor, "Renamed");
expect(card.querySelector(".title-editor-submit")).toBeNull();
// The icon is offered all the same, which is what the editor is dressed for.
expect(card.querySelector(".title-editor-icon")).toBeTruthy();
});
/** The same picker on a card being renamed, which writes the icon onto the card itself. */
it("writes the icon picked while a card's title is edited", async () => {
const { container } = await setup();
const setLabel = vi.spyOn(attributes, "setLabel").mockResolvedValue(undefined);
const card = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-note");
const noteId = card?.getAttribute("data-note-id");
if (!card || !noteId) throw new Error("expected a card");
await act(async () => {
card.dispatchEvent(new KeyboardEvent("keydown", { key: "F2", bubbles: true }));
await flush();
});
expect(card.querySelector("textarea")).toBeTruthy();
await pickIcon(card);
expect(setLabel).toHaveBeenCalledWith(noteId, "iconClass", PICKED_ICON);
setLabel.mockRestore();
});
/** Takes an icon from the picker standing in the given editor. */
async function pickIcon(host: HTMLElement | null | undefined) {
const button = host?.querySelector<HTMLElement>(".title-editor-icon button");
if (!button) throw new Error("expected an icon beside the field");
await act(async () => {
button.click();
await flush();
});
}
/**
* Cards are made at the end of a column, and at its head for the reader adding what comes next
* rather than what comes last. Both ends are reached from the field: Enter for the end, and
* Shift+Enter or the button's own menu for the head.
*/
it("makes a card at the head of the column on Shift+Enter", async () => {
const { container } = await setup();
const created = vi.spyOn(BoardApi.prototype, "createNewItem")
.mockResolvedValue(undefined);
created.mockClear();
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
if (!editor) throw new Error("expected the new-item editor to be open");
await type(editor, "On top");
await act(async () => {
editor.dispatchEvent(new KeyboardEvent("keydown",
{ key: "Enter", shiftKey: true, bubbles: true }));
await flush();
});
expect(created).toHaveBeenCalledWith("Doing", "On top", "top", undefined, textTemplate());
// The field is emptied for the next one, as it is for a card made at the end.
expect(editor.value).toBe("");
});
it("offers both ends of the column from the button's own menu", async () => {
const { container } = await setup();
const created = vi.spyOn(BoardApi.prototype, "createNewItem")
.mockResolvedValue(undefined);
created.mockClear();
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
const button = slot?.querySelector<HTMLElement>(".title-editor-submit");
if (!editor || !button) throw new Error("expected the editor and its button");
// Nothing typed: there is nowhere to put a card that is not being made.
button.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
expect(show).not.toHaveBeenCalled();
await type(editor, "Either end");
button.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
const items = show.mock.calls.at(-1)?.[0].items ?? [];
const entries = items.flatMap(item => item && "title" in item && "handler" in item
? [ { title: item.title, shortcut: "shortcut" in item ? item.shortcut : undefined,
handler: item.handler } ]
: []);
expect(entries.map(entry => [ entry.title, entry.shortcut ])).toEqual([
[ "board_view.create-at-top", "Shift+Enter" ],
[ "board_view.create-at-bottom", "Enter" ]
]);
await act(async () => {
entries[0]?.handler?.({} as never, {} as never);
await flush();
});
expect(created).toHaveBeenCalledWith("Doing", "Either end", "top", undefined, textTemplate());
expect(editor.value).toBe("");
show.mockRestore();
});
/** A finger has no second button, so it holds the create button to reach the same menu. */
it("opens the placement menu on a hold, and gives it up for a scroll", async () => {
const { container } = await setup();
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
const button = slot?.querySelector<HTMLElement>(".title-editor-submit");
if (!editor || !button) throw new Error("expected the editor and its button");
await type(editor, "Held");
// Only now: the board is drawn through timers of its own, which would never come round.
vi.useFakeTimers();
const press = (type: string, at: number, pointerType = "touch") =>
button.dispatchEvent(new PointerEvent(type, {
bubbles: true, pointerType, clientX: at, clientY: at
}));
// A finger that walks away is scrolling the board, and the menu is not what it asked for.
press("pointerdown", 0);
press("pointermove", 40);
vi.advanceTimersByTime(1000);
expect(show).not.toHaveBeenCalled();
press("pointerdown", 0);
vi.advanceTimersByTime(1000);
expect(show).toHaveBeenCalled();
// The press ends in a click of its own, which must not reach the page and close the menu.
const reached = vi.fn();
document.addEventListener("click", reached);
button.dispatchEvent(new MouseEvent("click", { bubbles: true }));
document.removeEventListener("click", reached);
expect(reached).not.toHaveBeenCalled();
// A mouse has its own way there, so holding it down offers nothing.
show.mockClear();
press("pointerdown", 0, "mouse");
vi.advanceTimersByTime(1000);
expect(show).not.toHaveBeenCalled();
vi.useRealTimers();
show.mockRestore();
});
/**
* With nothing typed there is nothing to save, so the button reaches for a note that exists
* already instead, and goes back to making one as soon as the field holds anything.
*/
it("offers an existing note from the new-item button while the field is empty", async () => {
const { container } = await setup();
const chosen = vi.spyOn(dialog, "chooseNote").mockResolvedValue("existing");
const added = vi.spyOn(BoardApi.prototype, "addExistingItem").mockResolvedValue(true);
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.click();
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
const button = () => slot?.querySelector<HTMLElement>(".title-editor-submit");
if (!editor) throw new Error("expected the editor");
expect(button()?.className).toContain("bx-folder-open");
await act(async () => {
slot?.querySelector<HTMLElement>(".title-editor-submit")
?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
await flush();
});
expect(chosen).toHaveBeenCalled();
// No place given: the field below a column adds the note at the end of it.
expect(added).toHaveBeenCalledWith("Doing", "existing", undefined);
await type(editor, "Typed");
expect(button()?.className).toContain("bx-plus-circle");
});
it("starts the new item off with the character typed on its button", async () => {
const { container } = await setup();
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
await act(async () => {
slot?.dispatchEvent(new KeyboardEvent("keydown", { key: "R", bubbles: true }));
await flush();
});
const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");
expect(editor?.value).toBe("R");
// After the character rather than over it, so the next key carries on.
expect([ editor?.selectionStart, editor?.selectionEnd ]).toEqual([ 1, 1 ]);
});
it("opens the new item empty for Enter, and ignores keys that are not characters", async () => {
const { container } = await setup();
const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]
.querySelector<HTMLElement>(".board-new-item");
for (const key of [ "Tab", "ArrowRight", "F2" ]) {
await act(async () => {
slot?.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true }));
await flush();
});
}
expect(slot?.querySelector("textarea")).toBeNull();
await act(async () => {
slot?.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
expect(slot?.querySelector<HTMLTextAreaElement>("textarea")?.value).toBe("");
});
/**
* Both boards record the very same rename, so the failing one cannot tell its own record from
* the live one by looking at it. Each board keeps a map of its own, and the undo writes into
* the one it recorded itself in, which nothing reads any more.
*/
it("does not undo the next board's identical rename when the last one fails", async () => {
const { container } = await setup();
// Both renames are held open, so both records are live when the first one fails.
let rejectFirst: (reason: Error) => void = () => {};
let resolveSecond: () => void = () => {};
vi.mocked(executeBulkActions)
.mockImplementationOnce(() => new Promise((_r, reject) => { rejectFirst = reject; }))
.mockImplementationOnce(() => new Promise((resolve) => { resolveSecond = resolve; }));
// "Done" on both boards, so neither record can be told from the other by its contents.
await renameColumnAt(container, 2, "Renamed");
const other = buildNote({
title: "Other board",
"#collection": "",
"#viewType": "board",
children: [
{ title: "Fourth", "#status": "Doing" },
{ title: "Fifth", "#status": "Done" }
]
});
const showOther = async () => {
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={other}
noteIds={[ ...other.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "Doing" }, { value: "Done" } ] }}
/>
</ParentComponent.Provider>,
container
);
await flush();
});
await act(async () => { await flush(); });
};
await showOther();
await renameColumnAt(container, 1, "Renamed");
await act(async () => {
rejectFirst(new Error("offline"));
await flush();
});
// Rendered again, which is what reads the record the failed rename could have taken away.
await showOther();
expect(columnTitles(container)).toEqual([ "Doing", "Renamed" ]);
resolveSecond();
});
it("says so when what was typed could not be saved", async () => {
const { container } = await setup();
const error = vi.spyOn(toast, "showError").mockImplementation(() => {});
vi.spyOn(server, "put").mockRejectedValueOnce(new Error("offline"));
await renameSecondColumn(container, "Renamed");
await act(async () => { await flush(); });
expect(error).toHaveBeenCalledWith("board_view.save-error");
});
it("lets an IME finish composing before taking the Enter as a save", async () => {
const { container } = await setup();
const header = container.querySelectorAll<HTMLElement>(".board-column h3")[1];
await act(async () => {
startEditingTitle(header.closest(".board-column") as HTMLElement);
await flush();
});
const input = header.querySelector<HTMLInputElement>("input");
if (!input) throw new Error("expected the title editor");
// The Enter that commits a CJK conversion must not also close the editor.
await act(async () => {
input.focus();
input.value = "Composing";
input.dispatchEvent(new KeyboardEvent("keydown", {
key: "Enter", isComposing: true, bubbles: true, cancelable: true
}));
await flush();
});
expect(header.querySelector("input")).toBeTruthy();
expect(saved).toHaveLength(0);
});
it("keeps the cards of the renamed column under it", async () => {
const { container } = await setup();
await renameSecondColumn(container, "In Progress");
const cards = [ ...container.querySelectorAll<HTMLElement>(".board-column") ]
.map(column => [ ...column.querySelectorAll(".board-note .title") ]
.map(el => el.textContent));
expect(cards).toEqual([ [ "First" ], [ "Second" ], [ "Third" ] ]);
});
});
describe("Board editors and menus", () => {
let container: HTMLElement | undefined;
beforeEach(() => {
saved.length = 0;
vi.restoreAllMocks();
vi.spyOn(server, "put").mockResolvedValue(undefined);
});
afterEach(() => {
if (container) {
render(null, container);
container.remove();
container = undefined;
}
});
it("opens the add-column editor with Enter, and names the column typed into it", async () => {
const board = await renderBoard();
const slot = board.querySelector<HTMLElement>(".board-add-column");
await act(async () => {
slot?.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
const input = slot?.querySelector<HTMLInputElement>("input");
if (!input) throw new Error("expected the column-name editor");
await act(async () => {
input.focus();
input.value = "Blocked";
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
await act(async () => { await flush(); });
expect(saved.at(-1)?.columns?.map(column => column.value))
.toEqual([ "To Do", "Done", "Blocked" ]);
});
it("says so rather than adding a column the board already has", async () => {
const board = await renderBoard();
const message = vi.spyOn(toast, "showMessage").mockImplementation(() => {});
await addColumnNamed(board, "Done");
expect(message).toHaveBeenCalled();
expect(saved).toHaveLength(0);
});
it("leaves the title alone when the editor is dismissed with Escape", async () => {
const board = await renderBoard();
const header = board.querySelectorAll<HTMLElement>(".board-column h3")[0];
await act(async () => {
header.focus();
header.dispatchEvent(new KeyboardEvent("keydown", { key: "F2", bubbles: true }));
await flush();
});
const input = header.querySelector<HTMLInputElement>("input");
if (!input) throw new Error("expected the title editor");
await act(async () => {
input.focus();
input.value = "Renamed away";
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
await flush();
});
await act(async () => { await flush(); });
expect(saved).toHaveLength(0);
expect(columnTitles(board)).toEqual([ "To Do", "Done" ]);
});
it("opens the column menu from its button as well as from a right click", async () => {
const board = await renderBoard();
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
await act(async () => {
board.querySelector<HTMLElement>(".board-column .column-menu")?.click();
await flush();
});
expect(show).toHaveBeenCalled();
});
it("puts a column beside another from the menu, ready to be named", async () => {
const board = await renderBoard();
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
board.querySelector(".board-column h3")
?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
const parent = (show.mock.calls.at(-1)?.[0].items ?? [])
.find(item => item && "uiIcon" in item && item.uiIcon === "bx bx-columns");
if (!parent || !("items" in parent)) throw new Error("expected an add-column entry");
const after = (parent.items ?? [])[1];
if (!after || !("handler" in after)) throw new Error("expected an after entry");
await act(async () => {
after.handler?.(after, {} as never);
await flush();
});
await act(async () => { await flush(); });
expect(saved.at(-1)?.columns?.map(column => column.value))
.toEqual([ "To Do", "board_view.new-column", "Done" ]);
// The column it made is the one waiting to be named.
expect(board.querySelectorAll(".board-column")[1].querySelector("input")).toBeTruthy();
});
it("keeps a wheel inside a scrolling column from also scrolling the board", async () => {
const board = await renderBoard();
const content = board.querySelector<HTMLElement>(".board-column-content");
if (!content) throw new Error("expected a scrollable column body");
Object.defineProperty(content, "scrollHeight", { value: 900, configurable: true });
Object.defineProperty(content, "clientHeight", { value: 300, configurable: true });
let reachedBoard = false;
board.addEventListener("wheel", () => { reachedBoard = true; });
await act(async () => {
content.dispatchEvent(new Event("wheel", { bubbles: true }));
await flush();
});
expect(reachedBoard).toBe(false);
});
/** Types a name into the add-column editor and commits it by blurring. */
async function addColumnNamed(board: HTMLElement, name: string) {
const slot = board.querySelector<HTMLElement>(".board-add-column");
await act(async () => {
slot?.click();
await flush();
});
const input = slot?.querySelector<HTMLInputElement>("input");
if (!input) throw new Error("expected the column-name editor");
await act(async () => {
input.focus();
input.value = name;
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
await act(async () => { await flush(); });
}
/**
* The backdrop and the frozen cards are driven by classes rather than by `:has()`: a `:has()`
* naming a descendant makes every card insertion invalidate the whole board.
*/
it("marks the board and the one column while a card title is being edited", async () => {
const board = await renderBoard();
const view = board.querySelector<HTMLElement>(".board-view");
const columns = board.querySelectorAll<HTMLElement>(".board-column");
const card = columns[0]?.querySelector<HTMLElement>(".board-note");
if (!view || !card || columns.length < 2) throw new Error("expected a card in two columns");
expect(view.classList.contains("editing-open")).toBe(false);
await act(async () => {
card.dispatchEvent(new KeyboardEvent("keydown", { key: "F2", bubbles: true }));
await flush();
});
expect(view.classList.contains("editing-open")).toBe(true);
expect(columns[0].classList.contains("editing-open")).toBe(true);
expect(columns[1].classList.contains("editing-open")).toBe(false);
const editor = card.querySelector<HTMLTextAreaElement>("textarea");
if (!editor) throw new Error("expected the card editor to be open");
await act(async () => {
editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
await flush();
});
expect(view.classList.contains("editing-open")).toBe(false);
expect(columns[0].classList.contains("editing-open")).toBe(false);
});
it("marks a field opened between cards, and leaves the one at a column's foot unmarked", async () => {
const board = await renderBoard();
const view = board.querySelector<HTMLElement>(".board-view");
const columns = board.querySelectorAll<HTMLElement>(".board-column");
const card = columns[0]?.querySelector<HTMLElement>(".board-note");
if (!view || !card || columns.length < 2) throw new Error("expected a card in two columns");
await act(async () => {
card.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
expect(board.querySelector(".board-new-item.inserting")).toBeTruthy();
expect(view.classList.contains("editing-open")).toBe(true);
expect(columns[0].classList.contains("editing-open")).toBe(true);
expect(columns[1].classList.contains("editing-open")).toBe(false);
const field = columns[0].querySelector<HTMLTextAreaElement>(
".board-new-item.inserting textarea");
if (!field) throw new Error("expected the insert field to be open");
await act(async () => {
field.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
await flush();
});
expect(view.classList.contains("editing-open")).toBe(false);
// The field below a column makes a card the same way but leaves the board undimmed.
const footer = columns[1].querySelector<HTMLElement>(".board-new-item");
if (!footer) throw new Error("expected the column footer");
await act(async () => {
footer.dispatchEvent(new MouseEvent("click", { bubbles: true }));
await flush();
});
expect(footer.classList.contains("editing")).toBe(true);
expect(footer.classList.contains("inserting")).toBe(false);
expect(view.classList.contains("editing-open")).toBe(false);
expect(columns[1].classList.contains("editing-open")).toBe(false);
});
async function renderBoard() {
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
children: [
{ title: "First", "#status": "To Do" },
{ title: "Second", "#status": "Done" }
]
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" }, { value: "Done" } ] }}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
return mountPoint;
}
});
describe("Board grouped by a relation", () => {
let container: HTMLElement | undefined;
afterEach(() => {
if (container) {
render(null, container);
container.remove();
container = undefined;
}
});
it("names its columns with a link to the note each stands for", async () => {
const { board } = await renderRelationBoard();
// The note's own icon is what a relation column shows, so it offers no picker of its own.
expect(board.querySelector(".board-column h3 a")).toBeTruthy();
expect(board.querySelector(".board-column h3 > .column-icon")).toBeNull();
});
it("picks a note rather than typing a name when a column is renamed", async () => {
const { board } = await renderRelationBoard();
const header = board.querySelector<HTMLElement>(".board-column h3");
await act(async () => {
header?.dispatchEvent(new KeyboardEvent("keydown", { key: "F2", bubbles: true }));
await flush();
});
// The note picker, not the plain text box a label column is renamed through. Dismissing it
// is the autocomplete's own jQuery binding, which does not answer keys under happy-dom.
expect(header?.querySelector("input.note-autocomplete")).toBeTruthy();
});
async function renderRelationBoard() {
const target = buildNote({ title: "In progress" });
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
"#board:groupBy": "~status",
children: [ { title: "First", "~status": target.noteId } ]
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<BoardView
note={note}
notePath={`root/${note.noteId}`}
noteIds={[ ...note.getChildNoteIds() ]}
highlightedTokens={null}
viewConfig={{ columns: [ { value: target.noteId } ] }}
saveConfig={() => {}}
media="screen"
onReady={() => {}}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
// The link resolves its note path before it renders, which lands a tick later.
await act(async () => { await flush(); });
return { board: mountPoint, target };
}
});
describe("the promoted attributes a card shows", () => {
let container: HTMLElement;
let drawn = 0;
afterEach(() => {
render(null, container);
container.remove();
});
it("draws every one of them until the board's config arranges them", async () => {
await draw();
expect(pills()).toEqual([ "Due: 2026-01-01", "owner: Ada" ]);
});
it("draws them in the stored order, and leaves out what is hidden", async () => {
await draw([ { name: "owner" }, { name: "dueDate", hidden: true } ]);
expect(pills()).toEqual([ "owner: Ada" ]);
});
it("draws an attribute the config has never named, after the ones it has", async () => {
await draw([ { name: "owner" } ]);
expect(pills()).toEqual([ "owner: Ada", "Due: 2026-01-01" ]);
});
/** A board defining two promoted labels, one card carrying a value for each. */
async function draw(promotedAttributes?: { name: string, hidden?: boolean }[]) {
// The cache keeps what a note id was built with, so a card of its own is what keeps this
// test's values off the one the test before it drew.
const card = `promotedCard${++drawn}`;
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
"#label:dueDate(inheritable)": "promoted,alias=Due,single,text",
"#label:owner(inheritable)": "promoted,single,text",
children: [
{ id: card, title: "First", "#status": "To Do", "#dueDate": "2026-01-01",
"#owner": "Ada" }
]
});
container = document.body.appendChild(document.createElement("div"));
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" } ], promotedAttributes }}
/>
</ParentComponent.Provider>,
container
);
});
await act(async () => { await flush(); });
}
function pills() {
return [ ...container.querySelectorAll(".board-note .user-attribute") ]
.map(element => element.textContent);
}
});
describe("Board filtering", () => {
let container: HTMLElement;
let host: Component;
afterEach(() => {
vi.useRealTimers();
saved.length = 0;
render(null, container);
container.remove();
});
/** A board of four cards over two columns, opened with a stored filter query. */
async function setup({ matched, tokens = [], limit, columns }: {
matched: string[];
tokens?: { token: string; type: "plain" }[];
limit?: number;
/** The stored columns, in place of an open "To Do" and "Done". */
columns?: BoardViewData["columns"];
}) {
searchInSubtree.mockReset();
searchInSubtree.mockResolvedValue({
searchResultNoteIds: matched,
highlightedTokens: tokens,
error: null
});
const note = buildNote({
title: "Board",
// The header the filter box stands in is drawn for a collection note alone.
type: "book",
"#collection": "",
"#viewType": "board",
children: [
{ id: "filtered1", title: "First", "#status": "To Do" },
{ id: "filtered2", title: "Second", "#status": "To Do" },
{ id: "filtered3", title: "Third", "#status": "To Do" },
{ id: "filtered4", title: "Fourth", "#status": "Done" }
]
});
host = new Component();
container = document.body.appendChild(document.createElement("div"));
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{
columns: columns ?? [ { value: "To Do", ...(limit ? { limit } : {}) },
{ value: "Done" } ],
filterQuery: "#urgent"
}}
/>
</ParentComponent.Provider>,
container
);
});
await act(async () => { await flush(); });
await act(async () => { await flush(); });
return note;
}
function cardTitles(column: number) {
const columns = [ ...container.querySelectorAll(".board-column") ];
return [ ...columns[column].querySelectorAll(".board-note .title") ]
.map(el => el.textContent);
}
/** Presses the filter's clear button, which stands in the collection header. */
async function clearFilter() {
const clear = container.querySelector<HTMLElement>(".collection-filter-clear");
if (!clear) throw new Error("expected the filter's clear button");
await act(async () => {
clear.click();
await flush();
});
}
/**
* Lets animation frames run, one pass each: a render is flushed only as a pass ends. The
* hold takes two frames, and the release is drawn on the one after.
*/
async function nextFrames(count: number) {
for (let frame = 0; frame < count; frame++) {
await act(async () => { await nextFrame(); });
}
}
/** A branch change under the board, which is what re-runs an active filter. */
function branchChange(parentNoteId: string, noteId: string) {
const results = new LoadResults([ {
entityName: "branches",
entityId: "branch1",
entity: { branchId: "branch1", parentNoteId, noteId }
} as never ]);
results.addBranch("branch1", "other");
return results;
}
/**
* The move is drawn at once, into the unfiltered map. Counting the place among the shown cards
* instead puts the card partway up the column, and it jumps to the end when the write lands.
*/
it("sends a card to the end of the column, not to the end of what the filter shows", async () => {
// "To Do" holds three cards and shows one, so the two counts are far enough apart to tell
// which of them the move used.
await setup({ matched: [ "filtered3", "filtered4" ] });
expect(cardTitles(0)).toEqual([ "Third" ]);
expect(cardTitles(1)).toEqual([ "Fourth" ]);
const carried = container.querySelectorAll<HTMLElement>(".board-column")[1]
?.querySelector<HTMLElement>(".board-note");
if (!carried) throw new Error("expected a card in the second column");
carried.focus();
await act(async () => {
carried.dispatchEvent(new KeyboardEvent("keydown", {
key: "ArrowLeft", ctrlKey: true, bubbles: true, cancelable: true
}));
await flush();
});
// Drawn behind the card already shown, which is where the end of the column is.
expect(cardTitles(0)).toEqual([ "Third", "Fourth" ]);
});
it("shows only the matched cards, in their own order, keeping every column", async () => {
// The matches arrive in score order; the board must keep branch order regardless.
const note = await setup({ matched: [ "filtered3", "filtered1" ] });
expect(searchInSubtree).toHaveBeenCalledWith("#urgent", note.noteId, false);
expect(cardTitles(0)).toEqual([ "First", "Third" ]);
expect(cardTitles(1)).toEqual([]);
expect(container.querySelectorAll(".board-column")).toHaveLength(2);
});
it("counts the visible cards in the badge while the limit reads the real column", async () => {
await setup({ matched: [ "filtered1" ], limit: 2 });
const column = container.querySelector(".board-column");
// One of three cards is shown, and three stand against the limit of two.
expect(column?.querySelector(".counter-badge")?.textContent).toBe("1/2");
expect(column?.classList.contains("over-limit")).toBe(true);
});
/**
* The cards drawn are derived from what the filter matches rather than held beside it, so a
* re-run's answer cannot be left unapplied by whatever else the board is doing at the time.
*/
it("draws what a re-run matches", async () => {
const note = await setup({ matched: [ "filtered1" ] });
expect(cardTitles(0)).toEqual([ "First" ]);
searchInSubtree.mockResolvedValue({
searchResultNoteIds: [ "filtered2", "filtered3" ],
highlightedTokens: [],
error: null
});
// A change under the board re-runs the filter, after the wait that collects a run of them.
vi.useFakeTimers();
await act(async () => {
await host.handleEvent("entitiesReloaded", {
loadResults: branchChange(note.noteId, "filtered2")
});
await vi.advanceTimersByTimeAsync(400);
});
expect(cardTitles(0)).toEqual([ "Second", "Third" ]);
});
/**
* A board opened onto a stored query draws nothing until the query has said what it matches,
* rather than drawing every card and then taking most of them away.
*/
it("draws no cards until the stored query has resolved", async () => {
let resolveSearch: (response: unknown) => void = () => {};
searchInSubtree.mockReset();
searchInSubtree.mockReturnValue(
new Promise(resolve => { resolveSearch = resolve; }) as never);
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
children: [
{ id: "held1", title: "First", "#status": "To Do" },
{ id: "held2", title: "Second", "#status": "To Do" }
]
});
host = new Component();
container = document.body.appendChild(document.createElement("div"));
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{
columns: [ { value: "To Do" } ],
filterQuery: "#urgent"
}}
/>
</ParentComponent.Provider>,
container
);
});
await act(async () => { await flush(); });
await act(async () => { await flush(); });
expect(container.querySelectorAll(".board-column")).toHaveLength(0);
expect(container.querySelectorAll(".board-note")).toHaveLength(0);
await act(async () => {
resolveSearch({
searchResultNoteIds: [ "held2" ],
highlightedTokens: [],
error: null
});
await flush();
});
await act(async () => { await flush(); });
expect(cardTitles(0)).toEqual([ "Second" ]);
});
it("marks the matched tokens in the card titles", async () => {
await setup({
matched: [ "filtered1" ],
tokens: [ { token: "First", type: "plain" } ]
});
const titles = [ ...container.querySelectorAll(".board-note .title") ];
expect(titles.flatMap((title) => highlightedTexts("search-match", title))).toEqual([ "First" ]);
});
/**
* The stored flags are set aside while the filter is on: a column without a match is drawn as
* a strip whatever is stored for it, one with a match is drawn open, and what the reader opens
* meanwhile is held by the board rather than written. Clearing the filter brings the stored
* state back, the hand-opened column included.
*/
it("collapses the columns the filter leaves empty, without writing, until it is cleared",
async () => {
await setup({ matched: [ "filtered1" ], columns: [
{ value: "To Do", collapsed: true, keepCollapsed: true },
{ value: "Done" }
] });
const isCollapsed = (index: number) =>
container.querySelectorAll(".board-column")[index].classList.contains("collapsed");
expect(isCollapsed(0)).toBe(false);
expect(cardTitles(0)).toEqual([ "First" ]);
expect(isCollapsed(1)).toBe(true);
// A click on the strip opens the column, and keeps it open once focus moves on: the
// stored `keepCollapsed` has no say either, so the kept column is not closed by it.
const done = container.querySelectorAll<HTMLElement>(".board-column")[1];
await act(async () => {
done.dispatchEvent(new Event("pointerdown", { bubbles: true }));
document.dispatchEvent(new Event("pointerup", { bubbles: true }));
done.dispatchEvent(new Event("click", { bubbles: true }));
});
expect(isCollapsed(1)).toBe(false);
const todo = container.querySelectorAll<HTMLElement>(".board-column")[0];
await act(async () => {
todo.dispatchEvent(new Event("focusin", { bubbles: true }));
});
expect(isCollapsed(0)).toBe(false);
expect(isCollapsed(1)).toBe(false);
expect(saved).toEqual([]);
await clearFilter();
// The one the reader opened stays open through the clear: it is stored open, and the
// override is dropped with the results it was made against, not with the query.
expect(isCollapsed(1)).toBe(false);
await nextFrames(3);
expect(isCollapsed(0)).toBe(true);
expect(isCollapsed(1)).toBe(false);
expect(cardTitles(1)).toEqual([ "Fourth" ]);
// Clearing the filter is the only write, and it leaves the columns as they were stored.
expect(saved.at(-1)?.columns).toEqual([
{ value: "To Do", collapsed: true, keepCollapsed: true },
{ value: "Done" }
]);
});
/**
* The cards come back first, and the columns keep their widths for two frames so the width
* transitions do not run over the card redraw; only then is the stored state drawn. A strip
* about to open draws its cards meanwhile, so the open finds them laid out.
*/
it("holds the columns' widths for two frames after the filter is cleared", async () => {
await setup({ matched: [ "filtered1" ], columns: [
{ value: "To Do", collapsed: true },
{ value: "Done" }
] });
const columnAt = (index: number) => container.querySelectorAll(".board-column")[index];
expect(columnAt(0).classList.contains("collapsed")).toBe(false);
expect(columnAt(1).classList.contains("collapsed")).toBe(true);
await clearFilter();
expect(cardTitles(0)).toEqual([ "First", "Second", "Third" ]);
expect(columnAt(0).classList.contains("collapsed")).toBe(false);
expect(columnAt(1).classList.contains("collapsed")).toBe(true);
expect(columnAt(1).classList.contains("pre-expanding")).toBe(true);
expect(columnAt(1).querySelectorAll(".board-note")).toHaveLength(1);
await nextFrames(3);
expect(columnAt(0).classList.contains("collapsed")).toBe(true);
// A collapse the filter asked for runs at the quick pace, like one asked for by hand.
expect(columnAt(0).classList.contains("quick-collapse")).toBe(true);
expect(columnAt(1).classList.contains("collapsed")).toBe(false);
expect(columnAt(1).classList.contains("pre-expanding")).toBe(false);
expect(cardTitles(1)).toEqual([ "Fourth" ]);
});
});
describe("a column that sorts its cards", () => {
let container: HTMLElement | undefined;
beforeEach(() => {
vi.spyOn(server, "post").mockImplementation(async (url) =>
(url === "notes/metadata" ? {} : undefined));
});
afterEach(() => {
vi.restoreAllMocks();
if (container) {
render(null, container);
container.remove();
container = undefined;
}
});
it("draws the sorted column in its own order and the others in branch order", async () => {
const { board } = await renderSortedBoard({ orderBy: "title" });
expect(cardTitlesIn(board, 0)).toEqual([ "Alpha", "Beta", "Delta" ]);
expect(cardTitlesIn(board, 1)).toEqual([ "Zulu", "Yankee" ]);
});
it("draws it backwards when the column asks for that", async () => {
const { board } = await renderSortedBoard({ orderBy: "title", descendingOrder: true });
expect(cardTitlesIn(board, 0)).toEqual([ "Delta", "Beta", "Alpha" ]);
});
it("leaves every column in branch order while none of them sorts", async () => {
const { board } = await renderSortedBoard({});
expect(cardTitlesIn(board, 0)).toEqual([ "Delta", "Beta", "Alpha" ]);
});
it("asks for no creation dates at all while no column sorts", async () => {
await renderSortedBoard({});
expect(server.post).not.toHaveBeenCalledWith("notes/metadata", expect.anything());
});
it("draws the column again when a title it sorts by changes", async () => {
const { board, host, cards } = await renderSortedBoard({ orderBy: "title" });
cards.beta.title = "Omega";
await act(async () => {
await host.handleEvent("entitiesReloaded",
{ loadResults: noteRenamed(cards.beta.noteId, "Omega") });
await flush();
});
expect(cardTitlesIn(board, 0)).toEqual([ "Alpha", "Delta", "Omega" ]);
});
it("sorts by a promoted attribute, keeping the cards that have no value last", async () => {
const { board } = await renderSortedBoard({ orderBy: "attr:priority" }, {
"#label:priority(inheritable)": "promoted,single,number",
values: { Alpha: "3", Beta: "1" }
});
expect(cardTitlesIn(board, 0)).toEqual([ "Beta", "Alpha", "Delta" ]);
});
it("sorts a select by the order its own definition offers the options in", async () => {
const { board } = await renderSortedBoard({ orderBy: "attr:priority" }, {
"#label:priority(inheritable)":
"promoted,alias=Priority,single,select,options=Low;Medium;High;Urgent",
values: { Delta: "Urgent", Beta: "Low", Alpha: "High" }
});
// Alphabetically this would read High, Low, Urgent.
expect(cardTitlesIn(board, 0)).toEqual([ "Beta", "Alpha", "Delta" ]);
});
it("reorders the column as soon as the sort is picked from its menu", async () => {
const { board } = await renderSortedBoard({});
expect(cardTitlesIn(board, 0)).toEqual([ "Delta", "Beta", "Alpha" ]);
await pickSort(board, "bx bx-text");
expect(cardTitlesIn(board, 0)).toEqual([ "Alpha", "Beta", "Delta" ]);
await pickSort(board, "bx bx-sort-down");
expect(cardTitlesIn(board, 0)).toEqual([ "Delta", "Beta", "Alpha" ]);
await pickSort(board, "bx bx-move-vertical");
expect(saved.at(-1)?.columns?.[0])
.toStrictEqual({ value: "To Do", orderBy: "manual", descendingOrder: true });
expect(cardTitlesIn(board, 0)).toEqual([ "Delta", "Beta", "Alpha" ]);
});
it("shows no sort button while the column is arranged by hand", async () => {
const { board } = await renderSortedBoard({});
expect(board.querySelector(".board-column h3 .column-sort")).toBeNull();
});
it("shows the way the order runs, and turns the arrow over for a descending one", async () => {
const { board } = await renderSortedBoard({ orderBy: "title" });
// `ActionButton` puts the icon on the button itself.
expect(sortButton(board)?.classList.contains("bx-sort-up")).toBe(true);
// Only the sorted column carries one.
expect(board.querySelectorAll(".board-column h3 .column-sort")).toHaveLength(1);
const descending = await renderSortedBoard({ orderBy: "title", descendingOrder: true });
expect(sortButton(descending.board)?.classList.contains("bx-sort-down")).toBe(true);
});
it("draws a column stored as default in the board's own order, button and all", async () => {
const { board } = await renderSortedBoard(
{ orderBy: "default" }, undefined, { orderBy: "title" });
expect(cardTitlesIn(board, 0)).toEqual([ "Alpha", "Beta", "Delta" ]);
// The button says the column is sorted, whichever order it took.
expect(sortButton(board)?.classList.contains("bx-sort-up")).toBe(true);
const backwards = await renderSortedBoard(
{ orderBy: "default" }, undefined, { orderBy: "title", isDescending: true });
expect(cardTitlesIn(backwards.board, 0)).toEqual([ "Delta", "Beta", "Alpha" ]);
expect(sortButton(backwards.board)?.classList.contains("bx-sort-down")).toBe(true);
});
/** The board holding no order of its own leaves such a column as the reader arranged it. */
it("leaves a column stored as default alone while the board holds no order", async () => {
const { board } = await renderSortedBoard({ orderBy: "default" });
expect(cardTitlesIn(board, 0)).toEqual([ "Delta", "Beta", "Alpha" ]);
expect(sortButton(board)).toBeNull();
});
it("opens the sort menu, and takes the column away once it is arranged by hand", async () => {
const { board } = await renderSortedBoard({ orderBy: "title" });
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
sortButton(board)?.click();
const items = show.mock.calls.at(-1)?.[0].items ?? [];
expect(items.map(item => (item && "uiIcon" in item ? item.uiIcon : "separator")))
.toEqual([
"bx bx-collection", "bx bx-move-vertical", "bx bx-text", "bx bx-calendar-plus",
"separator", "bx bx-sort-up", "bx bx-sort-down"
]);
show.mockRestore();
// Picking the manual order is what takes the button away.
await pickSort(board, "bx bx-move-vertical");
expect(board.querySelector(".board-column h3 .column-sort")).toBeNull();
});
/** A keyboard press carries no pointer position, which would put the menu at the origin. */
it("opens the menu against the button when a keyboard presses it", async () => {
const { board } = await renderSortedBoard({ orderBy: "title" });
const button = sortButton(board);
if (!button) throw new Error("expected a sort button");
button.getBoundingClientRect = () =>
({ right: 320, bottom: 48 }) as DOMRect;
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
// `detail` is 0 for the click a keyboard synthesises, and the coordinates are 0 with it.
button.dispatchEvent(new MouseEvent("click", { bubbles: true, detail: 0 }));
expect(show.mock.calls.at(-1)?.[0]).toMatchObject({ x: 320, y: 48 });
show.mockRestore();
});
/** A strip is too narrow to work in, and the menu would stand where it is about to widen. */
it("shows the button on a strip, with nothing to be done to it", async () => {
const { board } = await renderSortedBoard({ orderBy: "title", collapsed: true });
expect(board.querySelector(".board-column.collapsed")).toBeTruthy();
expect(sortButton(board)?.hasAttribute("disabled")).toBe(true);
});
/** The button the first column shows, which is the only sorted one in these boards. */
function sortButton(board: HTMLElement) {
return board.querySelector<HTMLElement>(".board-column h3 .column-sort");
}
/** Opens the first column's menu and picks the sort entry carrying the given icon. */
async function pickSort(board: HTMLElement, icon: string) {
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
board.querySelector(".board-column h3")
?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
const sort = (show.mock.calls.at(-1)?.[0].items ?? []).find(item =>
item && "uiIcon" in item && item.uiIcon === "bx bx-sort-alt-2");
if (!sort || !("items" in sort)) throw new Error("expected a sort entry");
const entry = (sort.items ?? []).find(item =>
item && "uiIcon" in item && item.uiIcon === icon);
if (!entry || !("handler" in entry)) throw new Error(`expected a ${icon} entry`);
await act(async () => {
entry.handler?.(entry, {} as never);
await flush();
});
show.mockRestore();
}
/** The note row a rename produces, which is what the card reads its new title from. */
function noteRenamed(noteId: string, title: string) {
const results = new LoadResults([ {
entityName: "notes",
entityId: noteId,
entity: { noteId, title }
} as never ]);
results.addNote(noteId, "other");
return results;
}
function cardTitlesIn(board: HTMLElement, column: number) {
const columns = [ ...board.querySelectorAll(".board-column") ];
return [ ...columns[column].querySelectorAll(".board-note .title") ]
.map(el => el.textContent);
}
async function renderSortedBoard(
sort: { orderBy?: string, descendingOrder?: boolean, collapsed?: boolean },
promoted?: { "#label:priority(inheritable)": string, values: Record<string, string> },
/** The order the board holds, which a column stored as `default` takes. */
boardSort?: { orderBy: string, isDescending?: boolean }
) {
const priorities = promoted?.values ?? {};
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
...(boardSort
? {
"#board:sortColumns": boardSort.orderBy,
...(boardSort.isDescending ? { "#board:sortColumnsDescending": "" } : {})
}
: {}),
...(promoted
? { "#label:priority(inheritable)": promoted["#label:priority(inheritable)"] }
: {}),
children: [
{ title: "Delta", "#status": "To Do", ...priorityOf("Delta") },
{ title: "Beta", "#status": "To Do", ...priorityOf("Beta") },
{ title: "Alpha", "#status": "To Do", ...priorityOf("Alpha") },
{ title: "Zulu", "#status": "Done" },
{ title: "Yankee", "#status": "Done" }
]
});
function priorityOf(title: string): Record<string, string> {
return priorities[title] ? { "#priority": priorities[title] } : {};
}
const host = new Component();
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{
columns: [ { value: "To Do", ...sort }, { value: "Done" } ]
}}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
await act(async () => { await flush(); });
const byTitle = Object.fromEntries([ ...note.getChildNoteIds() ]
.map(noteId => froca.notes[noteId])
.map(child => [ child.title.toLowerCase(), child ]));
return { board: mountPoint, host, cards: byTitle };
}
});
describe("Switchable board grouping", () => {
let container: HTMLElement | undefined;
afterEach(() => {
saved.length = 0;
if (container) {
render(null, container);
container.remove();
container = undefined;
}
});
/** Columns stored for the default grouping and for `priority`, each with its own icons. */
const CONFIG: BoardViewData = {
columns: [ { value: "To Do" }, { value: "Done", icon: "bx bx-check" } ],
priorityViewColumns: [ { value: "High", icon: "bx bx-up-arrow" }, { value: "Low" } ]
};
async function setup(config: BoardViewData = CONFIG) {
// `buildNote` appends to whatever the cache already holds for the id, so the previous
// test's definitions, and the grouping it switched to, would still be on the note.
delete noteAttributeCache.attributes["switchBoard"];
const note = buildNote({
id: "switchBoard",
title: "Board",
// The header the dropdown stands in is drawn for a collection note alone.
type: "book",
"#collection": "",
"#viewType": "board",
"#board:groupBy": "status",
"#label:status(inheritable)": "promoted,alias=Status,single,select,options=To Do;Done",
"#label:priority(inheritable)": "promoted,alias=Priority,single,select,options=High;Low",
children: [
{ title: "First", "#status": "To Do", "#priority": "High" },
{ title: "Second", "#status": "Done", "#priority": "Low" }
]
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
const host = new Component();
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={config}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
await act(async () => { await flush(); });
return { note, container: mountPoint, host };
}
/** Writes `#board:groupBy`, the way the dropdown does, and lets the board catch up. */
async function groupBy(note: ReturnType<typeof buildNote>, host: Component, value: string) {
const attribute = note.getAttributes().find(attr => attr.name === "board:groupBy");
if (!attribute) throw new Error("expected the board to carry #board:groupBy");
attribute.value = value;
const results = new LoadResults([ {
entityName: "attributes",
entityId: "groupByAttr",
entity: {
attributeId: "groupByAttr",
noteId: "switchBoard",
type: "label",
name: "board:groupBy",
value
}
} as never ]);
results.addAttribute("groupByAttr", "other");
await act(async () => {
await host.handleEvent("entitiesReloaded", { loadResults: results });
await flush();
});
// The switch is committed on one pass and stored on the next.
await act(async () => { await flush(); });
await act(async () => { await flush(); });
}
function cardTitles(board: HTMLElement, column: number) {
const columns = [ ...board.querySelectorAll(".board-column") ];
return [ ...columns[column].querySelectorAll(".board-note .title") ]
.map(title => title.textContent);
}
it("names the grouping in force and re-groups the cards when it changes", async () => {
const { note, container, host } = await setup();
expect(container.querySelector(".board-group-by button")?.textContent).toContain("Status");
expect(columnTitles(container)).toEqual([ "To Do", "Done" ]);
await groupBy(note, host, "priority");
expect(container.querySelector(".board-group-by button")?.textContent)
.toContain("Priority");
expect(columnTitles(container)).toEqual([ "High", "Low" ]);
// Drawn from the new grouping's own entries, not from the ones it replaced.
expect(columnIcons(container)).toEqual([ "bx bx-up-arrow", DEFAULT_COLUMN_ICON ]);
expect(cardTitles(container, 0)).toEqual([ "First" ]);
});
/**
* The board reads the new grouping before it is pointed at it. A write made from that read
* would put the columns it is leaving under the key of the one it is arriving at.
*/
it("stores the new grouping's columns under its own key and nowhere else", async () => {
// Nothing stored for `priority`, so the switch is what gives it a column list.
const { note, host } = await setup({ columns: CONFIG.columns });
saved.length = 0;
await groupBy(note, host, "priority");
expect(saved.length).toBeGreaterThan(0);
for (const config of saved) {
expect(config.priorityViewColumns?.map(col => col.value)).toEqual([ "High", "Low" ]);
expect(config.columns).toEqual(CONFIG.columns);
}
});
it("leaves the grouping it left exactly as it was", async () => {
const { note, container, host } = await setup();
await groupBy(note, host, "priority");
await groupBy(note, host, "status");
expect(columnTitles(container)).toEqual([ "To Do", "Done" ]);
expect(columnIcons(container)).toEqual([ DEFAULT_COLUMN_ICON, "bx bx-check" ]);
expect(saved.at(-1)?.columns ?? CONFIG.columns).toEqual(CONFIG.columns);
});
/**
* Before the grouping could be switched, every board stored its columns under `columns`. Read
* as the default grouping's, they would follow the board onto whatever it is switched to next.
*/
it("moves a pre-switching column list under the grouping it belongs to", async () => {
const note = buildNote({
id: "legacyBoard",
title: "Board",
"#collection": "",
"#viewType": "board",
"#board:groupBy": "priority",
children: [
{ title: "First", "#priority": "High" },
{ title: "Second", "#priority": "Low" }
]
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{
columns: [
{ value: "Low", icon: "bx bx-down-arrow" }, { value: "High" }
]
}}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
await act(async () => { await flush(); });
expect(saved.at(-1)?.priorityViewColumns?.map(col => col.value)).toEqual([ "Low", "High" ]);
expect(saved.at(-1)).not.toHaveProperty("columns");
expect(columnTitles(mountPoint)).toEqual([ "Low", "High" ]);
expect(columnIcons(mountPoint)).toEqual([ "bx bx-down-arrow", DEFAULT_COLUMN_ICON ]);
});
/**
* `adoptLegacyColumns()` reads a config with `columns` and no `*ViewColumns` key as one written
* before the grouping could be switched. Deleting the last keyed list must not leave that shape.
*/
it("keeps the default grouping's columns off a deleted grouping it was opened on", async () => {
delete noteAttributeCache.attributes["deleteBoard"];
const note = buildNote({
id: "deleteBoard",
title: "Board",
"#collection": "",
"#viewType": "board",
"#board:groupBy": "priority",
"#label:priority(inheritable)": "promoted,alias=Priority,single,select,options=High;Low",
children: [
{ title: "First", "#status": "To Do", "#priority": "High" },
{ title: "Second", "#status": "Done", "#priority": "Low" }
]
});
const setLabel = vi.spyOn(attributes, "setLabel").mockResolvedValue(undefined);
// Every refresh asks the api this, which is where the test takes hold of it.
let api: BoardApi | undefined;
const hasStoredSince = BoardApi.prototype.hasStoredSince;
vi.spyOn(BoardApi.prototype, "hasStoredSince").mockImplementation(
function (this: BoardApi, config) {
api = this;
return hasStoredSince.call(this, config);
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
const host = new Component();
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={CONFIG}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
await act(async () => { await flush(); });
expect(columnTitles(mountPoint)).toEqual([ "High", "Low" ]);
setLabel.mockClear();
// What `PromotedAttributesCard` deletes: the definition and every card's value. The
// deletion reaches froca once `ws.waitForMaxKnownEntityChangeId()` is awaited, or only
// after the request has returned where nothing waits for it.
const deleted = Object.values(froca.attributes).filter(attribute =>
attribute.name === "label:priority" || attribute.name === "priority");
let deletionArrived = false;
async function applyDeletion() {
if (deletionArrived) {
return;
}
deletionArrived = true;
for (const attribute of deleted) {
delete froca.attributes[attribute.attributeId];
for (const owner of [ note, ...note.getChildNoteIds().map(id => froca.notes[id]) ]) {
owner.attributes = owner.attributes
.filter(attributeId => attributeId !== attribute.attributeId);
noteAttributeCache.attributes[owner.noteId] = noteAttributeCache
.attributes[owner.noteId]?.filter(cached => cached !== attribute) ?? [];
}
}
const results = new LoadResults(deleted.map(attribute => ({
entityName: "attributes",
entityId: attribute.attributeId,
entity: {
attributeId: attribute.attributeId, noteId: attribute.noteId, type: "label",
name: attribute.name, isDeleted: true
}
}) as never));
for (const attribute of deleted) {
results.addAttribute(attribute.attributeId, "other");
}
await host.handleEvent("entitiesReloaded", { loadResults: results });
}
vi.spyOn(ws, "waitForMaxKnownEntityChangeId").mockImplementation(applyDeletion);
await act(async () => {
await api?.deleteGrouping("priority", async () => {});
await flush();
});
await act(async () => {
await applyDeletion();
await flush();
});
await act(async () => { await flush(); });
await act(async () => { await flush(); });
expect(api).toBeDefined();
expect(saved.at(-1)?.priorityViewColumns ?? []).toEqual([]);
expect(saved.at(-1)?.columns).toEqual(CONFIG.columns);
expect(setLabel).not.toHaveBeenCalledWith(
"deleteBoard", "label:priority", expect.anything(), expect.anything());
vi.restoreAllMocks();
});
});
describe("Board properties from the note menu", () => {
let container: HTMLElement | undefined;
afterEach(() => {
if (container) {
render(null, container);
container.remove();
container = undefined;
}
// A modal Bootstrap still believes is shown traps the focus of every later test, and its
// teardown waits on a transition happy-dom never runs. Every one of them: earlier tests
// leave dialogs of their own behind, and the shown one is the last.
for (const modal of document.querySelectorAll<HTMLElement>(".board-properties-dialog")) {
BootstrapModal.getInstance(modal)?.dispose();
modal.remove();
}
document.querySelector(".modal-backdrop")?.remove();
document.body.classList.remove("modal-open");
});
/**
* The menu is drawn outside the board, so it asks for the dialog by event. Each open board
* hears it, and only the one in the tab the menu was opened from answers.
*/
it("opens the dialog for its own tab, and not for another one", async () => {
const host = await renderBoardInContext("ntx-1");
const isOpen = () => !!document.querySelector(".board-properties-dialog .modal-dialog");
expect(isOpen()).toBe(false);
await act(async () => {
await host.handleEvent("showCollectionProperties", { ntxId: "ntx-2" });
await flush();
});
expect(isOpen()).toBe(false);
await act(async () => {
await host.handleEvent("showCollectionProperties", { ntxId: "ntx-1" });
await flush();
});
expect(isOpen()).toBe(true);
});
/**
* The collection settings menu opens the same dialog. Its entry comes after the view options,
* separated from them by a divider.
*/
it("opens the dialog from the collection settings menu, below a divider", async () => {
await renderBoardInContext("ntx-1");
const cog = container?.querySelector<HTMLElement>(".collection-properties button.bx-cog");
const options = cog?.closest(".dropdown");
if (!options) throw new Error("expected a settings menu on the collection bar");
await act(async () => {
$(options as HTMLElement).children("button:not([aria-expanded=true])").trigger("click");
await flush();
});
const entry = [ ...document.querySelectorAll<HTMLElement>(".tn-popup .dropdown-item") ].at(-1);
if (!entry) throw new Error("expected an entry in the settings menu");
expect(entry.textContent).toContain("board_view.properties");
expect(entry.previousElementSibling?.className).toContain("dropdown-divider");
await act(async () => {
entry.click();
await flush();
});
expect(document.querySelector(".board-properties-dialog .modal-dialog")).toBeTruthy();
});
/** Mounts a board belonging to the given tab, returning what events reach it through. */
async function renderBoardInContext(ntxId: string) {
const note = buildNote({
title: "Board",
type: "book",
"#collection": "",
"#viewType": "board",
children: [
{ title: "First", "#status": "To Do" },
{ title: "Second", "#status": "Done" }
]
});
const host = new Component();
Object.assign(host, { noteContext: contextStub({ ntxId, isActive: () => true }) });
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" }, { value: "Done" } ] }}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
return host;
}
});
describe("a column windowed for its size", () => {
let container: HTMLElement | undefined;
beforeEach(() => {
saved.length = 0;
vi.restoreAllMocks();
vi.spyOn(server, "put").mockResolvedValue(undefined);
});
afterEach(() => {
if (container) {
render(null, container);
container.remove();
container = undefined;
}
});
it("draws a slice of a big column and stands spacers for the rest", async () => {
const board = await renderSized(200, 3);
const columns = board.querySelectorAll<HTMLElement>(".board-column");
const big = columns[0];
const small = columns[1];
if (!big || !small) throw new Error("expected two columns");
const drawn = big.querySelectorAll(".board-note");
expect(drawn.length).toBeGreaterThan(0);
expect(drawn.length).toBeLessThan(200);
expect(big.classList.contains("windowed")).toBe(true);
const area = big.querySelector<HTMLElement>(".board-column-content");
expect(area?.dataset.windowCount).toBe("200");
expect(area?.dataset.windowFrom).toBe("0");
// The cards it is not drawing are held by the spacer below them.
const spacers = big.querySelectorAll<HTMLElement>(".board-window-spacer");
expect(spacers).toHaveLength(2);
expect(spacers[0].style.height).toBe("0px");
expect(Number.parseFloat(spacers[1].style.height)).toBeGreaterThan(0);
// A column that fits keeps the path it has always had.
expect(small.classList.contains("windowed")).toBe(false);
expect(small.querySelectorAll(".board-note")).toHaveLength(3);
expect(small.querySelector<HTMLElement>(".board-column-content")?.dataset.windowCount)
.toBeUndefined();
});
/**
* The drag places a card at a column's foot by this count. It has to stand on the column
* itself: a collapsed column draws no card area to hang it off, and a windowed one draws fewer
* cards than it holds.
*/
it("says on each column how many cards it holds, drawn or not", async () => {
const board = await renderSized(200, 3);
const columns = board.querySelectorAll<HTMLElement>(".board-column");
expect(columns[0].dataset.count).toBe("200");
expect(columns[0].querySelectorAll(".board-note").length).toBeLessThan(200);
expect(columns[1].dataset.count).toBe("3");
});
/**
* The drag and the keyboard both name a card by the place it holds in its column. Counting
* drawn elements would name a place among whatever is on screen, which moves as it scrolls.
*/
it("gives each card the place it holds in the column, not among the ones drawn", async () => {
const board = await renderSized(200, 3);
const drawn = [ ...board.querySelectorAll<HTMLElement>(".board-column-content .board-note") ];
const big = drawn.filter((card) => card.closest(".board-column")
=== board.querySelector(".board-column"));
expect(big.map((card) => card.dataset.index))
.toEqual(big.map((_, index) => String(index)));
});
async function renderSized(big: number, small: number) {
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
children: [
...Array.from({ length: big }, (_, i) => ({
title: `Big ${i}`, "#status": "To Do"
})),
...Array.from({ length: small }, (_, i) => ({
title: `Small ${i}`, "#status": "Done"
}))
]
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" }, { value: "Done" } ] }}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
return mountPoint;
}
});
describe("how wide the board draws its columns", () => {
let container: HTMLElement | undefined;
afterEach(() => {
saved.length = 0;
if (container) {
render(null, container);
container.remove();
container = undefined;
}
});
/**
* The width itself is CSS, which happy-dom does not resolve. What the board answers for is the
* class it carries, off which the stylesheet picks one of the three widths.
*/
it("carries the width its label names, and no class where it names none", async () => {
expect((await draw("wide")).className).toContain("board-wide-columns");
expect((await draw("narrow")).className).toContain("board-narrow-columns");
// Without a class the width is whatever is inherited, which is the stylesheet's own
// default or a theme's if one sets it.
expect((await draw(undefined)).className).not.toMatch(/board-\w+-columns/);
expect((await draw("enormous")).className).not.toMatch(/board-\w+-columns/);
});
async function draw(width: string | undefined) {
if (container) {
render(null, container);
container.remove();
}
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
...(width ? { "#board:columnWidth": width } : {}),
children: [ { title: "First", "#status": "To Do" } ]
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" } ] }}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
const board = mountPoint.querySelector<HTMLElement>(".board-view");
if (!board) throw new Error("expected the board to be drawn");
return board;
}
});
describe("what the board hands the right pane", () => {
let container: HTMLElement | undefined;
afterEach(() => {
saved.length = 0;
if (container) {
render(null, container);
container.remove();
container = undefined;
}
});
/**
* The pane lists the columns of whichever board is on show, so the board publishes them into
* the context it belongs to rather than the pane reading the board's own state.
*/
it("publishes its columns, scrolls to one on request, and takes them back", async () => {
const published: { key: string, value: unknown }[] = [];
const cleared: string[] = [];
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
children: [
{ title: "First", "#status": "To Do" },
{ title: "Second", "#status": "Done" },
{ title: "Third", "#status": "Done" }
]
});
const host = new Component();
Object.assign(host, { noteContext: {
setContextData: (key: string, value: unknown) => published.push({ key, value }),
clearContextData: (key: string) => cleared.push(key)
} });
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" }, { value: "Done" } ] }}
/>
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
const outline = published.at(-1);
expect(outline?.key).toBe("boardColumns");
const { columns, scrollToColumn } = outline?.value as {
columns: { value: string, title: string, count: number }[],
scrollToColumn: (column: string) => void
};
expect(columns.map(({ title, count }) => `${title}:${count}`))
.toEqual([ "To Do:1", "Done:2" ]);
// What a press on one of the pane's entries does. Both are recorded rather than watched
// for: happy-dom scrolls nothing, and a dialog another spec left standing holds the focus.
const scrolled: (string | undefined)[] = [];
const focused: (string | undefined)[] = [];
for (const element of mountPoint.querySelectorAll<HTMLElement>(".board-column")) {
element.scrollIntoView = () => scrolled.push(element.dataset.column);
const heading = element.querySelector<HTMLElement>("h3");
if (heading) {
heading.focus = () => focused.push(element.dataset.column);
}
}
scrollToColumn("Done");
expect(scrolled).toEqual([ "Done" ]);
// The heading takes the focus with it, so the board's own keys carry on from there.
expect(focused).toEqual([ "Done" ]);
// A board that is no longer on show leaves nothing behind for the pane to list.
act(() => { render(null, mountPoint); });
expect(cleared).toContain("boardColumns");
});
});
/**
* Disposes every modal an earlier test left shown. Such a dialog can be mounted only after that
* test's teardown, and a modal Bootstrap believes shown pulls the focus back into itself from
* anything focused later. The mobile rails follow the focus, so the page is cleared of them first.
*/
function disposeShownModals() {
for (const modal of document.querySelectorAll<HTMLElement>(".modal.show")) {
BootstrapModal.getInstance(modal)?.dispose();
modal.remove();
}
document.querySelector(".modal-backdrop")?.remove();
document.body.classList.remove("modal-open");
}
/**
* Runs a step with a tab manager standing in for the app's, which the card's menu and the reveal
* after a drop ask for the active pane. Only for that step: the board's own hooks read the manager
* while it renders, and answer differently to a stand-in than to none.
*/
async function withTabManager(step: () => Promise<void>) {
const previous = appContext.tabManager;
appContext.tabManager = {
getActiveContext: () => undefined,
// The menu on mobile asks whether the pane is split, which one with no splits is not.
getNoteContextById: () => ({ getMainContext: () => ({ getSubContexts: () => [] }) })
} as never;
try {
await step();
} finally {
appContext.tabManager = previous;
}
}
describe("Card toolbar on mobile", () => {
let container: HTMLElement;
let host: Component;
/** Stands in for the observer the rail watches its card with, and reports on request. */
class MockIntersectionObserver {
static instances: MockIntersectionObserver[] = [];
observe = vi.fn();
disconnect = vi.fn();
constructor(readonly callback: IntersectionObserverCallback) {
MockIntersectionObserver.instances.push(this);
}
report(isIntersecting: boolean) {
this.callback(
[ { isIntersecting } as IntersectionObserverEntry ],
this as unknown as IntersectionObserver);
}
}
let realIntersectionObserver: typeof IntersectionObserver;
beforeEach(() => {
layout.onMobile = true;
MockIntersectionObserver.instances = [];
realIntersectionObserver = window.IntersectionObserver;
window.IntersectionObserver =
MockIntersectionObserver as unknown as typeof IntersectionObserver;
});
afterEach(() => {
window.IntersectionObserver = realIntersectionObserver;
layout.onMobile = false;
vi.useRealTimers();
saved.length = 0;
render(null, container);
container.remove();
});
/** A board of two columns, three cards, nothing sorted, and an inbox only when asked for. */
async function setup({ withInbox = false } = {}) {
disposeShownModals();
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
...(withInbox ? { "#board:showInbox": "true" } : {}),
children: [
{ id: "tool1", title: "First", "#status": "To Do" },
{ id: "tool2", title: "Second", "#status": "To Do" },
{ id: "tool3", title: "Third", "#status": "Done" }
]
});
host = new Component();
container = document.body.appendChild(document.createElement("div"));
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" }, { value: "Done" } ] }}
/>
</ParentComponent.Provider>,
container
);
});
await act(async () => { await flush(); });
return note;
}
function card(noteId: string) {
const element = container.querySelector<HTMLElement>(
`.board-note[data-note-id="${noteId}"]`);
if (!element) throw new Error(`expected the card ${noteId}`);
return element;
}
const toolbar = () => container.querySelector<HTMLElement>(".board-card-toolbar");
/** The buttons on the rail, each by the icon it wears. */
const buttons = () => [ ...toolbar()?.querySelectorAll("button") ?? [] ]
.map(button => [ ...button.classList ].find(name => name.startsWith("bx-")));
function button(icon: string) {
const element = toolbar()?.querySelector<HTMLElement>(`button.${icon}`);
if (!element) throw new Error(`expected a ${icon} button`);
return element;
}
async function focus(noteId: string) {
await act(async () => { card(noteId).focus(); });
}
/** Lets a dismissed rail finish sliding off, after which it is gone. Needs fake timers. */
async function letRailLeave() {
await act(async () => { vi.advanceTimersByTime(RAIL_EXIT_MS); });
}
it("follows the focus onto a card and leaves with it, over the board", async () => {
await setup();
expect(toolbar()).toBeNull();
await focus("tool1");
expect(buttons()).toEqual([
"bx-rename", "bx-list-plus", "bx-list-plus", "bx-task-x", "bx-dots-vertical-rounded"
]);
// Above first, then below. The glyph's plus sits at the foot of its list, so the one
// above is told apart by the class that turns it over.
expect(toolbar()?.querySelectorAll("button")[1].classList
.contains("bx-flip-vertical")).toBe(true);
// The rail offers the rename, so the card's own hover-revealed icon is left out.
expect(card("tool1").querySelector(".edit-icon")).toBeNull();
// Portaled onto the board, whose edge the rail is pinned to, rather than into the card.
expect(toolbar()?.parentElement).toBe(container.querySelector(".board-view-container"));
// A press on the rail keeps the focus where it is, or the rail would close under it.
const press = new PointerEvent("pointerdown", { bubbles: true, cancelable: true });
button("bx-rename").dispatchEvent(press);
expect(press.defaultPrevented).toBe(true);
// Dismissed, it slides off before it goes, and takes no press meanwhile.
vi.useFakeTimers();
await act(async () => { card("tool1").blur(); });
expect(toolbar()?.classList.contains("leaving")).toBe(true);
await letRailLeave();
expect(toolbar()).toBeNull();
});
/**
* Focus moving from one card to another hands the rail over: the newcomer stands in place
* without sliding in, and the rail it replaces is dropped rather than left sliding off.
*/
it("hands over between cards without sliding out and in", async () => {
await setup();
await focus("tool1");
expect(toolbar()?.classList.contains("takes-over")).toBe(false);
await focus("tool2");
const rails = container.querySelectorAll(".board-card-toolbar");
expect(rails).toHaveLength(1);
expect(rails[0].classList.contains("takes-over")).toBe(true);
expect(rails[0].classList.contains("leaving")).toBe(false);
// The one standing slides off on its own once nothing takes over from it.
vi.useFakeTimers();
await act(async () => { card("tool2").blur(); });
expect(toolbar()?.classList.contains("leaving")).toBe(true);
await letRailLeave();
expect(toolbar()).toBeNull();
});
/**
* A drag measures the board as it stands, so nothing may scroll it under the finger. The focus
* held for the card last dropped is revealed again whenever the board redraws, and lifting a
* card redraws it: that reveal scrolled the board to the earlier card, and the lifted one
* landed in a neighbouring column.
*/
it("reveals no earlier focus while a card is carried", async () => {
await setup();
vi.useFakeTimers();
const scrolled = vi.fn();
Object.defineProperty(card("tool1"), "scrollIntoView",
{ value: scrolled, configurable: true });
const pressOn = (element: HTMLElement, type: string) => element.dispatchEvent(
new PointerEvent(type, {
bubbles: true, pointerType: "touch", clientX: 10, clientY: 10
}));
const settle = () => act(async () => { vi.advanceTimersByTime(1000); });
// The first drop leaves its focus on the first card, revealed once the board settles.
await act(async () => { pressOn(card("tool1"), "pointerdown"); });
await settle();
await act(async () => { pressOn(card("tool1"), "pointercancel"); });
await settle();
expect(document.activeElement).toBe(card("tool1"));
expect(scrolled).toHaveBeenCalledTimes(1);
// Lifting another card redraws the board. The first card is left where it is, however
// long the second is held.
await act(async () => { pressOn(card("tool2"), "pointerdown"); });
await settle();
const board = container.querySelector(".board-view-container");
expect(board?.classList.contains("board-dragging")).toBe(true);
await settle();
expect(scrolled).toHaveBeenCalledTimes(1);
await act(async () => { pressOn(card("tool2"), "pointercancel"); });
await settle();
});
/**
* The rail stands for the card, so a card scrolled off the screen entirely takes it along,
* and brings it back. Watched only while the rail is wanted: the observer goes with the focus.
*/
it("goes off the screen with its card and comes back with it", async () => {
await setup();
expect(MockIntersectionObserver.instances).toHaveLength(0);
await focus("tool1");
expect(toolbar()).not.toBeNull();
const observer = MockIntersectionObserver.instances.at(-1);
if (!observer) throw new Error("expected the card to be watched");
expect(observer.observe).toHaveBeenCalledWith(card("tool1"));
vi.useFakeTimers();
await act(async () => { observer.report(false); });
await letRailLeave();
expect(toolbar()).toBeNull();
await act(async () => { observer.report(true); });
expect(toolbar()?.classList.contains("leaving")).toBe(false);
await act(async () => { card("tool1").blur(); });
await letRailLeave();
expect(toolbar()).toBeNull();
expect(observer.disconnect).toHaveBeenCalled();
});
it("is left out off mobile, where the card has its menu", async () => {
layout.onMobile = false;
await setup();
await focus("tool1");
expect(toolbar()).toBeNull();
});
it("inserts, renames, removes and opens the menu for the focused card", async () => {
await setup();
const removeFromBoard = vi.spyOn(BoardApi.prototype, "removeFromBoard")
.mockResolvedValue(undefined);
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
// Insert below opens the field under the card, which takes the focus and the rail with it.
await focus("tool1");
vi.useFakeTimers();
await act(async () => {
toolbar()?.querySelectorAll<HTMLElement>("button")[2].click();
});
await letRailLeave();
const column = container.querySelectorAll(".board-column")[0];
const order = [ ...column.querySelectorAll(".board-note, .board-new-item.inserting") ]
.map(element => element.getAttribute("data-note-id") ?? "field");
expect(order).toEqual([ "tool1", "field", "tool2" ]);
expect(toolbar()).toBeNull();
// Rename opens the title editor in the card, and the rail stands aside while it is open.
await focus("tool2");
await act(async () => { button("bx-rename").click(); });
await letRailLeave();
expect(card("tool2").classList.contains("editing")).toBe(true);
expect(toolbar()).toBeNull();
await focus("tool3");
await withTabManager(async () => {
await act(async () => { button("bx-dots-vertical-rounded").click(); });
});
expect(show).toHaveBeenCalledTimes(1);
await act(async () => { button("bx-task-x").click(); });
expect(removeFromBoard).toHaveBeenCalledWith([ "tool3" ]);
removeFromBoard.mockRestore();
show.mockRestore();
});
/**
* A board with an inbox keeps a card whose grouping value is cleared, in the inbox, so what
* takes a card off such a board is deleting the note, as on the card's menu.
*/
it("offers to delete the note instead where the board has an inbox", async () => {
await setup({ withInbox: true });
const deleteNotes = vi.spyOn(branches, "deleteNotes").mockResolvedValue(false);
await focus("tool1");
expect(buttons()).toEqual([
"bx-rename", "bx-list-plus", "bx-list-plus", "bx-trash", "bx-dots-vertical-rounded"
]);
// The card's own branch, and neither the tree's confirmation shortcut nor its erase.
await act(async () => { button("bx-trash").click(); });
expect(deleteNotes.mock.calls).toEqual([ [ [ expect.any(String) ], false, false ] ]);
deleteNotes.mockRestore();
});
/**
* A long press picks the card up, and nothing is offered while it is carried. Once the drop
* is aborted, the card takes the focus back, and the rail with it. Aborted rather than let
* go: with no geometry under happy-dom a release is a drop at the head of a column, and the
* move it starts runs on past the test.
*/
it("stands through a lift, hides once the card is carried, and is back after", async () => {
await setup();
await focus("tool1");
vi.useFakeTimers();
const element = card("tool1");
const press = (type: string, x = 10) => element.dispatchEvent(new PointerEvent(type, {
bubbles: true, pointerType: "touch", clientX: x, clientY: 10
}));
const board = () => container.querySelector(".board-view-container");
await act(async () => {
press("pointerdown");
vi.advanceTimersByTime(1000);
});
// The drag's own class, which must survive the board's redraw on activation, or the
// auto-scroll it keeps working stops with it. The rail stands: nothing has moved yet.
expect(board()?.classList.contains("board-dragging")).toBe(true);
expect(board()?.classList.contains("board-carrying")).toBe(false);
expect(toolbar()?.classList.contains("leaving")).toBe(false);
// Carried once it has come far enough from where it was lifted, which is what hides the
// rail (by class, see card_toolbar.css); a wobble under the finger does not.
await act(async () => { press("pointermove", 22); });
expect(board()?.classList.contains("board-carrying")).toBe(false);
await act(async () => { press("pointermove", 35); });
expect(board()?.classList.contains("board-carrying")).toBe(true);
await act(async () => { press("pointercancel", 35); });
expect(board()?.classList.contains("board-dragging")).toBe(false);
expect(board()?.classList.contains("board-carrying")).toBe(false);
expect(document.activeElement).toBe(card("tool1"));
expect(toolbar()?.classList.contains("leaving")).toBe(false);
});
});
describe("Selection mode on mobile", () => {
let container: HTMLElement;
let host: Component;
beforeEach(() => {
layout.onMobile = true;
});
afterEach(() => {
layout.onMobile = false;
saved.length = 0;
render(null, container);
container.remove();
});
/** A collection note, so the header the mode is switched on from is drawn. */
async function setup(labels: Record<string, string> = {}) {
disposeShownModals();
const note = buildNote({
title: "Board",
type: "book",
"#collection": "",
"#viewType": "board",
...labels,
children: [
{ id: "pick1", title: "First", "#status": "To Do" },
{ id: "pick2", title: "Second", "#status": "To Do" },
{ id: "pick3", title: "Third", "#status": "Done" }
]
});
host = new Component();
container = document.body.appendChild(document.createElement("div"));
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" }, { value: "Done" } ] }}
/>
</ParentComponent.Provider>,
container
);
});
await act(async () => { await flush(); });
return note;
}
function card(noteId: string) {
const element = container.querySelector<HTMLElement>(
`.board-note[data-note-id="${noteId}"]`);
if (!element) throw new Error(`expected the card ${noteId}`);
return element;
}
const board = () => container.querySelector(".board-view-container");
const bar = () => container.querySelector<HTMLElement>(".board-selection-bar");
const selected = () => [ ...container.querySelectorAll(".board-note.selected") ]
.map(element => element.getAttribute("data-note-id"));
function headerButton(icon: string) {
const element = container.querySelector<HTMLElement>(`.board-header-tools button.${icon}`);
if (!element) throw new Error(`expected a ${icon} button in the header`);
return element;
}
function railButton(icon: string) {
const element = container.querySelector<HTMLElement>(`.board-card-toolbar button.${icon}`);
if (!element) throw new Error(`expected a ${icon} button on the rail`);
return element;
}
async function startSelecting() {
await act(async () => { headerButton("bx-select-multiple").click(); });
}
async function tap(noteId: string) {
await act(async () => {
card(noteId).focus();
card(noteId).click();
});
}
it("is switched on from the header and covers that row while it lasts", async () => {
await setup();
expect(bar()).toBeNull();
expect(board()?.classList.contains("selecting")).toBe(false);
await startSelecting();
expect(board()?.classList.contains("selecting")).toBe(true);
expect(bar()?.querySelector(".board-selection-count")?.textContent)
.toBe('board_view.cards-selected:{"count":0}');
// Over the grouping and the filter, which stay in the page beneath it.
expect(bar()?.parentElement?.querySelector(".board-group-by")).toBeTruthy();
await act(async () => { headerButton("bx-x").click(); });
expect(bar()).toBeNull();
expect(board()?.classList.contains("selecting")).toBe(false);
});
it("offers no way to delete what is picked out of a read-only board", async () => {
await setup({ "#readOnly": "" });
await startSelecting();
await tap("pick1");
expect(selected()).toEqual([ "pick1" ]);
expect(railButton("bx-dots-vertical-rounded")).toBeTruthy();
expect(container.querySelector(".board-card-toolbar button.bx-trash")).toBeNull();
});
it("is left out off mobile", async () => {
layout.onMobile = false;
await setup();
expect(container.querySelector(".board-selection-toggle")).toBeNull();
// Off mobile they are drawn in `OverlayControlGroup`, outside `.board-header-tools`.
expect(container.querySelector(".board-header-tools button.bx-collapse-alt")).toBeNull();
expect(container.querySelector(".board-header-tools button.bx-expand-alt")).toBeNull();
});
/** `onCollapseAll` and `onExpandAll`, which only the header carries on mobile. */
it("closes and opens every column from the header", async () => {
await setup();
const columns = () => [ ...container.querySelectorAll(".board-column") ];
expect(columns().some(column => column.classList.contains("collapsed"))).toBe(false);
await act(async () => {
headerButton("bx-collapse-alt").click();
await flush();
});
expect(columns().every(column => column.classList.contains("collapsed"))).toBe(true);
await act(async () => {
headerButton("bx-expand-alt").click();
await flush();
});
expect(columns().some(column => column.classList.contains("collapsed"))).toBe(false);
});
/**
* A tap picks a card out or puts it back, and opens nothing. The focused card's own rail
* stands aside for the selection's, which has nothing to offer until a card is picked.
*/
it("picks cards out with a tap instead of opening them", async () => {
await setup();
const openCard = vi.spyOn(BoardApi.prototype, "openCard").mockImplementation(() => {});
await startSelecting();
expect(railButton("bx-trash").hasAttribute("disabled")).toBe(true);
expect(container.querySelector(".board-card-toolbar button.bx-rename")).toBeNull();
await tap("pick1");
expect(selected()).toEqual([ "pick1" ]);
expect(openCard).not.toHaveBeenCalled();
expect(bar()?.querySelector(".board-selection-count")?.textContent)
.toBe('board_view.cards-selected:{"count":1}');
expect(railButton("bx-trash").hasAttribute("disabled")).toBe(false);
expect(container.querySelector(".board-card-toolbar button.bx-rename")).toBeNull();
await tap("pick1");
expect(selected()).toEqual([]);
// Off again, a tap opens the card as before.
await act(async () => { headerButton("bx-x").click(); });
await tap("pick1");
expect(openCard).toHaveBeenCalledTimes(1);
expect(selected()).toEqual([]);
openCard.mockRestore();
});
/** The selection rail stands for the mode; a heading focused meanwhile floats no rail. */
it("keeps the selection rail alone when a heading is focused", async () => {
await setup();
await startSelecting();
const heading = container.querySelector<HTMLElement>(".board-column h3");
if (!heading) throw new Error("expected a heading");
await act(async () => { heading.focus(); });
expect(container.querySelectorAll(".board-card-toolbar")).toHaveLength(1);
expect([ ...container.querySelectorAll(".board-card-toolbar button") ]
.map(button => [ ...button.classList ].find(name => name.startsWith("bx-"))))
.toEqual([ "bx-trash", "bx-dots-vertical-rounded" ]);
});
it("selects the column of the last picked card, and acts on the whole selection", async () => {
await setup();
const deleteNotes = vi.spyOn(branches, "deleteNotes").mockResolvedValue(false);
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
await startSelecting();
await tap("pick1");
await act(async () => { headerButton("bx-list-check").click(); });
expect(selected().sort()).toEqual([ "pick1", "pick2" ]);
// The menu is the one the cards offer, carrying every picked card.
await withTabManager(async () => {
await act(async () => { railButton("bx-dots-vertical-rounded").click(); });
});
expect(show).toHaveBeenCalledTimes(1);
await act(async () => { railButton("bx-trash").click(); });
expect(deleteNotes).toHaveBeenCalledTimes(1);
expect(deleteNotes.mock.calls[0][0]).toHaveLength(2);
// Reset gives the selection up and ends the mode with it.
await act(async () => { headerButton("bx-x").click(); });
expect(selected()).toEqual([]);
expect(bar()).toBeNull();
deleteNotes.mockRestore();
show.mockRestore();
});
/**
* The board is not remounted between notes, and a card the next board also holds, as a clone,
* would otherwise still be selected there without the reader having picked it.
*/
it("gives the selection up with the mode when another board is shown", async () => {
await setup();
await startSelecting();
await tap("pick1");
expect(selected()).toEqual([ "pick1" ]);
const other = buildNote({
title: "Other board",
type: "book",
"#collection": "",
"#viewType": "board",
children: [ { id: "pick1", title: "First", "#status": "To Do" } ]
});
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={other}
noteIds={[ ...other.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" } ] }}
/>
</ParentComponent.Provider>,
container
);
});
await act(async () => { await flush(); });
expect(card("pick1")).toBeTruthy();
expect(selected()).toEqual([]);
expect(bar()).toBeNull();
});
});
describe("Column toolbar on mobile", () => {
let container: HTMLElement;
let host: Component;
beforeEach(() => {
layout.onMobile = true;
});
afterEach(() => {
layout.onMobile = false;
vi.useRealTimers();
saved.length = 0;
render(null, container);
container.remove();
});
async function setup() {
disposeShownModals();
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
children: [
{ id: "head1", title: "First", "#status": "To Do" },
{ id: "head2", title: "Second", "#status": "Done" }
]
});
host = new Component();
container = document.body.appendChild(document.createElement("div"));
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
initialConfig={{ columns: [ { value: "To Do" }, { value: "Done" } ] }}
/>
</ParentComponent.Provider>,
container
);
});
await act(async () => { await flush(); });
return note;
}
function heading(index: number) {
const element = container.querySelectorAll<HTMLElement>(".board-column h3")[index];
if (!element) throw new Error(`expected the heading of column ${index}`);
return element;
}
const column = (index: number) => container.querySelectorAll(".board-column")[index];
const toolbar = () => container.querySelector<HTMLElement>(".board-card-toolbar");
const buttons = () => [ ...toolbar()?.querySelectorAll("button") ?? [] ]
.map(button => [ ...button.classList ].find(name => name.startsWith("bx-")));
function button(icon: string) {
const element = toolbar()?.querySelector<HTMLElement>(`button.${icon}`);
if (!element) throw new Error(`expected a ${icon} button`);
return element;
}
async function letRailLeave() {
await act(async () => { vi.advanceTimersByTime(RAIL_EXIT_MS); });
}
it("follows the focus onto a heading, which a tap gives it", async () => {
await setup();
expect(toolbar()).toBeNull();
// A tap is a click, and on mobile the heading takes the focus from it.
await act(async () => { heading(0).click(); });
expect(document.activeElement).toBe(heading(0));
expect(buttons()).toEqual([ "bx-edit-alt", "bx-collapse-horizontal", "bx-sort-alt-2" ]);
vi.useFakeTimers();
await act(async () => { heading(0).blur(); });
await letRailLeave();
expect(toolbar()).toBeNull();
});
/** Neither gesture the hint names is there on a touch screen, where the rail collapses. */
it("leaves the collapse hint off the heading", async () => {
await setup();
expect(Tooltip.getInstance(heading(0))).toBeNull();
});
it("collapses and opens the column, offering only the open on a strip", async () => {
await setup();
await act(async () => { heading(0).focus(); });
await act(async () => { button("bx-collapse-horizontal").click(); });
expect(column(0).classList.contains("collapsed")).toBe(true);
expect(buttons()).toEqual([ "bx-expand-horizontal" ]);
await act(async () => { button("bx-expand-horizontal").click(); });
expect(column(0).classList.contains("collapsed")).toBe(false);
expect(buttons()).toEqual([ "bx-edit-alt", "bx-collapse-horizontal", "bx-sort-alt-2" ]);
});
/**
* A tap lands on a strip on the way to the rail standing over it, so it brings the rail up and
* leaves the column as it is. Only the rail's own button opens it.
*/
it("leaves a strip collapsed when it is tapped, and opens it from the rail", async () => {
await setup();
await act(async () => { heading(0).focus(); });
await act(async () => { button("bx-collapse-horizontal").click(); });
expect(column(0).classList.contains("collapsed")).toBe(true);
await act(async () => { column(0).dispatchEvent(new Event("click", { bubbles: true })); });
expect(column(0).classList.contains("collapsed")).toBe(true);
expect(buttons()).toEqual([ "bx-expand-horizontal" ]);
await act(async () => { button("bx-expand-horizontal").click(); });
expect(column(0).classList.contains("collapsed")).toBe(false);
});
it("opens the sort menu and the title editor", async () => {
await setup();
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
await act(async () => { heading(0).focus(); });
await act(async () => { button("bx-sort-alt-2").click(); });
expect(show).toHaveBeenCalledTimes(1);
// The rename opens the editor in the heading, and the rail stands aside while it is open.
vi.useFakeTimers();
await act(async () => { button("bx-edit-alt").click(); });
await letRailLeave();
expect(heading(0).querySelector("input, textarea")).toBeTruthy();
expect(toolbar()).toBeNull();
show.mockRestore();
});
});
describe("BoardView, read-only", () => {
let container: HTMLElement | undefined;
afterEach(() => {
vi.restoreAllMocks();
saved.length = 0;
if (container) {
render(null, container);
container.remove();
container = undefined;
}
});
/** `forced` makes the board read-only through `isReadOnly`, as a slide does, instead of `#readOnly`. */
async function setup(host = new Component(), { forced = false } = {}) {
const note = buildNote({
title: "Board",
"#collection": "",
"#viewType": "board",
...(forced ? {} : { "#readOnly": "" }),
children: [
{ id: "roCard1", title: "First", "#status": "To Do" },
{ id: "roCard2", title: "Second", "#status": "Done" }
]
});
const mountPoint = document.createElement("div");
container = mountPoint;
document.body.appendChild(mountPoint);
await act(async () => {
render(
<ParentComponent.Provider value={host}>
<Harness note={note} noteIds={[ ...note.getChildNoteIds() ]} initialConfig={{}} isReadOnly={forced || undefined} />
</ParentComponent.Provider>,
mountPoint
);
});
await act(async () => { await flush(); });
return mountPoint;
}
/** The titles of the entries of the menu last shown. */
const menuTitles = (show: { mock: { calls: unknown[][] } }) =>
((show.mock.calls.at(-1)?.[0] as { items: { title?: string }[] } | undefined)?.items ?? [])
.map((item) => item.title);
it("is read-only through `isReadOnly` even when its note can be edited", async () => {
const board = await setup(new Component(), { forced: true });
expect(board.querySelector(".board-note")).not.toBeNull();
expect(board.querySelector(".board-new-item")).toBeNull();
expect(board.querySelector(".board-add-column")).toBeNull();
expect(board.querySelector(".board-note .edit-icon")).toBeNull();
});
it("offers no way to add a card or a column, nor to rename either", async () => {
vi.mocked(getNoteTypeOptions).mockClear();
const board = await setup();
// What a card can be made from is read for making one, which a read-only board never does.
expect(getNoteTypeOptions).not.toHaveBeenCalled();
const column = board.querySelector<HTMLElement>(".board-column");
const card = board.querySelector<HTMLElement>(".board-note");
expect(column === null || card === null).toBe(false);
expect(board.querySelector(".board-new-item")).toBeNull();
expect(board.querySelector(".board-add-column")).toBeNull();
expect(board.querySelector(".board-column h3 > .column-icon.static")).not.toBeNull();
expect(board.querySelector(".board-column h3 > .column-icon button")).toBeNull();
expect(board.querySelector(".board-note .edit-icon")).toBeNull();
await act(async () => {
if (column) startEditingTitle(column);
card?.dispatchEvent(new KeyboardEvent("keydown", { key: "F2", bubbles: true }));
card?.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
expect(board.querySelector(".board-column h3.editing")).toBeNull();
expect(board.querySelectorAll(".board-note.editing, .board-new-item").length).toBe(0);
});
it("offers as hints only the keys that change nothing", async () => {
const host = new Component();
await setup(host);
const sections = collectShortcutHints(host);
expect(sections.map((section) => section.titleKey)).toEqual([
"board_view.hints.navigation",
"board_view.hints.selection"
]);
expect(sections.flatMap((section) => section.hints.map((hint) => hint.labelKey))).toEqual([
"board_view.hints.navigate_items",
"board_view.hints.navigate_columns",
"board_view.hints.first_last_item",
"board_view.hints.open_item",
"board_view.hints.toggle_column",
"board_view.hints.toggle_selection",
"board_view.hints.extend_selection",
"board_view.hints.select_column",
"board_view.hints.clear_selection"
]);
});
it("changes nothing from the keyboard or from a note dropped from the tree", async () => {
const board = await setup();
// Opening the board records its columns, which is not the reader's doing.
saved.length = 0;
const writes = [
...([
"removeFromBoard", "confirmAndRemoveColumn", "insertColumn", "moveToColumnEnd",
"reorderColumn", "changeColumn"
] as const).map((name) => vi.spyOn(BoardApi.prototype, name)),
...([ "deleteNotes", "moveAfterBranch" ] as const)
.map((name) => vi.spyOn(branches, name).mockResolvedValue(undefined as never))
];
const card = board.querySelector<HTMLElement>(".board-note");
const header = board.querySelector<HTMLElement>(".board-column h3");
const column = board.querySelector<HTMLElement>(".board-column");
expect([ card, header, column ]).not.toContain(null);
const presses: [ HTMLElement | null, KeyboardEventInit ][] = [
[ card, { key: "Delete" } ],
[ card, { key: "Delete", shiftKey: true } ],
[ card, { key: "ArrowRight", ctrlKey: true } ],
[ card, { key: "ArrowDown", ctrlKey: true } ],
[ header, { key: "Enter", ctrlKey: true } ],
[ header, { key: "ArrowRight", ctrlKey: true } ],
[ header, { key: "Delete" } ]
];
for (const [ target, init ] of presses) {
await act(async () => {
target?.focus();
target?.dispatchEvent(
new KeyboardEvent("keydown", { ...init, bubbles: true, cancelable: true }));
await flush();
});
}
const dragged = JSON.stringify(
[ { noteId: "roCard2", branchId: "roBranch", title: "Second" } ]);
for (const type of [ "dragover", "drop" ]) {
const event = new Event(type, { bubbles: true, cancelable: true });
Object.defineProperty(event, "dataTransfer", {
value: { types: [ TREE_CLIPBOARD_TYPE ], getData: () => dragged }
});
await act(async () => {
column?.dispatchEvent(event);
await flush();
});
}
expect({
saved,
called: writes
.filter((write) => write.mock.calls.length)
.map((write) => write.getMockName())
}).toEqual({ saved: [], called: [] });
});
it("keeps only the entries that change nothing in the menus, and shows none for the board", async () => {
const board = await setup();
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
await withTabManager(async () => {
board.querySelector(".board-note")?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
});
const cardMenu = menuTitles(show);
expect(cardMenu).toContain("board_view.copy-reference");
expect(cardMenu).not.toContain("board_view.edit-title");
expect(cardMenu).not.toContain("link_context_menu.open_note_in_popup");
expect(cardMenu).not.toContain("board_view.delete-note");
board.querySelector(".board-column h3")?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
expect(menuTitles(show)).toEqual([ "board_view.copy-reference" ]);
const calls = show.mock.calls.length;
board.querySelector(".board-view-container")
?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
expect(show.mock.calls.length).toBe(calls);
});
});