reactjs - 反应事件处理程序中引发的期望/忽略错误
问题描述
我如何期望事件处理程序中出现错误?
it("throws an error if [some cool condition]", () => {
getCoolCheck().mockRejectedValue("You're not cool enough bro...");
const screen = render(<CoolComponent />);
const btn = screen.getByLabelText("Are you cool enough?");
fireEvent.click(btn);
// this throws an intended error... how do I check it?
});
function CoolComponent() {
<button onClick={handleCoolCheck}>Are you cool enough?</button>
async function handleCoolCheck() {
try {
await getCoolCheck();
alert("So cool!")
} catch (e) {
alert(`error occurred while cool checking: ${e}`);
throw e;
}
}
}
此外,这不是一个正常的错误,而是一个被拒绝的承诺。
解决方案
推荐阅读
- javascript - TUI(Toast UI)日历 API 未按预期工作。
- xaml - xaml 元素如何继承 FontFamily 和 Foreground 等属性?
- angular - 使用地图向可观察对象添加值
- c++ - c++内存分配和重写vector类
- java - Android:创建意图后,我无法从 MainActivity 打开 UI
- python - Python Tkinter Scrollbar 只是调整它所在的框架的大小,而不是使画布可滚动
- flask - 渲染静态文件时出现Flask内部服务器错误
- python - 如何创建请求对象以获取令牌
- string - 将字符串转换为标题大小写 - Emacs Lisp
- java - 服务器发送请求 firebase java.net.protocolexception 在读取响应后无法写入请求正文