-
Relevant code or config:Return jsx being tested return (
<>
<div
id="focusWarning"
data-testid="focusWarning"
className={focusWarning}
>
Click to focus
</div>
<div
id="codeWrapper"
data-testid="codeWrapper"
className={blurred}
onClick={handleClickToFocus}
ref={blurCodeRef}
>
{/* child components */}
</div>
<div className="grid justify-items-center py-2">
<input
id="codeInput"
data-testid="codeInput"
ref={focusInputRef}
tabIndex={0}
defaultValue={getBareElements(getLastWord(typed))}
autoComplete="off"
onKeyPress={handleKeyPress}
onKeyDown={(e) => e.key === "Backspace" && handleKeyPress(e)}
onBlur={handleFocusOut}
autoFocus
/>
</div>
</>
); What you did:Trying to test focus being taken away from the input element. it("focus away from codeWrapper", async () => {
const codeWrapper = screen.getByTestId("codeWrapper");
const codeInput = screen.getByTestId("codeInput");
const focusWarning = screen.getByTestId("focusWarning");
fireEvent.click(codeInput);
expect(codeInput).not.toHaveFocus();
expect(focusWarning).not.toHaveClass("hidden");
expect(codeWrapper).toHaveClass("blurred");
}); What happened:
Reproduction:Problem description:The expect and received output is the same:
This test should pass. It looks like the Suggested solution:Not familiar enough with this library sorry. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The input has Though I'd add this: even though And last but not least, I suggest you use |
Beta Was this translation helpful? Give feedback.
The input has
autoFocus
enabled, so it is understandable that it has focus. Can you try withoutautoFocus
to see if it works?Though I'd add this: even though
fireEvent.click(codeInput)
is too low level and won't actually set focus on the input, in real life if you click on the input element, that'll also set focus on the input. So the situation you're describing in your test has multiple angles through which the input should end up having focus. I wonder why you so adamantly expect it not to have focus. 🤔And last but not least, I suggest you use
userEvent.click(codeInput)
instead offireEvent.click(codeInput)
. It's on a separate library though. Check it out: https://github.com/testing-l…