【发布时间】:2018-11-19 14:40:56
【问题描述】:
我在尝试了解如何使用 Jest 测试反应文件中方法的输出时遇到问题。我对这种 Web 开发风格完全陌生,因此感谢您提供任何帮助。
我有一个这样的js文件:
import * as React from 'react';
import 'es6-promise';
import 'isomorphic-fetch';
export default class FetchData extends React.Component {
constructor() {
super();
this.state = { documents: [], loading: true };
fetch('api/SampleData/GetDocuments')
.then(response => response.json())
.then(data => {
this.setState({ documents: data, loading: false });
});
}
render() {
let contents = this.state.loading ? <p><em>Loading...</em></p>
: FetchData.renderdocumentsTable(this.state.documents);
return <div>
<button onClick={() => { this.refreshData() }}>Refresh</button>
<p>This component demonstrates bad document data from the server.</p>
{contents}
</div>;
}
refreshData() {
fetch('api/SampleData/GetDocuments')
.then(response => response.json())
.then(data => {
this.setState({ documents: data, loading: false });
});
}
static renderdocumentsTable(documents) {
return <table className='table'>
<thead>
<tr>
<th>Filename</th>
<th>CurrentSite</th>
<th>CorrectSite</th>
</tr>
</thead>
<tbody>
{documents.map(document =>
<tr className="document-row" key={document.documentId}>
<td>{document.filename}</td>
<td>{document.currentSite}</td>
<td>{document.correctSite}</td>
</tr>
)}
</tbody>
</table>;
}
}
我基本上希望能够测试返回的表是否具有正确的列数,但是我无法确切知道如何使用 Jest 执行此操作。
谢谢, 亚历克斯
【问题讨论】:
标签: reactjs jestjs babel-jest