Skip to content

Commit ea7d14c

Browse files
authored
test(combobox): add test for combobox for new filterMethod prop (#258)
2 parents ec4d659 + 323c996 commit ea7d14c

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/Combobox/Combobox.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ export interface ComboboxProps extends Omit<FormControlProps, 'type'> {
3232
/** Adds a FormLabel to `<Combobox />` */
3333
label?: string;
3434
/** Adds icon defined to FormControl */
35-
icon: React.ReactElement;
35+
icon?: React.ReactElement;
3636
/** Enable y-axis scrolling of a menu with a default max-height of 480px */
3737
scrollable?: boolean;
38-
/** Filter method of the combobox. Defaults to 'startsWith'. Apply */
38+
/** Filter method of the combobox. Defaults to 'startsWith'. Applying custom filter function to apply your custom logic filter
39+
* `type CustomFilter = (inputValue: string, menuItems: string[]) => string[]`
40+
*/
3941
filterMethod?: 'startsWith' | 'includes' | CustomFilter;
4042
}
4143

tests/Combobox/Combobox.test.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,52 @@ describe('<Combobox>', () => {
483483
)
484484
).toBeInTheDocument();
485485
});
486+
it('For filterMethod=includes, when gh is typed matches one of menuList, country filtered menuList should show only strings including gh. Results in 2', async () => {
487+
const { container } = render(
488+
<Combobox menuList={menuList} filterMethod="includes" />
489+
);
490+
491+
fireEvent.change(
492+
container.querySelector('input.form-control.dropdown-toggle')!,
493+
{ target: { value: 'gh' } }
494+
);
495+
expect(container.querySelector('input')?.value).toEqual('gh');
496+
497+
fireEvent.click(
498+
container.querySelector('input.form-control.dropdown-toggle')!
499+
);
500+
await waitFor(() => {
501+
expect(container.querySelector('ul.dropdown-menu')).toBeInTheDocument();
502+
const dropdownItem = container.querySelectorAll("li>button.dropdown-item")
503+
expect(dropdownItem.length).toEqual(2);
504+
expect(dropdownItem[0].textContent).toEqual('Afghanistan');
505+
expect(dropdownItem[1].textContent).toEqual('Ghana');
506+
});
507+
});
508+
it('For custom filterMethod, custom filter behaviour is applied instead', async () => {
509+
const customFilter = (inputValue: string, menuItems: string[]) => {
510+
const filtered = menuItems.filter((n) => {
511+
const nLowerCase = n.toLowerCase();
512+
const valueLower = inputValue.toLowerCase();
513+
return nLowerCase.endsWith(valueLower);
514+
});
515+
return filtered;
516+
};
517+
const { container } = render(
518+
<Combobox menuList={["apple", "orange", "banana"]} filterMethod={customFilter} />
519+
);
520+
521+
fireEvent.change(
522+
container.querySelector('input.form-control.dropdown-toggle')!,
523+
{ target: { value: 'e' } }
524+
);
525+
expect(container.querySelector('input')?.value).toEqual('e');
526+
await waitFor(() => {
527+
expect(container.querySelector('ul.dropdown-menu')).toBeInTheDocument();
528+
const dropdownItem = container.querySelectorAll("li>button.dropdown-item")
529+
expect(dropdownItem.length).toEqual(2);
530+
expect(dropdownItem[0].textContent).toEqual('apple');
531+
expect(dropdownItem[1].textContent).toEqual('orange');
532+
});
533+
});
486534
});

0 commit comments

Comments
 (0)