我弹出Delete
,并想查看单击Confirm
按钮后是否删除了该元素:
userEvent.click(screen.getByTestId('confirm-btn')) //trigger Confirm button
waitForElementToBeRemoved(screen.getByText("Delete")).then(() =>{
expect(screen.getByText("Item 1")).not.toBeInTheDocument();
});
当我将.not.toBeInTheDocument()
更改为.toBeInTheDocument();
时,测试仍然通过。
为什么会这样?如何以正确的方式做到这一点?
答案 0 :(得分:0)
我明白了。应该是这样的:
userEvent.click(screen.getByTestId('confirm-btn')) //trigger Confirm button
//await added. Need to add async at the start also, e.g. it("Successfully deleted", async() => ...
await waitForElementToBeRemoved(screen.getByText("Delete")).then(() =>{
expect(screen.queryByText("Item 1")).not.toBeInTheDocument(); // another mistake here.
});
为了安全起见,在测试声明后必须具有expect.assertions(1);
。这样可以确保调用一个断言,否则将引发错误。