I am having trouble testing one of my components.
I think I understand what's happening. When I enter a new character and the upstream is updated then the component is rendered again but now the component does not have the prior data.
Here is the code:
export const MyTest = (
{value, updateCallback}: {
value: string,
updateCallback: (value: string) => void
}) => {
return (
<>
<label htmlFor="my-test">My Test</label>
<input value={value} type="text" id="my-test" onChange={(e) => {
updateCallback(e.target.value);
console.log(e.target.value);
}}/>
</>
);
}
And here is the test:
test("test", async () => {
const user = userEvent.setup();
const updateCallback = vi.fn()
render(<MyTest value="" updateCallback={updateCallback}/>)
const input = screen.getByLabelText("My Test");
await act(async () => await user.type(input, "my-value"));
expect(updateCallback).toHaveBeenLastCalledWith("my-value");
})
My Test fails with the following output
m
y
-
v
a
l
u
e
AssertionError: expected last "spy" call to have been called with [ 'my-value' ]