Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs SVG格式的测试图标_Reactjs_Material Ui_React Testing Library_React Material - Fatal编程技术网

Reactjs SVG格式的测试图标

Reactjs SVG格式的测试图标,reactjs,material-ui,react-testing-library,react-material,Reactjs,Material Ui,React Testing Library,React Material,如何测试按钮中添加的图标?此按钮触发打开菜单菜单触发器标签为常量 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 = () => { con

如何测试按钮中添加的图标?此按钮触发打开菜单<代码>菜单触发器标签为常量

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>
  );
};

我知道如何点击按钮。我不知道如何断言图标。

我不熟悉react测试库。 但我支持开玩笑。主要思想是通过组件选择来测试它

这可能有点像开玩笑+恩齐姆

const checkOpenState = (tree, state) => {
    expect(!!tree.find(ExpandLess).length).toBe(state)
};
const checkCloseState = (tree, state) => {
   expect(!!tree.find(ExpandMore).length).toBe(state)
};
const toggleState = (tree) => {
   tree.find(Button).props().onClick();
}
it('should render proper icon on show and hide', async () => {
  const component = shallow(<MyMenu />);
  checkOpenState(component, false);
  checkCloseState(component, true);

  toggleState(component);

  checkOpenState(component, true);
  checkCloseState(component, false);
constcheckopenstate=(树,状态)=>{
expect(!!tree.find(ExpandLess.length).toBe(state)
};
const checkCloseState=(树,状态)=>{
expect(!!tree.find(ExpandMore.length).toBe(state)
};
常量切换状态=(树)=>{
tree.find(Button.props().onClick();
}
它('应该在显示和隐藏时呈现正确的图标',异步()=>{
常量分量=浅();
checkOpenState(组件,false);
checkCloseState(组件,真);
切换状态(组件);
checkOpenState(组件,true);
检查关闭状态(组件,错误);

对于react测试库,您可以查看doc并调整我的解决方案)

什么是
的代码?我想您只需将打开图标放在一个的
render()
中,将关闭图标放在另一个的
render()中,即可区分打开和关闭
。你能添加一个jsfiddle,codesnipt链接吗?@M.M.H.Masud我已经编辑了这个问题,并在生成的DOM中添加了完整的
MyMenu
组件源代码(查看浏览器控制台)没有可能找到
ExpandLess
ExpandMore
,因此我在测试期间找不到它们,因为RTL只看到组件的DOM。在这种情况下,您可以检查prop'endIcon'以获得正确的值吗?我不知道如何使用React测试库进行此操作(我将在明天更新React测试库的答案)
const checkOpenState = (tree, state) => {
    expect(!!tree.find(ExpandLess).length).toBe(state)
};
const checkCloseState = (tree, state) => {
   expect(!!tree.find(ExpandMore).length).toBe(state)
};
const toggleState = (tree) => {
   tree.find(Button).props().onClick();
}
it('should render proper icon on show and hide', async () => {
  const component = shallow(<MyMenu />);
  checkOpenState(component, false);
  checkCloseState(component, true);

  toggleState(component);

  checkOpenState(component, true);
  checkCloseState(component, false);