forked from testing-library/user-event
/
upload.js
140 lines (115 loc) Β· 4 KB
/
upload.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
import React from "react";
import { cleanup, render, fireEvent } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
import userEvent from "../../src";
afterEach(cleanup);
describe("userEvent.upload", () => {
it("should fire the correct events for input", () => {
const file = new File(["hello"], "hello.png", { type: "image/png" });
const events = [];
const eventsHandler = jest.fn((evt) => events.push(evt.type));
const eventHandlers = {
onMouseOver: eventsHandler,
onMouseMove: eventsHandler,
onMouseDown: eventsHandler,
onFocus: eventsHandler,
onMouseUp: eventsHandler,
onClick: eventsHandler,
};
const { getByTestId } = render(
<input type="file" data-testid="element" {...eventHandlers} />
);
userEvent.upload(getByTestId("element"), file);
expect(events).toEqual([
"mouseover",
"mousemove",
"mousedown",
"focus",
"mouseup",
"click",
]);
});
it("should fire the correct events with label", () => {
const file = new File(["hello"], "hello.png", { type: "image/png" });
const inputEvents = [];
const labelEvents = [];
const eventsHandler = (events) => jest.fn((evt) => events.push(evt.type));
const getEventHandlers = (events) => ({
onMouseOver: eventsHandler(events),
onMouseMove: eventsHandler(events),
onMouseDown: eventsHandler(events),
onFocus: eventsHandler(events),
onMouseUp: eventsHandler(events),
onClick: eventsHandler(events),
});
const { getByTestId } = render(
<>
<label
htmlFor="element"
data-testid="label"
{...getEventHandlers(labelEvents)}
>
Element
</label>
<input type="file" id="element" {...getEventHandlers(inputEvents)} />
</>
);
userEvent.upload(getByTestId("label"), file);
expect(inputEvents).toEqual(["focus", "click"]);
expect(labelEvents).toEqual([
"mouseover",
"mousemove",
"mousedown",
"mouseup",
"click",
]);
});
it("should upload the file", () => {
const file = new File(["hello"], "hello.png", { type: "image/png" });
const { getByTestId } = render(<input type="file" data-testid="element" />);
const input = getByTestId("element");
userEvent.upload(input, file);
expect(input.files[0]).toStrictEqual(file);
expect(input.files.item(0)).toStrictEqual(file);
expect(input.files).toHaveLength(1);
fireEvent.change(input, {
target: { files: { item: () => {}, length: 0 } },
});
expect(input.files[0]).toBeUndefined();
expect(input.files.item[0]).toBeUndefined();
expect(input.files).toHaveLength(0);
});
it("should upload multiple files", () => {
const files = [
new File(["hello"], "hello.png", { type: "image/png" }),
new File(["there"], "there.png", { type: "image/png" }),
];
const { getByTestId } = render(
<input type="file" multiple data-testid="element" />
);
const input = getByTestId("element");
userEvent.upload(input, files);
expect(input.files[0]).toStrictEqual(files[0]);
expect(input.files.item(0)).toStrictEqual(files[0]);
expect(input.files[1]).toStrictEqual(files[1]);
expect(input.files.item(1)).toStrictEqual(files[1]);
expect(input.files).toHaveLength(2);
fireEvent.change(input, {
target: { files: { item: () => {}, length: 0 } },
});
expect(input.files[0]).toBeUndefined();
expect(input.files.item[0]).toBeUndefined();
expect(input.files).toHaveLength(0);
});
it("should not upload when is disabled", () => {
const file = new File(["hello"], "hello.png", { type: "image/png" });
const { getByTestId } = render(
<input type="file" data-testid="element" disabled />
);
const input = getByTestId("element");
userEvent.upload(input, file);
expect(input.files[0]).toBeUndefined();
expect(input.files.item[0]).toBeUndefined();
expect(input.files).toHaveLength(0);
});
});