Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import {
|
||||
createHotSwapDrillPage,
|
||||
createMockPage,
|
||||
createMockRegistry,
|
||||
createVisualSnapshot,
|
||||
generateHotSwapMock,
|
||||
generateMockArtifacts,
|
||||
generateMockPageHtml,
|
||||
} from "./index.js";
|
||||
|
||||
describe("@toolctrl/ui-mock", () => {
|
||||
test("builds route-addressable hot-swap mock pages", () => {
|
||||
const page = createMockPage({ id: "empty", route: "/empty", state: { status: "empty" } });
|
||||
const registry = createMockRegistry([page]);
|
||||
expect(registry.get("/empty")).toBe(page);
|
||||
expect(generateHotSwapMock(page)).toContain("\"route\": \"/empty\"");
|
||||
});
|
||||
|
||||
test("generates deterministic mock module and static page artifacts", () => {
|
||||
const page = createHotSwapDrillPage();
|
||||
const artifacts = generateMockArtifacts(page);
|
||||
|
||||
expect(artifacts.module).toContain("export const mockPage");
|
||||
expect(artifacts.module).toContain("\"id\": \"phase-c-hot-swap\"");
|
||||
expect(artifacts.html).toContain('data-ui-mock-route="/mock/phase-c/hot-swap"');
|
||||
expect(artifacts.html).toContain("Serialized state");
|
||||
expect(artifacts.html).toContain("Hot swap drill");
|
||||
expect(artifacts.html).not.toContain(">mock.phase-c");
|
||||
expect(artifacts.snapshot).toEqual(createVisualSnapshot(page, { html: artifacts.html, module: artifacts.module }));
|
||||
});
|
||||
|
||||
test("keeps visual snapshots stable for equivalent mock page input", () => {
|
||||
const page = createHotSwapDrillPage();
|
||||
const reorderedPage = createMockPage({
|
||||
route: page.route,
|
||||
id: page.id,
|
||||
descriptionKey: page.descriptionKey,
|
||||
titleKey: page.titleKey,
|
||||
registries: page.registries,
|
||||
state: page.state,
|
||||
});
|
||||
|
||||
expect(generateHotSwapMock(reorderedPage)).toBe(generateHotSwapMock(page));
|
||||
expect(generateMockPageHtml(reorderedPage)).toBe(generateMockPageHtml(page));
|
||||
expect(createVisualSnapshot(reorderedPage)).toEqual(createVisualSnapshot(page));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,383 @@
|
||||
import type { DataTableModel, FileBrowserModel, ShellRegistries, TaskListModel, VMState } from "@toolctrl/view-models";
|
||||
|
||||
export type MockPage<T = unknown> = {
|
||||
readonly id: string;
|
||||
readonly route: string;
|
||||
readonly title?: string;
|
||||
readonly titleKey?: string;
|
||||
readonly description?: string;
|
||||
readonly descriptionKey?: string;
|
||||
readonly state: VMState<T>;
|
||||
readonly registries?: ShellRegistries;
|
||||
};
|
||||
|
||||
export type HotSwapDrillData = {
|
||||
readonly cards: readonly {
|
||||
readonly labelKey: string;
|
||||
readonly value: string;
|
||||
readonly trend: "up" | "flat" | "down";
|
||||
}[];
|
||||
readonly activity: readonly {
|
||||
readonly labelKey: string;
|
||||
readonly meta: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
export type MockVisualSnapshot = {
|
||||
readonly pageId: string;
|
||||
readonly route: string;
|
||||
readonly status: string;
|
||||
readonly htmlHash: string;
|
||||
readonly moduleHash: string;
|
||||
readonly stateHash: string;
|
||||
readonly metrics: {
|
||||
readonly htmlBytes: number;
|
||||
readonly moduleBytes: number;
|
||||
readonly sidebarItems: number;
|
||||
readonly breadcrumbItems: number;
|
||||
readonly mobileItems: number;
|
||||
readonly portalItems: number;
|
||||
};
|
||||
};
|
||||
|
||||
export type MockArtifacts = {
|
||||
readonly module: string;
|
||||
readonly html: string;
|
||||
readonly snapshot: MockVisualSnapshot;
|
||||
};
|
||||
|
||||
export function createMockPage<T>(page: MockPage<T>): MockPage<T> {
|
||||
return page;
|
||||
}
|
||||
|
||||
export function createMockRegistry(pages: readonly MockPage[] = []): ReadonlyMap<string, MockPage> {
|
||||
return new Map(pages.map((page) => [page.route, page]));
|
||||
}
|
||||
|
||||
export function generateHotSwapMock<T>(page: MockPage<T>): string {
|
||||
return ["export const mockPage = ", stableStringify(page), ";", "export default mockPage;", ""].join("\n");
|
||||
}
|
||||
|
||||
export function generateMockPageHtml<T>(page: MockPage<T>): string {
|
||||
const stateJson = stableStringify(page.state);
|
||||
const stateSummary = stableStringify({
|
||||
status: translateMockText(`mock.status.${page.state.status}`, page.state.status),
|
||||
cards: isHotSwapDrillData(page.state.data) ? page.state.data.cards.length : 0,
|
||||
activity: isHotSwapDrillData(page.state.data) ? page.state.data.activity.length : 0,
|
||||
});
|
||||
const registriesJson = stableStringify(page.registries ?? {});
|
||||
const title = page.titleKey ? translateMockText(page.titleKey, page.titleKey) : (page.title ?? page.id);
|
||||
const description = page.descriptionKey ? translateMockText(page.descriptionKey, page.descriptionKey) : (page.description ?? `mock.static.${page.id}`);
|
||||
const sidebarItems = page.registries?.sidebar ?? [];
|
||||
const breadcrumbs = page.registries?.breadcrumbs ?? [];
|
||||
const cards = isHotSwapDrillData(page.state.data) ? page.state.data.cards : [];
|
||||
const activity = isHotSwapDrillData(page.state.data) ? page.state.data.activity : [];
|
||||
|
||||
return [
|
||||
"<!doctype html>",
|
||||
'<html lang="en">',
|
||||
"<head>",
|
||||
' <meta charset="utf-8">',
|
||||
' <meta name="viewport" content="width=device-width, initial-scale=1">',
|
||||
` <title>${escapeHtml(`${translateMockText("mock.ui.documentTitle", "UI Mock")}: ${title}`)}</title>`,
|
||||
" <style>",
|
||||
" :root { color-scheme: light; --bg: #f6f7f9; --panel: #ffffff; --ink: #1f2937; --muted: #64748b; --line: #d9e0e8; --accent: #2563eb; --ok: #0f766e; }",
|
||||
" * { box-sizing: border-box; }",
|
||||
" body { margin: 0; background: var(--bg); color: var(--ink); font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }",
|
||||
" .mock-shell { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 720px; }",
|
||||
" .mock-nav { border-right: 1px solid var(--line); background: #eef2f7; padding: 24px 18px; }",
|
||||
" .mock-brand { font-size: 12px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }",
|
||||
" .mock-link { display: block; margin-top: 12px; padding: 9px 10px; border-radius: 6px; color: var(--ink); text-decoration: none; }",
|
||||
" .mock-link[data-active='true'] { background: var(--panel); color: var(--accent); box-shadow: inset 0 0 0 1px var(--line); }",
|
||||
" .mock-main { padding: 32px; }",
|
||||
" .mock-breadcrumbs { display: flex; gap: 8px; margin-bottom: 20px; color: var(--muted); font-size: 13px; }",
|
||||
" .mock-header { display: flex; justify-content: space-between; gap: 24px; align-items: flex-start; margin-bottom: 24px; }",
|
||||
" .mock-title { margin: 0; font-size: 28px; line-height: 1.2; }",
|
||||
" .mock-description { margin: 8px 0 0; max-width: 620px; color: var(--muted); }",
|
||||
" .mock-status { border: 1px solid var(--line); border-radius: 6px; background: var(--panel); padding: 10px 12px; color: var(--ok); font-weight: 700; }",
|
||||
" .mock-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; margin-bottom: 16px; }",
|
||||
" .mock-card, .mock-state { border: 1px solid var(--line); border-radius: 6px; background: var(--panel); padding: 16px; }",
|
||||
" .mock-card-label { margin: 0 0 8px; color: var(--muted); font-size: 13px; }",
|
||||
" .mock-card-value { margin: 0; font-size: 24px; font-weight: 750; }",
|
||||
" .mock-card-trend { margin-top: 10px; color: var(--ok); font-size: 12px; text-transform: uppercase; }",
|
||||
" .mock-activity { margin: 0; padding: 0; list-style: none; }",
|
||||
" .mock-activity li { display: flex; justify-content: space-between; gap: 16px; padding: 10px 0; border-top: 1px solid var(--line); }",
|
||||
" .mock-activity li:first-child { border-top: 0; padding-top: 0; }",
|
||||
" .mock-state pre { margin: 12px 0 0; white-space: pre-wrap; color: #334155; }",
|
||||
" </style>",
|
||||
"</head>",
|
||||
`<body data-ui-mock-route="${escapeAttribute(page.route)}" data-ui-mock-status="${escapeAttribute(page.state.status)}">`,
|
||||
` <div class="mock-shell" data-ui-mock-page="${escapeAttribute(page.id)}">`,
|
||||
` <nav class="mock-nav" aria-label="${escapeAttribute(translateMockText("mock.ui.sidebarAria", "Mock sidebar"))}">`,
|
||||
` <div class="mock-brand">${escapeHtml(translateMockText("mock.ui.brand", "Toolctrl mock"))}</div>`,
|
||||
...sidebarItems.map(
|
||||
(item) =>
|
||||
` <a class="mock-link" data-active="${item.href === page.route ? "true" : "false"}" href="${escapeAttribute(item.href)}">${escapeHtml(translateMockText(item.labelKey, item.labelKey))}</a>`
|
||||
),
|
||||
" </nav>",
|
||||
' <main class="mock-main">',
|
||||
` <div class="mock-breadcrumbs">${breadcrumbs.map((item) => `<span>${escapeHtml(translateMockText(item.labelKey, item.labelKey))}</span>`).join("<span>/</span>")}</div>`,
|
||||
' <header class="mock-header">',
|
||||
" <div>",
|
||||
` <h1 class="mock-title">${escapeHtml(title)}</h1>`,
|
||||
` <p class="mock-description">${escapeHtml(description)}</p>`,
|
||||
" </div>",
|
||||
` <div class="mock-status">${escapeHtml(translateMockText(`mock.status.${page.state.status}`, page.state.status))}</div>`,
|
||||
" </header>",
|
||||
` <section class="mock-grid" aria-label="${escapeAttribute(translateMockText("mock.ui.metricsAria", "Mock metrics"))}">`,
|
||||
...cards.map((card) =>
|
||||
[
|
||||
' <article class="mock-card">',
|
||||
` <p class="mock-card-label">${escapeHtml(translateMockText(card.labelKey, card.labelKey))}</p>`,
|
||||
` <p class="mock-card-value">${escapeHtml(card.value)}</p>`,
|
||||
` <div class="mock-card-trend">${escapeHtml(translateMockText(`mock.trend.${card.trend}`, card.trend))}</div>`,
|
||||
" </article>",
|
||||
].join("\n")
|
||||
),
|
||||
" </section>",
|
||||
` <section class="mock-card" aria-label="${escapeAttribute(translateMockText("mock.ui.activityAria", "Mock activity"))}">`,
|
||||
' <ul class="mock-activity">',
|
||||
...activity.map(
|
||||
(item) => ` <li><span>${escapeHtml(translateMockText(item.labelKey, item.labelKey))}</span><span>${escapeHtml(item.meta)}</span></li>`
|
||||
),
|
||||
" </ul>",
|
||||
" </section>",
|
||||
` <section class="mock-state" aria-label="${escapeAttribute(translateMockText("mock.ui.stateAria", "Mock state payload"))}">`,
|
||||
` <strong>${escapeHtml(translateMockText("mock.ui.serializedState", "Serialized state"))}</strong>`,
|
||||
` <pre>${escapeHtml(stateSummary)}</pre>`,
|
||||
" </section>",
|
||||
` <script type="application/json" id="mock-page-state">${escapeHtml(stateJson)}</script>`,
|
||||
` <script type="application/json" id="mock-page-registries">${escapeHtml(registriesJson)}</script>`,
|
||||
" </main>",
|
||||
" </div>",
|
||||
"</body>",
|
||||
"</html>",
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
const defaultMockTranslations: Record<string, string> = {
|
||||
"mock.root": "Mock",
|
||||
"mock.phase-c.label": "Phase C",
|
||||
"mock.phase-c.home": "Phase C home",
|
||||
"mock.phase-c.summary": "Phase C summary",
|
||||
"mock.phase-c.hot-swap.label": "Hot swap",
|
||||
"mock.phase-c.hot-swap.title": "Hot swap drill",
|
||||
"mock.phase-c.hot-swap.description": "Static mock page used to verify hot-swap rendering.",
|
||||
"mock.phase-c.hot-swap.metrics.modules": "Modules",
|
||||
"mock.phase-c.hot-swap.metrics.snapshotHash": "Snapshot hash",
|
||||
"mock.phase-c.hot-swap.metrics.browserDownloads": "Browser downloads",
|
||||
"mock.phase-c.hot-swap.activity.generatedModule": "Generated module",
|
||||
"mock.phase-c.hot-swap.activity.staticArtifact": "Static artifact",
|
||||
"mock.phase-c.hot-swap.activity.visualSnapshot": "Visual snapshot",
|
||||
"mock.phase-c.hot-swap.replacement.label": "Replacement",
|
||||
"mock.phase-c.hot-swap.replacement.title": "Replacement hot swap drill",
|
||||
"mock.phase-c.hot-swap.metrics.swap": "Swap",
|
||||
"mock.phase-c.hot-swap.activity.registrySwap": "Registry swap",
|
||||
"mock.ui.documentTitle": "UI Mock",
|
||||
"mock.ui.sidebarAria": "Mock sidebar",
|
||||
"mock.ui.brand": "Toolctrl mock",
|
||||
"mock.ui.metricsAria": "Mock metrics",
|
||||
"mock.ui.activityAria": "Mock activity",
|
||||
"mock.ui.stateAria": "Mock state payload",
|
||||
"mock.ui.serializedState": "Serialized state",
|
||||
"mock.status.ready": "Ready",
|
||||
"mock.trend.up": "Up",
|
||||
"mock.trend.flat": "Flat",
|
||||
"mock.trend.down": "Down",
|
||||
};
|
||||
|
||||
function translateMockText(key: string, fallback: string) {
|
||||
return defaultMockTranslations[key] ?? fallback;
|
||||
}
|
||||
|
||||
export function createVisualSnapshot<T>(page: MockPage<T>, artifacts?: { readonly html?: string; readonly module?: string }): MockVisualSnapshot {
|
||||
const html = artifacts?.html ?? generateMockPageHtml(page);
|
||||
const module = artifacts?.module ?? generateHotSwapMock(page);
|
||||
const state = stableStringify(page.state);
|
||||
return {
|
||||
pageId: page.id,
|
||||
route: page.route,
|
||||
status: page.state.status,
|
||||
htmlHash: stableHash(html),
|
||||
moduleHash: stableHash(module),
|
||||
stateHash: stableHash(state),
|
||||
metrics: {
|
||||
htmlBytes: byteLength(html),
|
||||
moduleBytes: byteLength(module),
|
||||
sidebarItems: page.registries?.sidebar?.length ?? 0,
|
||||
breadcrumbItems: page.registries?.breadcrumbs?.length ?? 0,
|
||||
mobileItems: page.registries?.mobile?.length ?? 0,
|
||||
portalItems: page.registries?.portals?.length ?? 0,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function generateMockArtifacts<T>(page: MockPage<T>): MockArtifacts {
|
||||
const module = generateHotSwapMock(page);
|
||||
const html = generateMockPageHtml(page);
|
||||
return {
|
||||
module,
|
||||
html,
|
||||
snapshot: createVisualSnapshot(page, { html, module }),
|
||||
};
|
||||
}
|
||||
|
||||
export function createHotSwapDrillPage(): MockPage<HotSwapDrillData> {
|
||||
return createMockPage({
|
||||
id: "phase-c-hot-swap",
|
||||
route: "/mock/phase-c/hot-swap",
|
||||
titleKey: "mock.phase-c.hot-swap.title",
|
||||
descriptionKey: "mock.phase-c.hot-swap.description",
|
||||
state: {
|
||||
status: "ready",
|
||||
data: {
|
||||
cards: [
|
||||
{ labelKey: "mock.phase-c.hot-swap.metrics.modules", value: "3", trend: "up" },
|
||||
{ labelKey: "mock.phase-c.hot-swap.metrics.snapshotHash", value: "stable", trend: "flat" },
|
||||
{ labelKey: "mock.phase-c.hot-swap.metrics.browserDownloads", value: "0", trend: "down" },
|
||||
],
|
||||
activity: [
|
||||
{ labelKey: "mock.phase-c.hot-swap.activity.generatedModule", meta: "index.ts" },
|
||||
{ labelKey: "mock.phase-c.hot-swap.activity.staticArtifact", meta: "hot-swap.html" },
|
||||
{ labelKey: "mock.phase-c.hot-swap.activity.visualSnapshot", meta: "fnv1a" },
|
||||
],
|
||||
},
|
||||
},
|
||||
registries: {
|
||||
sidebar: [
|
||||
{ id: "phase-c-home", href: "/mock/phase-c", labelKey: "mock.phase-c.home" },
|
||||
{ id: "phase-c-hot-swap", href: "/mock/phase-c/hot-swap", labelKey: "mock.phase-c.hot-swap.label" },
|
||||
],
|
||||
breadcrumbs: [
|
||||
{ id: "mock", href: "/mock", labelKey: "mock.root" },
|
||||
{ id: "phase-c", href: "/mock/phase-c", labelKey: "mock.phase-c.label" },
|
||||
{ id: "hot-swap", href: "/mock/phase-c/hot-swap", labelKey: "mock.phase-c.hot-swap.label" },
|
||||
],
|
||||
mobile: [{ id: "phase-c-hot-swap", href: "/mock/phase-c/hot-swap", labelKey: "mock.phase-c.hot-swap.label", priority: 1 }],
|
||||
portals: [{ id: "phase-c-summary", slot: "mock.summary", labelKey: "mock.phase-c.summary", order: 1 }],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock render implementation: renders the SAME normalized {@link DataTableModel}
|
||||
* data as the real Subframe impl but with deliberately different, minimal chrome
|
||||
* (div/row layout instead of a semantic `<table>`). Proves the implementation is
|
||||
* swappable — the data travels identically, only the surrounding markup differs.
|
||||
* Carries only structure, data-attributes and cell data; labels stay as i18n keys.
|
||||
*/
|
||||
export function renderDataTableMock(model: DataTableModel): string {
|
||||
const headerCells = model.columns
|
||||
.map((col) => `<span data-cell="head" data-i18n-key="${escapeHtml(col.labelKey)}" data-align="${escapeHtml(col.align)}"></span>`)
|
||||
.join("");
|
||||
|
||||
const rows =
|
||||
model.rowCount === 0
|
||||
? `<div data-row="empty" data-empty="true" data-i18n-key="${escapeHtml(model.emptyLabelKey)}"></div>`
|
||||
: model.rows
|
||||
.map((row) => {
|
||||
const cells = model.columns
|
||||
.map((col) => `<span data-cell="body" data-align="${escapeHtml(col.align)}">${escapeHtml(row.cells[col.key] ?? "")}</span>`)
|
||||
.join("");
|
||||
return `<div data-row="body" data-row-id="${escapeHtml(row.id)}">${cells}</div>`;
|
||||
})
|
||||
.join("");
|
||||
|
||||
const caption = model.captionKey ? `<div data-row="caption" data-i18n-key="${escapeHtml(model.captionKey)}"></div>` : "";
|
||||
|
||||
return `<div data-ui-impl="mock" data-ui-id="ui.data-table" data-row-count="${model.rowCount}">${caption}<div data-row="head">${headerCells}</div>${rows}</div>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock render implementation for the files browser. Same normalized
|
||||
* {@link FileBrowserModel} data as the real Subframe impl, deliberately different
|
||||
* minimal chrome — proves the renderer is swappable (hot-swap drill, M017).
|
||||
*/
|
||||
export function renderFileBrowserMock(model: FileBrowserModel): string {
|
||||
const rows =
|
||||
model.rowCount === 0
|
||||
? `<div data-row="empty" data-empty="true" data-i18n-key="${escapeHtml(model.emptyHeadlineKey)}"></div>`
|
||||
: model.rows
|
||||
.map(
|
||||
(row) =>
|
||||
`<div data-row="body" data-row-id="${escapeHtml(row.id)}" data-mime="${escapeHtml(row.mimeType)}" data-size="${row.sizeBytes}">${escapeHtml(row.displayName)}</div>`
|
||||
)
|
||||
.join("");
|
||||
|
||||
return `<div data-ui-impl="mock" data-ui-id="ui.files.browser" data-row-count="${model.rowCount}"><div data-row="head" data-i18n-key="${escapeHtml(model.titleKey)}"></div>${rows}</div>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock render implementation for the reusable task list. Same normalized
|
||||
* {@link TaskListModel} data as the real Subframe impl, deliberately different
|
||||
* minimal chrome — proves the renderer is swappable (hot-swap drill, M017).
|
||||
*/
|
||||
export function renderTaskListMock(model: TaskListModel): string {
|
||||
const rows =
|
||||
model.rowCount === 0
|
||||
? `<div data-row="empty" data-empty="true" data-i18n-key="${escapeHtml(model.emptyHeadlineKey)}"></div>`
|
||||
: model.rows
|
||||
.map(
|
||||
(row) =>
|
||||
`<div data-row="body" data-row-id="${escapeHtml(row.id)}" data-status-key="${escapeHtml(row.statusKey)}" data-overdue="${row.isOverdue}">${escapeHtml(row.title)}</div>`
|
||||
)
|
||||
.join("");
|
||||
|
||||
return `<div data-ui-impl="mock" data-ui-id="ui.tasks.list" data-row-count="${model.rowCount}"><div data-row="head" data-i18n-key="${escapeHtml(model.titleKey)}"></div>${rows}</div>`;
|
||||
}
|
||||
|
||||
function isHotSwapDrillData(value: unknown): value is HotSwapDrillData {
|
||||
if (!value || typeof value !== "object") return false;
|
||||
const candidate = value as Partial<HotSwapDrillData>;
|
||||
return Array.isArray(candidate.cards) && Array.isArray(candidate.activity);
|
||||
}
|
||||
|
||||
function stableStringify(value: unknown, spaces = 2): string {
|
||||
return JSON.stringify(sortValue(value), null, spaces);
|
||||
}
|
||||
|
||||
function sortValue(value: unknown): unknown {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((item) => sortValue(item));
|
||||
}
|
||||
if (!value || typeof value !== "object") {
|
||||
return value;
|
||||
}
|
||||
return Object.fromEntries(
|
||||
Object.entries(value)
|
||||
.sort(([left], [right]) => left.localeCompare(right))
|
||||
.map(([key, child]) => [key, sortValue(child)])
|
||||
);
|
||||
}
|
||||
|
||||
function stableHash(value: string): string {
|
||||
let hash = 0x811c9dc5;
|
||||
for (let index = 0; index < value.length; index += 1) {
|
||||
hash ^= value.charCodeAt(index);
|
||||
hash = Math.imul(hash, 0x01000193);
|
||||
}
|
||||
return (hash >>> 0).toString(16).padStart(8, "0");
|
||||
}
|
||||
|
||||
function byteLength(value: string): number {
|
||||
let bytes = 0;
|
||||
for (let index = 0; index < value.length; index += 1) {
|
||||
const code = value.charCodeAt(index);
|
||||
if (code < 0x80) bytes += 1;
|
||||
else if (code < 0x800) bytes += 2;
|
||||
else if (code >= 0xd800 && code <= 0xdbff) {
|
||||
bytes += 4;
|
||||
index += 1;
|
||||
} else bytes += 3;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
function escapeHtml(value: string): string {
|
||||
return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """);
|
||||
}
|
||||
|
||||
function escapeAttribute(value: string): string {
|
||||
return escapeHtml(value).replaceAll("'", "'");
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import type { DataTableModel } from "@toolctrl/view-models";
|
||||
|
||||
import { renderDataTableMock } from "./index.js";
|
||||
|
||||
const model: DataTableModel = {
|
||||
captionKey: "ui.data-table.caption",
|
||||
columns: [
|
||||
{ key: "name", labelKey: "ui.data-table.column.name", align: "left" },
|
||||
{ key: "amount", labelKey: "ui.data-table.column.amount", align: "right" },
|
||||
],
|
||||
rows: [
|
||||
{ id: "row-1", cells: { name: "Alpha", amount: "10" } },
|
||||
{ id: "row-2", cells: { name: "Beta", amount: "20" } },
|
||||
],
|
||||
emptyLabelKey: "ui.data-table.empty",
|
||||
rowCount: 2,
|
||||
};
|
||||
|
||||
describe("renderDataTableMock (mock impl)", () => {
|
||||
test("carries the mock implementation marker and the row data", () => {
|
||||
const html = renderDataTableMock(model);
|
||||
expect(html).toContain('data-ui-impl="mock"');
|
||||
expect(html).toContain('data-ui-id="ui.data-table"');
|
||||
expect(html).toContain('data-row-id="row-1"');
|
||||
expect(html).toContain('data-row-id="row-2"');
|
||||
expect(html).toContain("Alpha");
|
||||
expect(html).toContain("20");
|
||||
});
|
||||
|
||||
test("renders an empty marker when there are no rows", () => {
|
||||
const html = renderDataTableMock({ ...model, rows: [], rowCount: 0 });
|
||||
expect(html).toContain('data-empty="true"');
|
||||
expect(html).toContain('data-i18n-key="ui.data-table.empty"');
|
||||
expect(html).not.toContain("data-row-id=");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user