【问题标题】:How do I convert enzyme (shallow) to react testing library?如何将酶(浅)转换为反应测试库?
【发布时间】:2021-09-07 09:54:57
【问题描述】:

如何将我的这部分测试转换为 React 测试库,以便不再使用浅层?

const renderComponent = (jsx) => {
  const component = shallow(jsx);
  const title = component.find('.tile-bottom__title');
  const content = component.find('.tile-bottom__text');
  return {
    component,
    title,
    content,
  };
};

【问题讨论】:

    标签: unit-testing enzyme react-testing-library


    【解决方案1】:

    因为对于 react-testing-library,你可以从屏幕上读取 DOM 节点,你可以更新函数到

    const renderComponent = (jsx) => {
      const { container, getByTestId }  = render(jsx);
      const title = getByTestId('tile-bottom__title'); // not class but testId
      const content = getByTestId('tile-bottom__text'); // // not class but testId
      return {
        // component, // not needed to return component as you can use `screen` for further assertions
        title,
        content,
      };
    };
    
    

    【讨论】:

    • 容器是干什么用的?
    • 它被用作查看组件渲染位置的参考。
    猜你喜欢
    • 1970-01-01
    • 2019-01-30
    • 2020-08-26
    • 2020-06-01
    • 2018-01-17
    • 2017-09-24
    • 1970-01-01
    • 2017-05-22
    • 2021-06-04
    相关资源
    最近更新 更多