Skip to content

Commit 0ea3d93

Browse files
committed
test(multi-select): add more unit tests
1 parent bfe86be commit 0ea3d93

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

tests/MultiSelect/MultiSelect.test.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
) => item.text;
2626
export let itemToInput: ComponentProps<MultiSelect>["itemToInput"] =
2727
undefined;
28+
export let helperText: ComponentProps<MultiSelect>["helperText"] = "";
2829
</script>
2930

3031
<MultiSelect
@@ -46,6 +47,7 @@
4647
{translateWithIdSelection}
4748
{itemToString}
4849
{itemToInput}
50+
{helperText}
4951
on:select={(e) => {
5052
console.log("select", e.detail);
5153
}}

tests/MultiSelect/MultiSelect.test.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,114 @@ describe("MultiSelect", () => {
424424
expect(checkboxInput).toHaveAttribute("value", "slack");
425425
});
426426
});
427+
428+
it("does not show helper text if warn is true", () => {
429+
render(MultiSelect, {
430+
props: {
431+
items,
432+
helperText: "Help",
433+
warn: true,
434+
},
435+
});
436+
expect(screen.queryByText("Help")).not.toBeInTheDocument();
437+
});
438+
439+
it("does not show helper text if invalid is true", () => {
440+
render(MultiSelect, {
441+
props: {
442+
items,
443+
helperText: "Help",
444+
invalid: true,
445+
},
446+
});
447+
expect(screen.queryByText("Help")).not.toBeInTheDocument();
448+
});
449+
450+
it("does not show helper text if inline is true", () => {
451+
render(MultiSelect, {
452+
props: {
453+
items,
454+
helperText: "Help",
455+
type: "inline",
456+
},
457+
});
458+
expect(screen.queryByText("Help")).not.toBeInTheDocument();
459+
});
460+
461+
it("clears all selections when clear button is clicked", async () => {
462+
render(MultiSelect, {
463+
props: {
464+
items,
465+
selectedIds: ["0", "1"],
466+
},
467+
});
468+
await user.click(screen.getAllByRole("button")[0]);
469+
470+
const options = screen.getAllByRole("option");
471+
expect(options[0]).toHaveAttribute("aria-selected", "true");
472+
expect(options[1]).toHaveAttribute("aria-selected", "true");
473+
expect(options[2]).toHaveAttribute("aria-selected", "false");
474+
475+
const clearButton = screen.getByRole("button", { name: /clear/i });
476+
await user.click(clearButton);
477+
await user.click(screen.getByRole("button"));
478+
479+
expect(options[0]).toHaveAttribute("aria-selected", "false");
480+
expect(options[1]).toHaveAttribute("aria-selected", "false");
481+
expect(options[2]).toHaveAttribute("aria-selected", "false");
482+
});
483+
484+
it("skips disabled items during keyboard navigation", async () => {
485+
render(MultiSelect, {
486+
props: {
487+
items: [
488+
{ id: "1", text: "Aa" },
489+
{ id: "2", text: "Ba", disabled: true },
490+
{ id: "3", text: "Ca" },
491+
],
492+
filterable: true,
493+
placeholder: "Filter...",
494+
},
495+
});
496+
await user.click(screen.getByRole("button"));
497+
const input = screen.getByPlaceholderText("Filter...");
498+
499+
await user.type(input, "a");
500+
await user.keyboard("{ArrowDown}");
501+
await user.keyboard("{ArrowDown}");
502+
await user.keyboard("{Enter}");
503+
504+
const options = screen.getAllByRole("option");
505+
expect(options[2]).toHaveAttribute("aria-selected", "true");
506+
});
507+
508+
it("focuses input when filterable and menu is opened", async () => {
509+
render(MultiSelect, {
510+
props: {
511+
items,
512+
filterable: true,
513+
placeholder: "Filter...",
514+
},
515+
});
516+
await user.click(screen.getByRole("button"));
517+
const input = screen.getByPlaceholderText("Filter...");
518+
expect(input).toHaveFocus();
519+
});
520+
521+
it("does not select disabled items when clicked", async () => {
522+
render(MultiSelect, {
523+
props: {
524+
items: [
525+
{ id: "1", text: "A" },
526+
{ id: "2", text: "B", disabled: true },
527+
{ id: "3", text: "C" },
528+
],
529+
},
530+
});
531+
await user.click(screen.getByRole("button"));
532+
const disabledOption = screen.getByText("B").closest("[role='option']");
533+
534+
await user.click(disabledOption!);
535+
expect(disabledOption).toHaveAttribute("aria-selected", "false");
536+
});
427537
});

0 commit comments

Comments
 (0)