/
type.js
175 lines (146 loc) Β· 4.75 KB
/
type.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { cleanup, render, wait, fireEvent } from "@testing-library/vue";
import "@testing-library/jest-dom/extend-expect";
import userEvent from "../../src";
afterEach(cleanup);
const renderComponent = (type, events = {}, attrs = {}) =>
render({
render: function(h) {
return h(type, {
attrs: { "data-testid": "input", ...attrs },
on: events
});
}
});
describe("userEvent.type", () => {
it.each(["input", "textarea"])("should type text in <%s>", type => {
const input = jest.fn();
const { getByTestId } = renderComponent(type, { input });
const text = "Hello, world!";
userEvent.type(getByTestId("input"), text);
expect(input).toHaveBeenCalledTimes(text.length);
expect(getByTestId("input")).toHaveProperty("value", text);
});
it("should not type when event.preventDefault() is called", () => {
const input = jest.fn();
const change = jest.fn();
const keydown = jest
.fn()
.mockImplementation(event => event.preventDefault());
const { getByTestId } = renderComponent("input", {
input,
keydown,
change
});
const text = "Hello, world!";
userEvent.type(getByTestId("input"), text);
expect(keydown).toHaveBeenCalledTimes(text.length);
expect(change).toHaveBeenCalledTimes(0);
expect(input).toHaveBeenCalledTimes(0);
expect(getByTestId("input")).not.toHaveProperty("value", text);
});
it.each(["input", "textarea"])(
"should not type when <%s> is disabled",
type => {
const change = jest.fn();
const { getByTestId } = renderComponent(
type,
{
change
},
{
disabled: true
}
);
const text = "Hello, world!";
userEvent.type(getByTestId("input"), text);
expect(change).not.toHaveBeenCalled();
expect(getByTestId("input")).toHaveProperty("value", "");
}
);
it.each(["input", "textarea"])(
"should not type when <%s> is readOnly",
type => {
const change = jest.fn();
const keydown = jest.fn();
const keypress = jest.fn();
const keyup = jest.fn();
const { getByTestId } = renderComponent(
type,
{
change,
keydown,
keypress,
keyup
},
{
readOnly: true
}
);
const text = "Hello, world!";
userEvent.type(getByTestId("input"), text);
expect(keydown).toHaveBeenCalledTimes(text.length);
expect(keypress).toHaveBeenCalledTimes(text.length);
expect(keyup).toHaveBeenCalledTimes(text.length);
expect(change).not.toHaveBeenCalled();
expect(getByTestId("input")).toHaveProperty("value", "");
}
);
it("should delay the typing when opts.delay is not 0", async () => {
jest.useFakeTimers();
const change = jest.fn();
const input = jest.fn();
const { getByTestId } = renderComponent("input", { change, input });
const text = "Hello, world!";
const delay = 10;
userEvent.type(getByTestId("input"), text, {
delay
});
expect(input).not.toHaveBeenCalled();
expect(getByTestId("input")).not.toHaveProperty("value", text);
for (let i = 0; i < text.length; i++) {
jest.advanceTimersByTime(delay);
await wait(() => expect(input).toHaveBeenCalledTimes(i + 1));
expect(getByTestId("input")).toHaveProperty(
"value",
text.slice(0, i + 1)
);
}
// Vue's change event is not emitted until blurring the input
expect(change).not.toHaveBeenCalled();
fireEvent.blur(getByTestId("input"));
await wait(() => expect(change).toHaveBeenCalledTimes(1));
});
it.each(["input", "textarea"])(
"should type text in <%s> all at once",
type => {
const input = jest.fn();
const { getByTestId } = renderComponent(type, { input });
const text = "Hello, world!";
userEvent.type(getByTestId("input"), text, {
allAtOnce: true
});
expect(getByTestId("input")).toHaveProperty("value", text);
expect(input).toHaveBeenCalledTimes(1);
}
);
it.each(["input", "textarea"])(
"should type text in <%s> up to maxLength if provided",
type => {
const input = jest.fn();
const maxLength = 10;
const { getByTestId } = renderComponent(type, { input }, { maxLength });
const text = "superlongtext";
const slicedText = text.slice(0, maxLength);
const inputEl = getByTestId("input");
userEvent.type(inputEl, text, {
allAtOnce: true
});
expect(inputEl).toHaveProperty("value", slicedText);
inputEl.value = "";
input.mockClear();
userEvent.type(inputEl, text);
expect(inputEl).toHaveProperty("value", slicedText);
expect(input).toHaveBeenCalledTimes(slicedText.length);
}
);
});