Skip to content

Commit 7785e9a

Browse files
committed
feat(select): add tests for onChange
1 parent 528d894 commit 7785e9a

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

packages/components/select/__tests__/select.test.tsx

+68
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,74 @@ describe("Select", () => {
652652
// assert that the select listbox is closed
653653
expect(select).toHaveAttribute("aria-expanded", "false");
654654
});
655+
656+
it("should work with onChange (< 300 select items)", async () => {
657+
const onChange = jest.fn();
658+
659+
let options = new Array(10).fill("");
660+
661+
options = options.map((_, i) => {
662+
return `option ${i}`;
663+
});
664+
665+
const wrapper = render(
666+
<Select isOpen aria-label="Favorite Animal" label="Favorite Animal" onChange={onChange}>
667+
{options.map((o) => (
668+
<SelectItem key={o} value={o}>
669+
{o}
670+
</SelectItem>
671+
))}
672+
</Select>,
673+
);
674+
675+
let listbox = wrapper.getByRole("listbox");
676+
677+
expect(listbox).toBeTruthy();
678+
679+
let listboxItems = wrapper.getAllByRole("option");
680+
681+
expect(listboxItems.length).toBe(10);
682+
683+
await act(async () => {
684+
await user.click(listboxItems[1]);
685+
686+
expect(onChange).toBeCalledTimes(1);
687+
});
688+
});
689+
690+
it("should work with onChange (>= 300 select items)", async () => {
691+
let onChange = jest.fn();
692+
693+
let options = new Array(300).fill("");
694+
695+
options = options.map((_, i) => {
696+
return `option ${i}`;
697+
});
698+
699+
const wrapper = render(
700+
<Select isOpen aria-label="Favorite Animal" label="Favorite Animal" onChange={onChange}>
701+
{options.map((o) => (
702+
<SelectItem key={o} value={o}>
703+
{o}
704+
</SelectItem>
705+
))}
706+
</Select>,
707+
);
708+
709+
let listbox = wrapper.getByRole("listbox");
710+
711+
expect(listbox).toBeTruthy();
712+
713+
let listboxItems = wrapper.getAllByRole("option");
714+
715+
expect(listboxItems.length).toBe(300);
716+
717+
await act(async () => {
718+
await user.click(listboxItems[1]);
719+
720+
expect(onChange).toBeCalledTimes(1);
721+
});
722+
});
655723
});
656724

657725
describe("Select with React Hook Form", () => {

0 commit comments

Comments
 (0)