【问题标题】: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,
};
};