Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/ui-mock/src/renderFileBrowserMock.test.ts
T
2026-07-08 16:15:37 +02:00

86 lines
3.0 KiB
TypeScript

import { describe, expect, test } from "vitest";
import type { FileBrowserModel } from "@hivegrid/view-models";
import { renderFileBrowserMock } from "./index.js";
const model: FileBrowserModel = {
titleKey: "ui.files.browser.title",
emptyHeadlineKey: "ui.files.browser.empty.headline",
emptyBodyKey: "ui.files.browser.empty.body",
canWrite: true,
rows: [
{
id: "file-1",
displayName: "Quartalsbericht.pdf",
mimeType: "application/pdf",
sizeBytes: 2048,
detailHref: "/files/file-1",
thumbnailUrl: null,
},
{
id: "file-2",
displayName: "logo.png",
mimeType: "image/png",
sizeBytes: 512,
detailHref: "/files/file-2",
thumbnailUrl: "/thumb/file-2",
},
],
rowCount: 2,
};
describe("renderFileBrowserMock (mock impl)", () => {
test("carries the impl marker, title key and one body row per file", () => {
const html = renderFileBrowserMock(model);
expect(html).toContain('data-ui-impl="mock"');
expect(html).toContain('data-ui-id="ui.files.browser"');
expect(html).toContain('data-row-count="2"');
// Header carries the i18n key, never a resolved label.
expect(html).toContain('data-row="head" data-i18n-key="ui.files.browser.title"');
// Exactly two body rows, no empty marker when rows exist.
expect(html.match(/data-row="body"/g)).toHaveLength(2);
expect(html).not.toContain('data-empty="true"');
});
test("emits mime type and raw byte size as data-attributes per row", () => {
const html = renderFileBrowserMock(model);
expect(html).toContain('data-row-id="file-1"');
expect(html).toContain('data-mime="application/pdf"');
expect(html).toContain('data-size="2048"');
expect(html).toContain("Quartalsbericht.pdf");
expect(html).toContain('data-row-id="file-2"');
expect(html).toContain('data-mime="image/png"');
expect(html).toContain('data-size="512"');
});
test("renders the empty headline marker when there are no rows", () => {
const html = renderFileBrowserMock({ ...model, rows: [], rowCount: 0 });
expect(html).toContain('data-row-count="0"');
expect(html).toContain('data-empty="true"');
expect(html).toContain('data-i18n-key="ui.files.browser.empty.headline"');
expect(html).not.toContain('data-row="body"');
});
test("escapes HTML metacharacters in display name and mime type", () => {
const html = renderFileBrowserMock({
...model,
rows: [
{
id: "x&y",
displayName: '<script>"alert"</script>',
mimeType: 'text/x-"weird"',
sizeBytes: 1,
detailHref: "/files/x",
thumbnailUrl: null,
},
],
rowCount: 1,
});
// No raw angle brackets or quotes from user data leak into the markup.
expect(html).toContain("&lt;script&gt;&quot;alert&quot;&lt;/script&gt;");
expect(html).toContain('data-row-id="x&amp;y"');
expect(html).toContain('data-mime="text/x-&quot;weird&quot;"');
expect(html).not.toContain("<script>");
});
});