【发布时间】:2020-11-25 11:33:50
【问题描述】:
假设我们有一个组件
import React, { Component } from "react";
import $ from "jquery";
class App extends Component {
componentDidMount() {
$('#empty_p_tag').text('hello')
}
render() {
return (
<div>
<p>This is a component</p>
<p id ="empty_p_tag"></p>
</div>)
}
}
export default App;
我们想要测试带有 id empty_p_tag 的 p 标签现在在标签中说“你好”。我们如何用酶和玩笑做到这一点?
it('should render hello', function () {
const app_wrapper = mount(<App/>);
const p_tag = app_wrapper.find('#empty_p_tag').text();
expect(p_tag).toBe('hello');
});
运行此测试时,我得到了响应:
expect(received).toBe(expected) // Object.is equality
Expected: "hello"
Received: ""
4 | const app_wrapper = mount(<App/>);
5 | const p_tag = app_wrapper.find('#empty_p_tag').text();
> 6 | expect(p_tag).toBe('hello');
| ^
7 |
8 | });
我不确定我在哪里做错了?我应该将 p_tag 元素存储在构造函数的 props 和 states 部分吗?此外,我试图避免使用 mount,但是当我使用浅层运行时,它并不快乐。
感谢您的帮助
【问题讨论】: