【发布时间】:2020-09-14 16:12:19
【问题描述】:
我正在构建一个简单的 todo 应用程序,我也在使用酶进行测试。我刚刚将 reder 列表提取到它自己的反应组件中,它工作正常。我正在尝试对此组件的输出进行模拟测试,但我很挣扎。
我运行测试时的当前结果是
Expected: "New Note"
Received: <p><button onClick={[Function onClick]}>X</button></p>
目前的测试看起来像这样......
describe('ListItems', () => {
let wrapper;
beforeEach(() => {
const items = ['New Note', 'efefre', 'efqeecec'];
wrapper = shallow(<ListItems list={[items]} />);
});
it('renders title of application', () => {
expect(wrapper.find("h1").text()).toContain("This is the list")
});
it('renders the new note on the page', () => {
const items = ['New Note']
const wrapper = shallow(<ListItems list={items} />)
const textOutput = wrapper.find("#text-output")
expect(textOutput.props().children).toEqual("New Note")
})
});
如您所见,我试图用“新注释”模拟一个新数组,在我的测试中传递了一个对象,但不是实际消息。我哪里错了?
以防万一这是我要测试的文件..
import React from 'react';
function ListItems(props) {
const list = props.list
const displayItems = list.map((item, index) =>
{
return <div id='text-output' key={index}>
<p>
{item.body}
<button
onClick={ () => props.deleteItem(item.id)}>
X
</button>
</p>
</div>
})
return(
<div>
<h1>This is the list</h1>
{displayItems}
</div>
)
}
如果有人可以提供帮助,那就太好了。
【问题讨论】:
标签: javascript reactjs unit-testing jestjs enzyme