Files
heroui-vue/tests/regressions/toast-table/empty-state-and-labels.test.ts
T

289 lines
8.6 KiB
TypeScript

import { afterEach, expect, it } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h, ref } from "vue";
import {
Table,
TableContent,
TableBody,
TableCollection,
TableRow,
TableCell,
ToastProvider,
ToastQueue,
createToastFunction,
ToastContent,
ToastTitle,
ToastDescription,
} from "../../../packages/vue/src";
const cleanup: (() => void)[] = [];
afterEach(() => {
cleanup
.splice(0)
.reverse()
.forEach((fn) => fn());
document.body.innerHTML = "";
});
it("TableBody preserves a populated TableCollection when #empty is supplied", () => {
const w = mount(
defineComponent({
setup: () => () =>
h(Table, {}, () =>
h(TableContent, {}, () =>
h(
TableBody,
{},
{
default: () =>
h(
TableCollection,
{ items: ["A"] },
{
default: ({ item }: { item: string }) =>
h(TableRow, { rowKey: item }, () =>
h(TableCell, {}, () => item),
),
},
),
empty: () => h("span", { "data-test": "empty" }, "Empty"),
},
),
),
),
}),
);
cleanup.push(() => w.unmount());
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(1);
expect(w.find('[data-test="empty"]').exists()).toBe(false);
});
it("Toast root follows explicit Title and Description ids", async () => {
const q = new ToastQueue();
cleanup.push(() => q.destroy());
const toast = createToastFunction(q);
const w = mount(
defineComponent({
setup: () => () =>
h(
ToastProvider,
{ queue: q, to: "body" },
{
default: () =>
h(ToastContent, {}, () => [
h(ToastTitle, { id: "custom-title" }, () => "Title"),
h(
ToastDescription,
{ id: "custom-description" },
() => "Description",
),
]),
},
),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
toast("Title", { timeout: 0 });
await flushPromises();
const root = document.querySelector('[data-slot="toast"]')!;
expect(root.getAttribute("aria-labelledby")).toBe("custom-title");
expect(root.getAttribute("aria-describedby")).toBe("custom-description");
});
it("title-only Toast does not reference a nonexistent description", async () => {
const q = new ToastQueue();
cleanup.push(() => q.destroy());
const toast = createToastFunction(q);
const w = mount(ToastProvider, {
props: { queue: q, to: "body" },
attachTo: document.body,
});
cleanup.push(() => w.unmount());
toast("Title only", { timeout: 0 });
await flushPromises();
const root = document.querySelector('[data-slot="toast"]')!;
const refs = (root.getAttribute("aria-describedby") ?? "")
.split(/\s+/)
.filter(Boolean);
for (const id of refs) expect(document.getElementById(id)).not.toBeNull();
});
it("Toast part ids react to later id attribute changes", async () => {
const title = ref("title-before");
const description = ref("description-before");
const q = new ToastQueue();
cleanup.push(() => q.destroy());
const w = mount(
defineComponent({
setup: () => () =>
h(
ToastProvider,
{ queue: q, to: "body" },
{
default: () =>
h(ToastContent, {}, () => [
h(ToastTitle, { id: title.value }, () => "Title"),
h(
ToastDescription,
{ id: description.value },
() => "Description",
),
]),
},
),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
createToastFunction(q)("Title", { timeout: 0 });
await flushPromises();
title.value = "title-after";
description.value = "description-after";
await flushPromises();
const root = document.querySelector('[data-slot="toast"]')!;
expect(root.querySelector('[data-slot="toast-title"]')?.id).toBe(
"title-after",
);
expect(root.getAttribute("aria-labelledby")).toBe("title-after");
expect(root.querySelector('[data-slot="toast-description"]')?.id).toBe(
"description-after",
);
expect(root.getAttribute("aria-describedby")).toBe("description-after");
});
it("wrapped collection transitions back to empty without swallowing its future rows", async () => {
const items = ref<string[]>(["A"]);
const Rows = defineComponent({
name: "Rows",
setup: () => () =>
h(
TableCollection,
{ items: items.value },
{
default: ({ item }: { item: string }) =>
h(TableRow, { rowKey: item }, () => h(TableCell, {}, () => item)),
},
),
});
const w = mount(
defineComponent({
setup: () => () =>
h(Table, {}, () =>
h(TableContent, {}, () =>
h(
TableBody,
{},
{
default: () => h(Rows),
empty: () => h("span", { "data-test": "empty" }, "Empty"),
},
),
),
),
}),
);
cleanup.push(() => w.unmount());
await flushPromises();
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(1);
items.value = [];
await flushPromises();
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(0);
expect(w.find('[data-test="empty"]').exists()).toBe(true);
items.value = ["B"];
await flushPromises();
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(1);
expect(w.find('[data-test="empty"]').exists()).toBe(false);
});
it("forced empty state does not expose existing data rows", async () => {
const w = mount(
defineComponent({
setup: () => () =>
h(Table, {}, () =>
h(TableContent, {}, () =>
h(
TableBody,
{ empty: true },
{
default: () =>
h(TableRow, { rowKey: "A" }, () =>
h(TableCell, {}, () => "A"),
),
empty: () => h("span", { "data-test": "empty" }, "Empty"),
},
),
),
),
}),
);
cleanup.push(() => w.unmount());
await flushPromises();
expect(w.find('[data-test="empty"]').exists()).toBe(true);
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(0);
});
it("observes native rows rendered by an anonymous wrapper as data changes", async () => {
const items = ref<string[]>([]);
const Rows = () =>
items.value.map((item) => h("tr", { key: item }, [h("td", item)]));
const w = mount(
defineComponent({
setup: () => () =>
h(Table, {}, () =>
h(TableContent, {}, () =>
h(
TableBody,
{},
{
default: () => h(Rows),
empty: () => h("span", { "data-test": "empty" }, "Empty"),
},
),
),
),
}),
);
cleanup.push(() => w.unmount());
await flushPromises();
expect(w.find('[data-test="empty"]').exists()).toBe(true);
items.value = ["A"];
await flushPromises();
expect(w.find('[data-test="empty"]').exists()).toBe(false);
expect(w.find("tbody").text()).toBe("A");
items.value = [];
await flushPromises();
expect(w.find('[data-test="empty"]').exists()).toBe(true);
});
it("switches forced empty off and restores the data row", async () => {
const empty = ref(false);
const w = mount(
defineComponent({
setup: () => () =>
h(Table, {}, () =>
h(TableContent, { selectionMode: "multiple" }, () =>
h(
TableBody,
{ empty: empty.value },
{
default: () =>
h(TableRow, { rowKey: "A" }, () =>
h(TableCell, {}, () => "A"),
),
empty: () => h("span", { "data-test": "empty" }, "Empty"),
},
),
),
),
}),
);
cleanup.push(() => w.unmount());
await flushPromises();
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(1);
empty.value = true;
await flushPromises();
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(0);
expect(w.find('[data-test="empty"]').exists()).toBe(true);
empty.value = false;
await flushPromises();
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(1);
expect(w.find('[data-test="empty"]').exists()).toBe(false);
});