57 lines
2.1 KiB
TypeScript
57 lines
2.1 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
import type { DataTableVM } from "@toolctrl/view-models";
|
|
import { renderDataTableMock } from "@toolctrl/ui-mock";
|
|
|
|
import { dataTableAdapter } from "./dataTable.js";
|
|
|
|
const vm: DataTableVM = {
|
|
caption: { labelKey: "ui.data-table.caption" },
|
|
columns: [
|
|
{ key: "name", labelKey: "ui.data-table.column.name" },
|
|
{ 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" } },
|
|
],
|
|
};
|
|
|
|
describe("dataTableAdapter hot-swap (M017)", () => {
|
|
test("normalizes the VM once via toProps", () => {
|
|
const model = dataTableAdapter.toProps(vm);
|
|
expect(model.rowCount).toBe(2);
|
|
expect(model.captionKey).toBe("ui.data-table.caption");
|
|
expect(model.columns.map((column) => column.align)).toEqual(["left", "right"]);
|
|
});
|
|
|
|
test("swaps the render implementation without changing the data", () => {
|
|
const defaultHtml = dataTableAdapter.render(vm);
|
|
const mockHtml = dataTableAdapter.render(vm, renderDataTableMock);
|
|
|
|
expect(defaultHtml.length).toBeGreaterThan(0);
|
|
expect(mockHtml.length).toBeGreaterThan(0);
|
|
|
|
// Chrome differs: only the implementation marker swaps.
|
|
expect(defaultHtml).toContain('data-ui-impl="subframe"');
|
|
expect(defaultHtml).not.toContain('data-ui-impl="mock"');
|
|
expect(mockHtml).toContain('data-ui-impl="mock"');
|
|
expect(mockHtml).not.toContain('data-ui-impl="subframe"');
|
|
expect(defaultHtml).not.toBe(mockHtml);
|
|
|
|
// Data is identical across implementations: all row ids and cell values present in both.
|
|
for (const html of [defaultHtml, mockHtml]) {
|
|
for (const row of vm.rows) {
|
|
expect(html).toContain(`data-row-id="${row.id}"`);
|
|
for (const value of Object.values(row.cells)) {
|
|
expect(html).toContain(value);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
test("the default renderer is the real subframe implementation", () => {
|
|
expect(dataTableAdapter.name).toBe("dataTable");
|
|
expect(dataTableAdapter.render(vm)).toContain('data-ui-id="ui.data-table"');
|
|
});
|
|
});
|