Skip to content

Commit c5df100

Browse files
authored
Merge pull request #147 from testing-library/fix/onchange-text-input
Fix/onchange text input
2 parents ceff569 + 8ea17a1 commit c5df100

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

__tests__/react/type.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { cleanup, render, wait } from "@testing-library/react";
2+
import { cleanup, render, wait, fireEvent } from "@testing-library/react";
33
import "@testing-library/jest-dom/extend-expect";
44
import userEvent from "../../src";
55

@@ -35,31 +35,40 @@ describe("userEvent.type", () => {
3535
expect(getByTestId("input")).not.toHaveProperty("value", text);
3636
});
3737

38-
it("should delayed the typing when opts.dealy is not 0", async () => {
38+
it("should delay the typing when opts.delay is not 0", async () => {
3939
jest.useFakeTimers();
4040
const onChange = jest.fn();
41+
const onInput = jest.fn();
4142
const { getByTestId } = render(
4243
React.createElement("input", {
4344
"data-testid": "input",
44-
onChange: onChange
45+
onInput,
46+
onChange
4547
})
4648
);
4749
const text = "Hello, world!";
4850
const delay = 10;
51+
// Attach a native change listener because React cannot listen for text input change events
4952
userEvent.type(getByTestId("input"), text, {
5053
delay
5154
});
52-
expect(onChange).not.toHaveBeenCalled();
55+
expect(onInput).not.toHaveBeenCalled();
5356
expect(getByTestId("input")).not.toHaveProperty("value", text);
5457

5558
for (let i = 0; i < text.length; i++) {
5659
jest.advanceTimersByTime(delay);
57-
await wait(() => expect(onChange).toHaveBeenCalledTimes(i + 1));
60+
await wait(() => expect(onInput).toHaveBeenCalledTimes(i + 1));
61+
expect(onChange).toHaveBeenCalledTimes(i + 1);
5862
expect(getByTestId("input")).toHaveProperty(
5963
"value",
6064
text.slice(0, i + 1)
6165
);
6266
}
67+
// Blurring the input "commits" the value, React's onChange should not fire
68+
fireEvent.blur(getByTestId("input"));
69+
await wait(() => expect(onChange).toHaveBeenCalledTimes(text.length), {
70+
timeout: 300
71+
});
6372
});
6473

6574
it.each(["input", "textarea"])(

src/index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,17 @@ function selectOption(option) {
9090
option.selected = true;
9191
}
9292

93+
function fireChangeEvent(event) {
94+
fireEvent.change(event.target);
95+
event.target.removeEventListener("blur", fireChangeEvent);
96+
}
97+
9398
const userEvent = {
9499
click(element) {
95-
const focusedElement = document.activeElement;
100+
const focusedElement = element.ownerDocument.activeElement;
96101
const wasAnotherElementFocused =
97-
focusedElement !== document.body && focusedElement !== element;
102+
focusedElement !== element.ownerDocument.body &&
103+
focusedElement !== element;
98104
if (wasAnotherElementFocused) {
99105
fireEvent.mouseMove(focusedElement);
100106
fireEvent.mouseLeave(focusedElement);
@@ -172,10 +178,8 @@ const userEvent = {
172178
};
173179
const opts = Object.assign(defaultOpts, userOpts);
174180
if (opts.allAtOnce) {
175-
fireEvent.change(element, { target: { value: text } });
181+
fireEvent.input(element, { target: { value: text } });
176182
} else {
177-
const typedCharacters = text.split("");
178-
179183
let actuallyTyped = "";
180184
for (let index = 0; index < text.length; index++) {
181185
const char = text[index];
@@ -193,12 +197,11 @@ const userEvent = {
193197
const pressEvent = fireEvent.keyPress(element, {
194198
key: key,
195199
keyCode,
196-
charCode: keyCode,
197-
keyCode: keyCode
200+
charCode: keyCode
198201
});
199202
if (pressEvent) {
200203
actuallyTyped += key;
201-
fireEvent.change(element, {
204+
fireEvent.input(element, {
202205
target: {
203206
value: actuallyTyped
204207
},
@@ -215,6 +218,7 @@ const userEvent = {
215218
});
216219
}
217220
}
221+
element.addEventListener("blur", fireChangeEvent);
218222
}
219223
};
220224

0 commit comments

Comments
 (0)