feat: establish private collection selection foundation
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
// @vitest-environment node
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createSSRApp, defineComponent, h } from "vue";
|
||||
import { renderToString } from "@vue/server-renderer";
|
||||
import Foundation from "../packages/vue/tests/fixtures/CollectionFoundation.vue";
|
||||
import SelectFoundation from "../packages/vue/tests/fixtures/SelectFoundation.vue";
|
||||
import { useCollectionRegistry } from "../packages/vue/src/components/collection/registry";
|
||||
import { useCollectionSelection } from "../packages/vue/src/components/collection/selection";
|
||||
|
||||
describe("collection foundation in real node SSR", () => {
|
||||
it("renders typed selection without browser globals", async () => {
|
||||
expect(typeof window).toBe("undefined");
|
||||
const html = await renderToString(
|
||||
createSSRApp(Foundation, { multiple: true, modelValue: [0] }),
|
||||
);
|
||||
expect(html).toContain('role="listbox"');
|
||||
expect(html.match(/aria-selected="true"/g)).toHaveLength(1);
|
||||
expect(html.match(/role="option"/g)).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("keeps instance registries isolated with no DOM access", async () => {
|
||||
const Component = defineComponent({
|
||||
setup() {
|
||||
const registry = useCollectionRegistry();
|
||||
registry.register({
|
||||
key: () => 0,
|
||||
element: () => undefined,
|
||||
textValue: () => "Zero",
|
||||
disabled: () => false,
|
||||
});
|
||||
const state = useCollectionSelection({
|
||||
mode: () => "single",
|
||||
controlled: () => false,
|
||||
value: () => undefined,
|
||||
defaultValue: [0],
|
||||
onChange: () => {},
|
||||
});
|
||||
return () =>
|
||||
h("output", `${registry.ordered().length}:${state.keys.value[0]}`);
|
||||
},
|
||||
});
|
||||
const html = await renderToString(
|
||||
createSSRApp({ render: () => h("main", [h(Component), h(Component)]) }),
|
||||
);
|
||||
expect(html).toBe("<main><output>1:0</output><output>1:0</output></main>");
|
||||
});
|
||||
|
||||
it("records that raw closed Select cannot SSR its selected item label", async () => {
|
||||
const html = await renderToString(
|
||||
createSSRApp(SelectFoundation, { defaultValue: 0, name: "choice" }),
|
||||
);
|
||||
expect(html).toContain('role="combobox"');
|
||||
expect(html).not.toContain("Numeric zero");
|
||||
// ItemText registers options on mount; SSR cannot discover closed descendants.
|
||||
expect(html).not.toContain('<option value="0"');
|
||||
});
|
||||
|
||||
it("records the raw Select empty-string key restriction", async () => {
|
||||
const warn = console.warn;
|
||||
console.warn = () => {};
|
||||
try {
|
||||
await expect(
|
||||
renderToString(
|
||||
createSSRApp(SelectFoundation, { includeEmpty: true, open: true }),
|
||||
),
|
||||
).rejects.toThrow("empty string");
|
||||
} finally {
|
||||
console.warn = warn;
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user