DOM Environment: "jest-environment-jsdom": "^29.1.2", "@testing-library/jest-dom": "^5.16.5"
What you did:
I have a Input Text Component setup which selects the entire value of the input field on focus. It is expected to when it loses focus or is blurred, it automatically deselects the selection.
Relevant code or config:
it('When blurred, deselect', () => {
const inputEl = screen.getByLabelText("Search") as HTMLInputElement;
const inputVal = "Placeholder"; // The input value
/** Fire an focus event */
inputEl.focus(); // // alternatively: fireEvent.focus(inputEl) - Tried both
const afterFocusSelection = inputVal.slice(
inputEl.selectionStart,
inputEl.selectionEnd
);
expect(afterFocusSelection).toBe(inputVal); // Compare to input value: Selected
console.log({ afterFocusSelection }); // "Placeholder"
/** Fire a blur event */
act(() => {
fireEvent.blur(inputEl); // alternatively: fireEvent.blur(inputEl) - Tried both
});
expect(inputEl).not.toHaveFocus(); // Passes Test
const afterBlurSelection = inputVal.slice(
inputEl.selectionStart,
inputEl.selectionEnd
);
console.log({ afterBlurSelection }); // Still returns "Placeholder"
expect(afterBlurSelection).not.toBe(inputVal); // Compare to input value: Not selected | Doesn't pass Test
});
What happened:
Getting the following error.
The console log of the selection, before and after Blur shows the same selection: "Placeholder"
@testing-library/react
version: 13.4.0"jest": "^29.1.2"
,"jest-environment-jsdom": "^29.1.2"
,"@testing-library/jest-dom": "^5.16.5"
What you did:
I have a Input Text Component setup which selects the entire value of the input field on focus. It is expected to when it loses focus or is blurred, it automatically deselects the selection.
Relevant code or config:
What happened:
Getting the following error.
"Placeholder"
Problem description:
Not able to test whether the functionality of select, deselect on focus and blur is possible.
This is unexpected behaviour, because the On Focus test works,
Reproduction:
This is the component. Copy this and import it into the test. The tests are already above.
I'll try making a repo later in the day and update this...but this should be extremely simple to reproduce.