尝试运行以下功能的测试:使用JEST和酶来反应JS
async patchAccount() {
const { user, services, FirmId } = this.props;
let StatusChanged = this.state.AccountStatus && (this.state.AccountStatus.value !== this.state.StartingStatus)
let AccountBody = {
AccountName: this.state.AccountName,
AccountTitle: this.state.AccountTitle,
}
if(StatusChanged) {
AccountBody.AccountStatusDate = new Date().toISOString().slice(0, 19).concat('Z');
}
let response = await Models.patchAccount({
user,
services,
body: AccountBody,
firmId: FirmId,
id: this.props.accountToEdit
})
if(!response.error) {
return true;
} else {
return false;
}
这是我设置测试文件account.test.js
it('Test patchAccount function ',async() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000
Models.postAccounts = jest.fn().mockImplementation(() => Promise.resolve(
{error: false},
{error: true},
))
wrapper.setProps({
user:{},
services:[],
FirmId:[],
accountToEdit:[]
})
wrapper.find('AccountForm').setState({
AccountBody:{},
AccountStatus:""
});
wrapper.update();
await expect(wrapper.find('AccountForm').instance().patchAccount()).toBeDefined()
});
我该如何正确测试它并确保兑现承诺。还尝试使用HaveBeenCalled()
调用模拟功能,但没有用。感谢您的帮助。