嗨,我试图测试功能使用React16与jest和酶,我已经写了测试用例,请看看下面的代码,我已经写了,我也试图测试。任何建议将是很大的帮助。
我想测试什么?
componentDidMount() {
if (this.props.isSubmissionComplete) {
window.location.href = process.env.REACT_APP_BASE_PATH;
}
if (document.getElementsByClassName("continue-button")[0]) {
document
.getElementsByClassName("continue-button")[0]
.classList.add("hidden");
}
if (document.getElementsByClassName("back-button")[0]) {
document
.getElementsByClassName("back-button")[0]
.classList.remove("offset-sm-1");
}
//Sets the applicable coverages to the state for use in the post submission pages
var coverageList = this.renderCoverageList();
this.props.updateApplicableCoverages(coverageList);
}
我写的代码是为了测试。
let wrapper;
beforeEach(() => {
wrapper = mount(
<FraudStatement/>
);
});
it("should hide the continue button if it exists", () => {
expect(wrapper.find(".continue-button").hasClass("hidden")).toBe(true);
});
我得到的错误
Error: expect(received).toBe(expected) // Object.is equality
Expected: true
Received: false Jest
1条答案
按热度按时间kwvwclae1#
说明
基本上这里我尝试在DOM中创建一个新元素,用s浅渲染组件,然后测试if条件,如果条件满足,它将期望Assert语句。
测试时,代码还标记100%的代码覆盖率。