React测试图谱测试DOM在事件火上发生变化
给定以下组件(< button>
是一个自定义组件,是< button>
like),
const MyElement = ({
onRemove,
}) => {
const [isRemoving, setIsRemoving] = useState(false);
const handleRemove = (event) => {
event.stopPropagation();
setIsRemoving(true);
onRemove().finally(() => setIsRemoving(false));
};
return (
<Button
status={isRemoving ? 'busy' : 'selected'}
onClick={handleRemove}
>
Remove
</Button>
);
};
我想测试按钮的status>状态>状态>转弯
忙
在成为选择
之前,一旦onRemove
函数解析。如何使用用户事件执行此操作?
Given the following component (<Button>
is a custom component that's <button>
-like)
const MyElement = ({
onRemove,
}) => {
const [isRemoving, setIsRemoving] = useState(false);
const handleRemove = (event) => {
event.stopPropagation();
setIsRemoving(true);
onRemove().finally(() => setIsRemoving(false));
};
return (
<Button
status={isRemoving ? 'busy' : 'selected'}
onClick={handleRemove}
>
Remove
</Button>
);
};
I want to test that the button's status
turn busy
before becoming selected
once the onRemove
function resolves. How do I do this with user events?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个模拟
onremove
async函数,并在stetIsremove(true)
之后手动解析承诺。这样您就可以断言iSremoving
是true
首先,并在解决承诺后,将其断言为false
。myElement.tsx
:myElement.test.tsx
:测试结果:
You can create a mock
onRemove
async function and resolve the promise manually aftersetIsRemove(true)
. So that you can assert theisRemoving
to betrue
firstly and assert it to befalse
after the promise is resolved.MyElement.tsx
:MyElement.test.tsx
:Test result: