-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
autoFocus prop and react-testing-library #3655
Comments
I have same issue. tried to use |
Finally, I resolve this issue. there is two points: 1. You should simulate keydown event for display dropdown. 2. Dropdown renders menu asynchronously. it('should display dropdown menu and call onSortChange when option clicked', async () => {
const { getByText, onSortChange, debug } = setup();
// (1) I tried to simulate mouse event but not working. this only is effective solution.
fireEvent.keyDown(document.querySelector('.list__control'), { key: 'ArrowDown', keyCode: 40 });
// (2) Dropdown renders menu asynchronosly. you have to wait for element to find by text
await waitForElement(() => getByText('Option 1'));
fireEvent.click(getByText('Option 1'));
expect(onSortChange).toHaveBeenCalledTimes(1);
expect(onSortChange).toHaveBeenLastCalledWith(['code', 'ASC']);
}); And don't forget use <ReactSelect
classNamePrefix="list"
noOptionsMessage={noOptionsMessage}
isSearchable={false}
isClearable={false}
components={selectComponents}
{...props}
/> |
@iamchanii are you using the |
@montezume No, but I wish it is help for you. as you can see, I added
|
@iamchanii thanks for your help! I figured out the source of the problem. When I did const input = getByLabelText('Select a fruit');
fireEvent.blur(input); the So calling blur on that div doesn't do what we need. So instead, I do describe("with autofocus", () => {
it.only("should be able to open", async () => {
const { getByLabelText, getByText, debug } = render(
<App autoFocus={true} />
);
const input = getByLabelText("Select a fruit");
fireEvent.blur(document.querySelector("input"));
fireEvent.keyDown(input, {
key: "ArrowDown",
keyCode: 40
});
await waitForElement(() => getByText("Papaya"));
fireEvent.click(getByText("Papaya"));
expect(getByText("Papaya")).toBeInTheDocument();
});
}); and it works, because now the blur is being called on the actual input. Thanks for your help! 🎉 I was even able to remove the async describe("with autofocus", () => {
it.only("should be able to open", () => {
const { getByLabelText, getByText } = render(<App autoFocus={true} />);
const containerDiv = getByLabelText("Select a fruit");
const input = containerDiv.querySelector("input");
fireEvent.blur(input);
fireEvent.keyDown(input, {
key: "ArrowDown",
keyCode: 40
});
expect(getByText("Papaya")).toBeInTheDocument();
fireEvent.click(getByText("Papaya"));
expect(getByText("Papaya")).toBeInTheDocument();
});
}); |
I'm glad to help you! 😄 |
I think I can close this now. Thanks! |
I was facing the same issue with autoFocus props in unit test cases. When used fireEvent.blur(input); all test cases were working. |
Summary
When testing
react-select
inputs with @testing-library/react, it's not possible to programatically open the select. This is with the latest version of react-select.Reproduction
I created a reproduction repo. I pasted the relevant code below to make it easier to follow
When I run the test, this is the result
The text was updated successfully, but these errors were encountered: