我正在尝试在具有等待的Async函数中测试Swal。存在Await以确保用户在继续操作之前选择对甜蜜警报的操作。
如何在Jest中对此进行测试?
这是测试: 从“ ./NotificationPreferences”导入{NotificationPreferences}; 从“ sweetalert2”导入swal;
it('user clicks opt out and OK', async () => {
const wrapper = mount(<NotificationPreferences {...props} />);
const instance = wrapper.instance();
const event = {
target: {
checked: false
}
};
await instance.onOptChange(event);
expect(swal).toHaveBeenCalledTimes(1);
});
功能如下:
async onOptChange(event){
let newPreferences = {
optIn: this.props.preferencesRedux.optIn,
id: this.props.preferencesRedux.id,
email: false,
card: false
};
let isChecked = event.target.checked;
let optStatus = 'in';
let shouldSavePreferences = true;
if(!isChecked) {//if opting out
optStatus = 'out';
await swal({
title: 'Please Be Advised',
text: 'some text',
type: 'warning',
showCancelButton: true,
showCloseButton: true,
focusCancel: true,
confirmButtonText: 'Opt out'
}).then((didUserOptOut) => {
//if user does click OK button, didUserOptOut.value === true
didUserOptOut.value !== true ? shouldSavePreferences = false : "";
swal.close()
});
} else {
newPreferences.card = true;
}
};
由于swal在函数内部等待,我该如何编写测试以便正确等待用户响应?