出于某种原因,我的测试在使用这样的 ref 时没有通过:
import React, { useRef } from "react";
import { waitForElement, render } from "@testing-library/react";
const MyComponent = props => {
const elementRef = useRef(null);
return (
<div>
<div ref={elementRef} />
{elementRef.current && <div data-testid="test-div" />}
</div>
);
};
describe("MyComponent", () => {
it("test-div should exist when the ref is set", async done => {
const { getByTestId } = render(<MyComponent />);
await waitForElement(() => getByTestId("test-div"));
});
});
Run Code Online (Sandbox Code Playgroud)
知道为什么这不起作用吗?