mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
test(combo-box): more unit tests
This commit is contained in:
parent
9e3d83031e
commit
c67e095eaf
2 changed files with 36 additions and 4 deletions
|
@ -141,12 +141,24 @@ describe("ComboBox", () => {
|
||||||
await user.type(input, "a");
|
await user.type(input, "a");
|
||||||
await user.click(screen.getAllByRole("option")[1]);
|
await user.click(screen.getAllByRole("option")[1]);
|
||||||
expect(input).toHaveValue("Email");
|
expect(input).toHaveValue("Email");
|
||||||
|
|
||||||
await user.click(document.body);
|
await user.click(document.body);
|
||||||
expect(input).not.toHaveFocus();
|
expect(input).not.toHaveFocus();
|
||||||
expect(screen.queryByRole("option")).not.toBeInTheDocument();
|
expect(screen.queryByRole("option")).not.toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should clear input when clicking clear button", async () => {
|
||||||
|
const consoleLog = vi.spyOn(console, "log");
|
||||||
|
render(ComboBox, { props: { selectedId: "1" } });
|
||||||
|
|
||||||
|
expect(consoleLog).not.toBeCalled();
|
||||||
|
const clearButton = screen.getByRole("button", { name: /clear/i });
|
||||||
|
await user.click(clearButton);
|
||||||
|
|
||||||
|
expect(screen.getByRole("textbox")).toHaveValue("");
|
||||||
|
expect(consoleLog).toHaveBeenCalledWith("clear", "clear");
|
||||||
|
});
|
||||||
|
|
||||||
it("should handle disabled items", async () => {
|
it("should handle disabled items", async () => {
|
||||||
render(ComboBoxCustom);
|
render(ComboBoxCustom);
|
||||||
|
|
||||||
|
@ -184,9 +196,26 @@ describe("ComboBox", () => {
|
||||||
it("should programmatically clear selection", async () => {
|
it("should programmatically clear selection", async () => {
|
||||||
render(ComboBoxCustom, { props: { selectedId: "1" } });
|
render(ComboBoxCustom, { props: { selectedId: "1" } });
|
||||||
|
|
||||||
expect(screen.getByRole("textbox")).toHaveValue("Email");
|
const textbox = screen.getByRole("textbox");
|
||||||
|
expect(textbox).toHaveValue("Email");
|
||||||
await user.click(screen.getByText("Clear"));
|
await user.click(screen.getByText("Clear"));
|
||||||
expect(screen.getByRole("textbox")).toHaveValue("");
|
expect(textbox).toHaveValue("");
|
||||||
|
expect(textbox).toHaveFocus();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not re-focus textbox if clearOptions.focus is false", async () => {
|
||||||
|
render(ComboBoxCustom, {
|
||||||
|
props: {
|
||||||
|
selectedId: "1",
|
||||||
|
clearOptions: { focus: false },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const textbox = screen.getByRole("textbox");
|
||||||
|
expect(textbox).toHaveValue("Email");
|
||||||
|
await user.click(screen.getByText("Clear"));
|
||||||
|
expect(textbox).toHaveValue("");
|
||||||
|
expect(textbox).not.toHaveFocus();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should close menu on Escape key", async () => {
|
it("should close menu on Escape key", async () => {
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
];
|
];
|
||||||
export let selectedId: string | undefined = undefined;
|
export let selectedId: string | undefined = undefined;
|
||||||
export let direction: "top" | "bottom" = "bottom";
|
export let direction: "top" | "bottom" = "bottom";
|
||||||
|
export let clearOptions: { focus?: boolean } = {};
|
||||||
export let shouldFilterItem = (item: ComboBoxItem, value: string) =>
|
export let shouldFilterItem = (item: ComboBoxItem, value: string) =>
|
||||||
item.text.toLowerCase().includes(value.toLowerCase());
|
item.text.toLowerCase().includes(value.toLowerCase());
|
||||||
export let itemToString = (item: ComboBoxItem) => item.text;
|
export let itemToString = (item: ComboBoxItem) => item.text;
|
||||||
|
@ -33,4 +34,6 @@
|
||||||
<span>Item {item.text}</span>
|
<span>Item {item.text}</span>
|
||||||
</ComboBox>
|
</ComboBox>
|
||||||
|
|
||||||
<button type="button" on:click={() => comboBoxRef.clear()}>Clear</button>
|
<button type="button" on:click={() => comboBoxRef.clear(clearOptions)}>
|
||||||
|
Clear
|
||||||
|
</button>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue