你好,我试图用Jest做一些TDD或单元测试,但是我发现检查组件状态很困难...
我怀疑的一个示例是以下代码,但是实际上任何其他更改了组件状态的情况都是相同的:
class ListAdmin extends List {
requestFilterBy() {
getOrdersFilteredByStatus(this.state.filterValue) //GET request to API
.then(response => {
if(response.data){
this.setState({orders : response.data}, function() {
if (this.state.orders.length < 1) {this.setState({error : `No tracks found with ${this.state.filterValue} status`})}
else {this.setState({error : ``})}
})
}
})
.catch(error => console.log(error))
}
现在我想将this.setState方法的回调函数转换为:
areThereAnyOrders (){
if (this.state.orders.length < 1) {this.setState({error : `No tracks found with ${this.state.filterValue} status`})}
else {this.setState({error : ``})}
})
}
现在要进行测试
您将如何理解所有的类依赖和东西?我目前正在这样做:
it ('areThereAnyOrders is valid', () => {
let listAdmin = new ListAdmin();
this.state.filterValue = "offer made"
listAdmin.areThereAnyOrders()
expect(listAdmin.state.error).toBe(`No tracks found with offer made status`)
})
或:
it ('areThereAnyOrders is valid', () => {
let listAdmin = new ListAdmin();
this.state.filterValue = "offer made"
this.state.orders = [{gallons : 10, price : 200}]
listAdmin.areThereAnyOrders()
expect(listAdmin.state.error).toBe("")
})
第一种情况下我有命令,第二种情况下我没有命令,
您将如何处理此类需要构建类的情况?