103 lines
3.8 KiB
TypeScript
103 lines
3.8 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
||
import { mount, flushPromises } from "@vue/test-utils";
|
||
import { defineComponent } from "vue";
|
||
import {
|
||
ComboBox,
|
||
ComboBoxInputGroup,
|
||
ComboBoxPopover,
|
||
ComboBoxTrigger,
|
||
Input,
|
||
ListBox,
|
||
ListBoxItem,
|
||
Autocomplete,
|
||
AutocompleteTrigger,
|
||
AutocompletePopover,
|
||
AutocompleteFilter,
|
||
SearchField,
|
||
SearchFieldInput,
|
||
} from "../packages/vue/src";
|
||
import { createBaseContains } from "../packages/vue/src/components/collection/match";
|
||
const components = {
|
||
ComboBox,
|
||
ComboBoxInputGroup,
|
||
ComboBoxPopover,
|
||
ComboBoxTrigger,
|
||
Input,
|
||
ListBox,
|
||
ListBoxItem,
|
||
Autocomplete,
|
||
AutocompleteTrigger,
|
||
AutocompletePopover,
|
||
AutocompleteFilter,
|
||
SearchField,
|
||
SearchFieldInput,
|
||
};
|
||
const roots: ReturnType<typeof mount>[] = [];
|
||
afterEach(() => {
|
||
roots.splice(0).forEach((w) => w.unmount());
|
||
document.body.innerHTML = "";
|
||
vi.restoreAllMocks();
|
||
});
|
||
function view(template: string) {
|
||
const w = mount(defineComponent({ components, template }), {
|
||
attachTo: document.body,
|
||
});
|
||
roots.push(w);
|
||
return w;
|
||
}
|
||
describe("public picker integration", () => {
|
||
it("retains Vue string template refs on the actual composed Input and clears them on removal", async () => {
|
||
const w = view(
|
||
'<ComboBox><ComboBoxInputGroup><Input ref="consumerInput"/><ComboBoxTrigger/></ComboBoxInputGroup><ComboBoxPopover><ListBox><ListBoxItem id="a">Alpha</ListBoxItem></ListBox></ComboBoxPopover></ComboBox>',
|
||
);
|
||
await flushPromises();
|
||
expect((w.vm.$refs.consumerInput as { $el: Element }).$el).toBe(
|
||
w.get("input[role=combobox]").element,
|
||
);
|
||
w.unmount();
|
||
roots.splice(roots.indexOf(w), 1);
|
||
expect(w.vm.$refs.consumerInput).toBeNull();
|
||
});
|
||
it("retains a string template ref on the teleported search input", async () => {
|
||
const w = view(
|
||
'<Autocomplete default-open><AutocompleteTrigger>Choose</AutocompleteTrigger><AutocompletePopover><AutocompleteFilter><SearchField><SearchFieldInput ref="consumerSearch"/></SearchField><ListBox><ListBoxItem id="a">Alpha</ListBoxItem></ListBox></AutocompleteFilter></AutocompletePopover></Autocomplete>',
|
||
);
|
||
await flushPromises();
|
||
expect((w.vm.$refs.consumerSearch as { $el: Element }).$el).toBe(
|
||
document.querySelector("input[role=combobox]"),
|
||
);
|
||
});
|
||
it("preserves an accessible name declared on the inner SearchField root", async () => {
|
||
view(
|
||
'<Autocomplete default-open aria-label="Picker"><AutocompleteTrigger>Choose</AutocompleteTrigger><AutocompletePopover><AutocompleteFilter><SearchField aria-label="Search options"><SearchFieldInput/></SearchField><ListBox><ListBoxItem id="a">Alpha</ListBoxItem></ListBox></AutocompleteFilter></AutocompletePopover></Autocomplete>',
|
||
);
|
||
await flushPromises();
|
||
expect(
|
||
document
|
||
.querySelector("input[role=combobox]")
|
||
?.getAttribute("aria-label"),
|
||
).toBe("Search options");
|
||
});
|
||
it("respects a caller-disabled Autocomplete trigger before opening", async () => {
|
||
const w = view(
|
||
'<Autocomplete><AutocompleteTrigger disabled>Choose</AutocompleteTrigger><AutocompletePopover><ListBox><ListBoxItem id="a">Alpha</ListBoxItem></ListBox></AutocompletePopover></Autocomplete>',
|
||
);
|
||
await w.get('[data-slot="autocomplete-trigger"]').trigger("click");
|
||
expect(
|
||
w.get('[data-slot="autocomplete-trigger"]').attributes("aria-expanded"),
|
||
).toBe("false");
|
||
expect(
|
||
w.get('[data-slot="autocomplete-trigger"]').attributes("aria-disabled"),
|
||
).toBe("true");
|
||
});
|
||
});
|
||
it.each([
|
||
["tr", "İstanbul", "istanbul", true],
|
||
["tr", "Isparta", "ısparta", true],
|
||
["tr", "Isparta", "isparta", false],
|
||
["de", "Fußgänger", "fuss", true],
|
||
["en", "Café", "cafe\u0301", true],
|
||
])("locale filter %s: %s / %s", (locale, text, query, expected) => {
|
||
expect(createBaseContains(locale)(text, query)).toBe(expected);
|
||
});
|