I am using Material-UI with react to build a Dropdown component.
Dropdown.tsx
import React from "react";
import ClickAwayListener from "@material-ui/core/ClickAwayListener";
import Grow, { GrowProps } from "@material-ui/core/Grow";
import Input from "@material-ui/core/Input";
import MenuList from "@material-ui/core/MenuList";
import Paper from "@material-ui/core/Paper";
import Popper from "@material-ui/core/Popper";
import MenuItem from "@material-ui/core/MenuItem";
export default function Dropdown(props: {
onSearch: (suggestion: string) => void;
}) {
const [searchText, setSearchText] = React.useState("");
const [open, setOpen] = React.useState(false);
let searchElement: null | HTMLElement = null;
const onSearchTextChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setSearchText(event.target.value);
setOpen(true);
};
const openSearchSuggestion = () => {
setOpen(true);
};
const setSearchElement = (node: null | HTMLElement) => {
searchElement = node;
};
const closeSearchSuggestion = () => {
setOpen(false);
};
const handleOnClickAway = (event: React.MouseEvent<EventTarget>) => {
if (searchElement && searchElement.contains(event.target as HTMLElement)) {
return;
}
closeSearchSuggestion();
};
const handleSearchSuggestionClick = (suggestion: string) => (
event: React.MouseEvent<EventTarget>
) => {
props.onSearch(suggestion);
};
const searchSuggestionsRenderer = (suggestion: string, index: number) => {
return (
<MenuItem
key={index}
onClick={handleSearchSuggestionClick(suggestion)}
className="suggestion-item"
id={`suggestion-item-${index}`}
disabled={suggestion === "Criteria 2"}
>
{searchText} in {suggestion}
</MenuItem>
);
};
const popperTrans = ({ TransitionProps }: { TransitionProps: GrowProps }) => {
const searchSuggestions = ["Criteria 1", "Criteria 2"];
return (
<Grow {...TransitionProps} style={{ transformOrigin: "0 0 0" }}>
<Paper className="search-suggestions-paper">
<ClickAwayListener onClickAway={handleOnClickAway}>
<MenuList>
{searchText &&
searchSuggestions &&
searchSuggestions.map(searchSuggestionsRenderer)}
</MenuList>
</ClickAwayListener>
</Paper>
</Grow>
);
};
return (
<div>
<Input
inputRef={setSearchElement}
className="search-input"
placeholder="Search"
value={searchText}
onChange={onSearchTextChange}
onFocus={openSearchSuggestion}
data-testid="searchInput"
/>
<Popper
className="search-suggestion-popper"
open={open && searchText.trim().length >= 2}
anchorEl={searchElement}
placement="bottom-start"
transition={true}
>
{popperTrans}
</Popper>
</div>
);
}
I am now testing this component using react-testing-library. I want to check that the 2nd option in the dropdown is disabled. I tried checking that the onSearch function which is called on click of the menuitem is not called when I click the 2nd option.
test to check option is disabled
test('that criteria 2 option is disabled', () => {
const props = { onSearch: jest.fn() }
const { getAllByRole, getByTestId } = render(<Dropdown {...props} />);
const Input = getByTestId('searchInput');
const TextBox = globalGetByRole(Input, 'textbox') as HTMLInputElement;
fireEvent.change(TextBox, { target: { value: 'test' } });
const MenuItems = getAllByRole('menuitem');
expect(MenuItems.length).toBe(2);
const Criteria2MenuItem = MenuItems[1];
fireEvent.click(Criteria2MenuItem);
expect(props.onSearch).toHaveBeenCalledTimes(0);
});
But this did not work. The function is being called once. Why is this happening and how can I test what I want to test?
When you disable Material-UI MenuItem e.g.
<MenuItem disabled>
{...}
</MenuItem>
It adds aria-disabled="true" and Mui-disabled CSS class to the li element in the DOM. li is not a valid button so you cannot test it by clicking it. You need to check if it has the aria-disabled attribute using .toHaveAttribute() from jest-dom.
expect(Criteria2MenuItem).toHaveAttribute("aria-disabled")
I am using RTL with Jest and This work for me.
const menuitem = screen.getAllByRole('menuitem', { name: 'manue-item-name' });
screen.debug(menuitem);
expect(menuitem[0].getAttribute('aria-disabled')).toBeTruthy();
OR
const menuitem = screen.getAllByRole('menuitem');
screen.debug(menuitem);
expect(menuitem[0].getAttribute('aria-disabled')).toBeTruthy();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With