当前对包含 this.refs [ data ] 的函数进行单元测试。由于我只能进行浅层渲染,所以我一直不确定。我试图通过这样做来嘲笑诗意
var refs = { '001m000000RkzHuAAJ': function () {} };
var refStub = sinon.mock(refs);
但我相信因为该函数正在执行 this.refs 而不是 refs 它不起作用。关于如何模拟 this.refs 的任何想法?
编辑:
const wrapper = shallow (
<CashAllocation receipt={receipt} slds={''} clients={clients} billingStatements={billingStatementswithAllocation} userTitle={'Premium Accounting User'} userPermission={2} />
);
wrapper.instance().autoApply();
wrapper.update();
console.log(wrapper.state('billingStatements'));
它在叫什么:
autoApply() {
const clientsSortedbyLargestOutstanding = this.props.clients.slice().sort(function(a,b) {
if (a.outstandingBalance < b.outstandingBalance) {
return 1;
} else if (a.outstandingBalance == b.outstandingBalance) {
return 0;
} else {
return -1;
}
});
var amountToApply = this.state.receipt.Amount_Remaining__c;
clientsSortedbyLargestOutstanding.forEach(function(client) {
var amountApplied = 0;
if (this.state.receipt.Amount_Remaining__c == 0) {
amountToApply = 0;
} else {
if (client.outstandingBalance < (amountToApply + client.currentAllocation)) {
amountApplied = client.currentAllocation + (client.outstandingBalance - client.currentAllocation);
amountToApply = amountToApply - (client.outstandingBalance - client.currentAllocation);
} else {
amountApplied = client.currentAllocation + amountToApply;
amountToApply = 0;
}
}
this.refs[ client.Id ].autoApply(amountApplied);
}, this);
}
我收到以下错误。当我挖掘堆栈跟踪时,它的 this.refs 导致它:
TypeError: Cannot read property 'autoApply' of undefined