【发布时间】:2021-02-02 16:34:02
【问题描述】:
如何测试按钮中添加的图标?此按钮触发打开菜单。 menuTriggerLabel 是常量。
import { Button } from '@material-ui/core';
import { ExpandLess, ExpandMore } from '@material-ui/icons';
import React from 'react';
export const myMenuTriggerLabel = 'My menu';
export const MyMenu = () => {
const [open, setOpen] = React.useState(false);
const handleToggle = () => {
setOpen((prevOpen) => !prevOpen);
};
return (
<div>
<Button onClick={handleToggle} endIcon={open ? <ExpandLess /> : <ExpandMore />}>
{myMenuTriggerLabel}
</Button>
</div>
);
};
我想测试图标是否为ExpandMore,点击按钮,测试图标是否为ExpandLess,再次点击按钮,测试图标是否为ExpandMore。
it('should render proper icon on show and hide', async () => {
const component = render(<MyMenu />);
const icon = component.getByText(menuTriggerLabel)
???
我知道如何点击按钮。我不知道如何断言图标。
【问题讨论】:
-
<ExpandLess />和<ExpandMore />的代码是什么?我想您只需将打开图标放在一个render()中,将关闭图标放在另一个render()中,就可以区分打开和关闭。 -
你能添加一个jsfiddle,codesnipt链接吗?
-
@M.M.H.Masud 我已经编辑了问题并添加了完整的
MyMenu组件源。
标签: reactjs material-ui react-testing-library react-material