[英]Testing if a prop function was called using react-testing-library and jest
I am trying to write a simple test to verify that a prop was called when user interacts with a couple buttons.我正在尝试编写一个简单的测试来验证当用户与几个按钮交互时是否调用了一个道具。 Here is my test:这是我的测试:
it('submits the name on save', async () => {
const name = 'Some name'
const updatedName = 'New name'
const onSubmit = jest.fn()
render(
<EditableName name={name} onSubmit={onSubmit} />
)
act(() => {
fireEvent.click(getByText('Edit'))
fireEvent.change(getByRole('input'), { target: { value: updatedName } })
fireEvent.click(getByText('Save'))
})
expect(onSubmit).toBeCalledTimes(1)
})
Result:结果:
● EditableName › submits the name on save
expect(jest.fn()).toBeCalledTimes(expected)
Expected number of calls: 1
Received number of calls: 0
When I console.log
to see if the line that calls onSubmit
is being reached, I can see that it is.当我在console.log
中查看是否正在到达调用onSubmit
的行时,我可以看到它是。
I am suspecting the problem is that the onSubmit
function I'm expecting is a different instance of the function by the time I'm asserting .toBeCalledTimes()
.我怀疑问题在于,当我断言.toBeCalledTimes()
时,我期望的onSubmit
函数是该函数的不同实例。 Because of the three events that I am firing cause the component to re-render.由于我触发的三个事件导致组件重新渲染。
How can I make sure the function that I am passing as a prop in the test is the same one that is being called?如何确保我在测试中作为道具传递的函数与被调用的函数相同? Do I need to use spyOn
?我需要使用spyOn
吗? If so, how?如果是这样,如何?
Thanks in advance!提前致谢!
Edit: Here is the minimal code example of the component:编辑:这是组件的最小代码示例:
import React, { useState } from "react";
import { Box, Button, TextField, Typography } from "@material-ui/core";
const EditableName = ({ name, onSubmit }) => {
const [editing, setEditing] = useState(false);
const [value, setValue] = useState(name);
const handleSubmit = () => {
onSubmit({ name: value });
};
const handleToggleEditMode = () => {
if (editing) {
setEditing(false);
setValue(name);
} else {
setEditing(true);
}
};
if (!editing) {
return (
<>
<Typography display="inline" variant="h5" component="h1">
{name}
</Typography>
<Button onClick={handleToggleEditMode}>
Edit
</Button>
</>
);
}
return (
<>
<TextField
name="name"
variant="outlined"
fullWidth
size="small"
label="Subsidy name"
value={value}
onChange={(e) => setValue(e.target.value)}
inputProps={{ role: "input" }}
/>
<Button onClick={handleSubmit}>Save</Button>
</>
);
};
export default EditableName;
I think you need to separate the events, and act
is built into RTL so you need to use await waitFor
instead:我认为您需要将事件分开,并且act
内置于 RTL 中,因此您需要使用await waitFor
代替:
await waitFor(() => {
fireEvent.click(getByText('Edit'))
});
//should probably assert changes here
await waitFor(() => {
fireEvent.change(getByRole('input'), { target: { value: updatedName } })
});
//should probably assert changes here
await waitFor(() => {
fireEvent.click(getByText('Save'))
})
expect(onSubmit).toBeCalledTimes(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.