import { afterEach, expect, it, vi } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { defineComponent, h, nextTick, ref } from "vue"; import { Table, TableBody, TableCell, TableColumn, TableContent, TableHeader, TableRow, TableScrollContainer, TableSortableColumnHeader, } from "../packages/vue/src"; afterEach(async () => { await flushPromises(); document.body.innerHTML = ""; }); function mountTable(props: Record = {}, rows = ["1", "2"]) { return mount( defineComponent({ setup: () => ({ props, rows }), render: () => h(TableScrollContainer, () => h(Table, props, () => h(TableContent, () => [ h(TableHeader, () => [ h(TableColumn, { id: "name", allowsSorting: true }, () => h( TableSortableColumnHeader, { sortDirection: undefined }, () => "名", ), ), ]), h(TableBody, () => rows.map((id) => h(TableRow, { rowKey: id }, () => h(TableCell, () => `row-${id}`), ), ), ), ]), ), ), }), { attachTo: document.body }, ); } it("renders table semantics and data-slots", () => { const w = mountTable(); expect(w.find('[data-slot="table"]').exists()).toBe(true); expect(w.find("table").exists()).toBe(true); expect(w.findAll('[data-slot="table-row"]')).toHaveLength(2); expect(w.find('[data-slot="table-sortable-column-header"]').exists()).toBe( true, ); w.unmount(); }); it("selection toggles and respects disabledKeys", async () => { const selected = ref<(string | number)[]>([]); const w = mount( defineComponent({ setup: () => ({ selected }), render: () => h(Table, () => h( TableContent, { selectionMode: "multiple", selectedKeys: selected.value, disabledKeys: ["2"], "onUpdate:selectedKeys": (k: (string | number)[]) => { selected.value = k; }, }, () => h(TableBody, () => ["1", "2"].map((id) => h(TableRow, { rowKey: id }, () => h(TableCell, () => id)), ), ), ), ), }), { attachTo: document.body }, ); await w.findAll('[data-slot="table-row"]')[0].trigger("click"); await nextTick(); expect(selected.value).toEqual(["1"]); await w.findAll('[data-slot="table-row"]')[1].trigger("click"); await nextTick(); expect(selected.value).toEqual(["1"]); await w.findAll('[data-slot="table-row"]')[0].trigger("click"); await nextTick(); expect(selected.value).toEqual([]); w.unmount(); }); it("sortable header updates sort direction and emits sortChange", async () => { const w = mountTable(); const col = w.get('[data-slot="table-column"]'); expect(col.attributes("data-allows-sorting")).toBe("true"); expect( w .get('[data-slot="table-sortable-column-header"]') .attributes("data-direction"), ).toBeUndefined(); await col.trigger("click"); await nextTick(); expect( w .get('[data-slot="table-sortable-column-header"]') .attributes("data-direction"), ).toBe("ascending"); await col.trigger("click"); await nextTick(); expect( w .get('[data-slot="table-sortable-column-header"]') .attributes("data-direction"), ).toBe("descending"); w.unmount(); }); it("mounts all 13 demos without Vue warnings", async () => { const names = [ "basic", "secondary-variant", "async-loading", "sorting", "selection", "expandable-rows", "pagination", "column-resizing", "empty-state", "virtualization", "tanstack-table", "custom-cells", "custom-styles", ]; expect(names).toHaveLength(13); const warnings: string[] = []; const spy = vi.spyOn(console, "warn").mockImplementation((...args) => { warnings.push(args.map(String).join(" ")); }); for (const name of names) { const mod = await import(`../apps/docs/demos/table-${name}.vue`); const w = mount(mod.default, { attachTo: document.body }); await nextTick(); expect(w.html().length).toBeGreaterThan(0); w.unmount(); } spy.mockRestore(); expect(warnings).toEqual([]); }); it("controlled sort-descriptor updates via v-model", async () => { const descriptor = ref<{ column: string; direction: "ascending" | "descending"; } | null>(null); const w = mount( defineComponent({ setup: () => ({ descriptor }), render: () => h(Table, () => h( TableContent, { sortDescriptor: descriptor.value, "onUpdate:sortDescriptor": (d: any) => { descriptor.value = d; }, }, () => h(TableHeader, () => h(TableRow, { rowKey: "head" }, () => h(TableColumn, { id: "name", allowsSorting: true }, () => h( TableSortableColumnHeader, { sortDirection: descriptor.value?.direction as "ascending" | "descending" | undefined, }, () => "名", ), ), ), ), ), ), }), { attachTo: document.body }, ); await w.find('[data-slot="table-column"]').trigger("click"); await nextTick(); expect(descriptor.value).toEqual({ column: "name", direction: "ascending" }); w.unmount(); });