【问题标题】:How can I mock out all the icons in React `@material-ui/icons` using Jest?如何使用 Jest 模拟 React `@material-ui/icons` 中的所有图标?
【发布时间】:2021-05-22 00:11:10
【问题描述】:

这个问题也可以概括为——如何模拟出导入模块上的所有属性以返回 React 组件?

【问题讨论】:

    标签: reactjs jestjs material-ui


    【解决方案1】:

    到目前为止我最好的解决方案是:

    jest.mock('@material-ui/icons', () => {
      const icons = {
        __esModule: true,
      };
    
      const handler = {
        get: function (_, prop) {
          return () => <div className={`mock_${prop}Icon`} />;
        },
      };
    
      return new Proxy(icons, handler);
    });
    

    【讨论】:

      猜你喜欢
      • 2020-08-30
      • 1970-01-01
      • 2019-10-04
      • 2021-06-08
      • 2022-11-22
      • 2018-11-22
      • 2019-07-12
      • 1970-01-01
      • 2016-12-07
      相关资源
      最近更新 更多