|
1 | 1 | import React from "react";
|
2 |
| -import { cleanup, render, wait } from "@testing-library/react"; |
| 2 | +import { cleanup, render, wait, fireEvent } from "@testing-library/react"; |
3 | 3 | import "@testing-library/jest-dom/extend-expect";
|
4 | 4 | import userEvent from "../../src";
|
5 | 5 |
|
@@ -35,31 +35,40 @@ describe("userEvent.type", () => {
|
35 | 35 | expect(getByTestId("input")).not.toHaveProperty("value", text);
|
36 | 36 | });
|
37 | 37 |
|
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 () => { |
39 | 39 | jest.useFakeTimers();
|
40 | 40 | const onChange = jest.fn();
|
| 41 | + const onInput = jest.fn(); |
41 | 42 | const { getByTestId } = render(
|
42 | 43 | React.createElement("input", {
|
43 | 44 | "data-testid": "input",
|
44 |
| - onChange: onChange |
| 45 | + onInput, |
| 46 | + onChange |
45 | 47 | })
|
46 | 48 | );
|
47 | 49 | const text = "Hello, world!";
|
48 | 50 | const delay = 10;
|
| 51 | + // Attach a native change listener because React cannot listen for text input change events |
49 | 52 | userEvent.type(getByTestId("input"), text, {
|
50 | 53 | delay
|
51 | 54 | });
|
52 |
| - expect(onChange).not.toHaveBeenCalled(); |
| 55 | + expect(onInput).not.toHaveBeenCalled(); |
53 | 56 | expect(getByTestId("input")).not.toHaveProperty("value", text);
|
54 | 57 |
|
55 | 58 | for (let i = 0; i < text.length; i++) {
|
56 | 59 | 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); |
58 | 62 | expect(getByTestId("input")).toHaveProperty(
|
59 | 63 | "value",
|
60 | 64 | text.slice(0, i + 1)
|
61 | 65 | );
|
62 | 66 | }
|
| 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 | + }); |
63 | 72 | });
|
64 | 73 |
|
65 | 74 | it.each(["input", "textarea"])(
|
|
0 commit comments