react-dropzone 11.5.3 → 12.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +1 -3
- package/.eslintrc +2 -4
- package/.husky/commit-msg +5 -0
- package/.husky/pre-commit +5 -0
- package/.nvmrc +1 -1
- package/README.md +78 -29
- package/commitlint.config.js +1 -1
- package/dist/es/index.js +195 -151
- package/dist/es/utils/index.js +53 -17
- package/dist/index.js +16 -2
- package/examples/events/README.md +3 -1
- package/examples/no-jsx/README.md +32 -0
- package/package.json +80 -76
- package/rollup.config.js +22 -19
- package/src/.eslintrc +7 -4
- package/src/__snapshots__/index.spec.js.snap +1 -1
- package/src/index.js +372 -288
- package/src/index.spec.js +1837 -1369
- package/src/utils/index.js +117 -56
- package/src/utils/index.spec.js +414 -254
- package/styleguide.config.js +57 -50
- package/testSetup.js +1 -1
- package/typings/.eslintrc +28 -0
- package/typings/react-dropzone.d.ts +24 -14
- package/typings/tests/accept.tsx +10 -9
- package/typings/tests/all.tsx +6 -5
- package/typings/tests/basic.tsx +8 -7
- package/typings/tests/events.tsx +8 -6
- package/typings/tests/file-dialog.tsx +4 -3
- package/typings/tests/hook.tsx +6 -6
- package/typings/tests/plugin.tsx +11 -22
- package/typings/tests/refs.tsx +4 -4
- package/tslint.json +0 -101
package/src/utils/index.spec.js
CHANGED
|
@@ -1,277 +1,437 @@
|
|
|
1
1
|
beforeEach(() => {
|
|
2
|
-
jest.resetModules()
|
|
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
|
-
|
|
2
|
+
jest.resetModules();
|
|
3
|
+
});
|
|
4
|
+
|
|
5
|
+
describe("fileMatchSize()", () => {
|
|
6
|
+
let utils;
|
|
7
|
+
beforeEach(async () => {
|
|
8
|
+
utils = await import("./index");
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("should return true if the file object doesn't have a {size} property", () => {
|
|
12
|
+
expect(utils.fileMatchSize({})).toEqual([true, null]);
|
|
13
|
+
expect(utils.fileMatchSize({ size: null })).toEqual([true, null]);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should return true if the minSize and maxSize were not provided", () => {
|
|
17
|
+
expect(utils.fileMatchSize({ size: 100 })).toEqual([true, null]);
|
|
18
|
+
expect(utils.fileMatchSize({ size: 100 }, undefined, undefined)).toEqual([
|
|
19
|
+
true,
|
|
20
|
+
null,
|
|
21
|
+
]);
|
|
22
|
+
expect(utils.fileMatchSize({ size: 100 }, null, null)).toEqual([
|
|
23
|
+
true,
|
|
24
|
+
null,
|
|
25
|
+
]);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should return true if the file {size} is within the [minSize, maxSize] range", () => {
|
|
29
|
+
expect(utils.fileMatchSize({ size: 100 }, 10, 200)).toEqual([true, null]);
|
|
30
|
+
expect(utils.fileMatchSize({ size: 100 }, 10, 99)).toEqual([
|
|
31
|
+
false,
|
|
32
|
+
{ code: "file-too-large", message: "File is larger than 99 bytes" },
|
|
33
|
+
]);
|
|
34
|
+
expect(utils.fileMatchSize({ size: 100 }, 101, 200)).toEqual([
|
|
35
|
+
false,
|
|
36
|
+
{ code: "file-too-small", message: "File is smaller than 101 bytes" },
|
|
37
|
+
]);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should return true if the file {size} is more than minSize", () => {
|
|
41
|
+
expect(utils.fileMatchSize({ size: 100 }, 100)).toEqual([true, null]);
|
|
42
|
+
expect(utils.fileMatchSize({ size: 100 }, 101)).toEqual([
|
|
43
|
+
false,
|
|
44
|
+
{ code: "file-too-small", message: "File is smaller than 101 bytes" },
|
|
45
|
+
]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should return true if the file {size} is less than maxSize", () => {
|
|
49
|
+
expect(utils.fileMatchSize({ size: 100 }, undefined, 100)).toEqual([
|
|
50
|
+
true,
|
|
51
|
+
null,
|
|
52
|
+
]);
|
|
53
|
+
expect(utils.fileMatchSize({ size: 100 }, null, 100)).toEqual([true, null]);
|
|
54
|
+
expect(utils.fileMatchSize({ size: 100 }, undefined, 99)).toEqual([
|
|
55
|
+
false,
|
|
56
|
+
{ code: "file-too-large", message: "File is larger than 99 bytes" },
|
|
57
|
+
]);
|
|
58
|
+
expect(utils.fileMatchSize({ size: 100 }, null, 99)).toEqual([
|
|
59
|
+
false,
|
|
60
|
+
{ code: "file-too-large", message: "File is larger than 99 bytes" },
|
|
61
|
+
]);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("isIeOrEdge", () => {
|
|
66
|
+
let utils;
|
|
67
|
+
beforeEach(async () => {
|
|
68
|
+
utils = await import("./index");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("should return true for IE10", () => {
|
|
51
72
|
const userAgent =
|
|
52
|
-
|
|
73
|
+
"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729)";
|
|
53
74
|
|
|
54
|
-
expect(utils.isIeOrEdge(userAgent)).toBe(true)
|
|
55
|
-
})
|
|
75
|
+
expect(utils.isIeOrEdge(userAgent)).toBe(true);
|
|
76
|
+
});
|
|
56
77
|
|
|
57
|
-
it(
|
|
78
|
+
it("should return true for IE11", () => {
|
|
58
79
|
const userAgent =
|
|
59
|
-
|
|
60
|
-
expect(utils.isIeOrEdge(userAgent)).toBe(true)
|
|
61
|
-
})
|
|
80
|
+
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; rv:11.0) like Gecko";
|
|
81
|
+
expect(utils.isIeOrEdge(userAgent)).toBe(true);
|
|
82
|
+
});
|
|
62
83
|
|
|
63
|
-
it(
|
|
84
|
+
it("should return true for Edge", () => {
|
|
64
85
|
const userAgent =
|
|
65
|
-
|
|
86
|
+
"Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16258";
|
|
66
87
|
|
|
67
|
-
expect(utils.isIeOrEdge(userAgent)).toBe(true)
|
|
68
|
-
})
|
|
88
|
+
expect(utils.isIeOrEdge(userAgent)).toBe(true);
|
|
89
|
+
});
|
|
69
90
|
|
|
70
|
-
it(
|
|
91
|
+
it("should return false for Chrome", () => {
|
|
71
92
|
const userAgent =
|
|
72
|
-
|
|
93
|
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36";
|
|
73
94
|
|
|
74
|
-
expect(utils.isIeOrEdge(userAgent)).toBe(false)
|
|
75
|
-
})
|
|
76
|
-
})
|
|
95
|
+
expect(utils.isIeOrEdge(userAgent)).toBe(false);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
77
98
|
|
|
78
|
-
describe(
|
|
99
|
+
describe("isKindFile()", () => {
|
|
79
100
|
it('should return true for DataTransferItem of kind "file"', async () => {
|
|
80
|
-
const utils = await import(
|
|
81
|
-
expect(utils.isKindFile({ kind:
|
|
82
|
-
expect(utils.isKindFile({ kind:
|
|
83
|
-
expect(utils.isKindFile({})).toBe(false)
|
|
84
|
-
expect(utils.isKindFile(null)).toBe(false)
|
|
85
|
-
})
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
describe(
|
|
89
|
-
const trueFn = jest.fn(() => true)
|
|
90
|
-
|
|
91
|
-
let utils
|
|
92
|
-
beforeEach(async
|
|
93
|
-
utils = await import(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
it(
|
|
118
|
-
expect(utils.isEvtWithFiles({ dataTransfer: { types: [
|
|
119
|
-
|
|
101
|
+
const utils = await import("./index");
|
|
102
|
+
expect(utils.isKindFile({ kind: "file" })).toBe(true);
|
|
103
|
+
expect(utils.isKindFile({ kind: "text/html" })).toBe(false);
|
|
104
|
+
expect(utils.isKindFile({})).toBe(false);
|
|
105
|
+
expect(utils.isKindFile(null)).toBe(false);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("isPropagationStopped()", () => {
|
|
110
|
+
const trueFn = jest.fn(() => true);
|
|
111
|
+
|
|
112
|
+
let utils;
|
|
113
|
+
beforeEach(async () => {
|
|
114
|
+
utils = await import("./index");
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("should return result of isPropagationStopped() if isPropagationStopped exists", () => {
|
|
118
|
+
expect(utils.isPropagationStopped({ isPropagationStopped: trueFn })).toBe(
|
|
119
|
+
true
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("should return value of cancelBubble if isPropagationStopped doesnt exist and cancelBubble exists", () => {
|
|
124
|
+
expect(utils.isPropagationStopped({ cancelBubble: true })).toBe(true);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("should return false if isPropagationStopped and cancelBubble are missing", () => {
|
|
128
|
+
expect(utils.isPropagationStopped({})).toBe(false);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe("isEvtWithFiles()", () => {
|
|
133
|
+
let utils;
|
|
134
|
+
beforeEach(async () => {
|
|
135
|
+
utils = await import("./index");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("should return true if some dragged types are files", () => {
|
|
139
|
+
expect(utils.isEvtWithFiles({ dataTransfer: { types: ["Files"] } })).toBe(
|
|
140
|
+
true
|
|
141
|
+
);
|
|
120
142
|
expect(
|
|
121
143
|
utils.isEvtWithFiles({
|
|
122
|
-
dataTransfer: { types: [
|
|
144
|
+
dataTransfer: { types: ["application/x-moz-file"] },
|
|
123
145
|
})
|
|
124
|
-
).toBe(true)
|
|
125
|
-
expect(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
)
|
|
146
|
+
).toBe(true);
|
|
147
|
+
expect(
|
|
148
|
+
utils.isEvtWithFiles({
|
|
149
|
+
dataTransfer: { types: ["Files", "application/x-moz-file"] },
|
|
150
|
+
})
|
|
151
|
+
).toBe(true);
|
|
152
|
+
expect(
|
|
153
|
+
utils.isEvtWithFiles({ dataTransfer: { types: ["text/plain"] } })
|
|
154
|
+
).toBe(false);
|
|
155
|
+
expect(
|
|
156
|
+
utils.isEvtWithFiles({ dataTransfer: { types: ["text/html"] } })
|
|
157
|
+
).toBe(false);
|
|
158
|
+
expect(
|
|
159
|
+
utils.isEvtWithFiles({
|
|
160
|
+
dataTransfer: { types: ["Files", "application/test"] },
|
|
161
|
+
})
|
|
162
|
+
).toBe(true);
|
|
130
163
|
expect(
|
|
131
164
|
utils.isEvtWithFiles({
|
|
132
|
-
dataTransfer: { types: [
|
|
165
|
+
dataTransfer: { types: ["application/x-moz-file", "application/test"] },
|
|
166
|
+
})
|
|
167
|
+
).toBe(true);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("should return true if the event has a target with files", () => {
|
|
171
|
+
expect(utils.isEvtWithFiles({ target: { files: [] } })).toBe(true);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("should return false otherwise", () => {
|
|
175
|
+
expect(utils.isEvtWithFiles({})).toBe(false);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe("composeEventHandlers", () => {
|
|
180
|
+
let utils;
|
|
181
|
+
beforeEach(async () => {
|
|
182
|
+
utils = await import("./index");
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("returns a fn", () => {
|
|
186
|
+
const fn = utils.composeEventHandlers(() => {});
|
|
187
|
+
expect(typeof fn).toBe("function");
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("runs every passed fn in order", () => {
|
|
191
|
+
const fn1 = jest.fn();
|
|
192
|
+
const fn2 = jest.fn();
|
|
193
|
+
const fn = utils.composeEventHandlers(fn1, fn2);
|
|
194
|
+
const event = { type: "click" };
|
|
195
|
+
const data = { ping: true };
|
|
196
|
+
fn(event, data);
|
|
197
|
+
expect(fn1).toHaveBeenCalledWith(event, data);
|
|
198
|
+
expect(fn2).toHaveBeenCalledWith(event, data);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("stops after first fn that calls stopPropagation()", () => {
|
|
202
|
+
const fn1 = jest.fn().mockImplementation((event) => {
|
|
203
|
+
Object.defineProperty(event, "cancelBubble", { value: true });
|
|
204
|
+
return event;
|
|
205
|
+
});
|
|
206
|
+
const fn2 = jest.fn();
|
|
207
|
+
const fn = utils.composeEventHandlers(fn1, fn2);
|
|
208
|
+
const event = new MouseEvent("click");
|
|
209
|
+
fn(event);
|
|
210
|
+
expect(fn1).toHaveBeenCalledWith(event);
|
|
211
|
+
expect(fn2).not.toHaveBeenCalled();
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("stops before first fn if bubble is already canceled", () => {
|
|
215
|
+
const fn1 = jest.fn();
|
|
216
|
+
const fn2 = jest.fn();
|
|
217
|
+
const fn = utils.composeEventHandlers(fn1, fn2);
|
|
218
|
+
const event = new MouseEvent("click");
|
|
219
|
+
Object.defineProperty(event, "cancelBubble", { value: true });
|
|
220
|
+
fn(event);
|
|
221
|
+
expect(fn1).not.toHaveBeenCalled();
|
|
222
|
+
expect(fn2).not.toHaveBeenCalled();
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe("fileAccepted", () => {
|
|
227
|
+
let utils;
|
|
228
|
+
beforeEach(async () => {
|
|
229
|
+
utils = await import("./index");
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("accepts bogus firefox file", () => {
|
|
233
|
+
const file = createFile("bogus.png", 100, "application/x-moz-file");
|
|
234
|
+
expect(utils.fileAccepted(file, ".pdf")).toEqual([true, null]);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("accepts file when single accept criteria", () => {
|
|
238
|
+
const file = createFile("hamster.pdf", 100, "application/pdf");
|
|
239
|
+
expect(utils.fileAccepted(file, ".pdf")).toEqual([true, null]);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("accepts file when multiple accept criteria", () => {
|
|
243
|
+
const file = createFile("hamster.pdf", 100, "application/pdf");
|
|
244
|
+
expect(utils.fileAccepted(file, [".pdf", ".png"])).toEqual([true, null]);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("rejects file when single accept criteria", () => {
|
|
248
|
+
const file = createFile("hamster.pdf", 100, "application/pdf");
|
|
249
|
+
expect(utils.fileAccepted(file, ".png")).toEqual([
|
|
250
|
+
false,
|
|
251
|
+
{ code: "file-invalid-type", message: "File type must be .png" },
|
|
252
|
+
]);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("rejects file when multiple accept criteria", () => {
|
|
256
|
+
const file = createFile("hamster.pdf", 100, "application/pdf");
|
|
257
|
+
expect(utils.fileAccepted(file, [".gif", ".png"])).toEqual([
|
|
258
|
+
false,
|
|
259
|
+
{
|
|
260
|
+
code: "file-invalid-type",
|
|
261
|
+
message: "File type must be one of .gif, .png",
|
|
262
|
+
},
|
|
263
|
+
]);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("rejects file when single accept criteria as array", () => {
|
|
267
|
+
const file = createFile("hamster.pdf", 100, "application/pdf");
|
|
268
|
+
expect(utils.fileAccepted(file, [".gif"])).toEqual([
|
|
269
|
+
false,
|
|
270
|
+
{ code: "file-invalid-type", message: "File type must be .gif" },
|
|
271
|
+
]);
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
describe("allFilesAccepted()", () => {
|
|
276
|
+
let utils;
|
|
277
|
+
beforeEach(async () => {
|
|
278
|
+
utils = await import("./index");
|
|
279
|
+
});
|
|
280
|
+
it("rejects file when multiple accept criteria", () => {
|
|
281
|
+
const files = [
|
|
282
|
+
createFile("hamster.pdf", 100, "application/pdf"),
|
|
283
|
+
createFile("fish.pdf", 100, "application/pdf"),
|
|
284
|
+
];
|
|
285
|
+
const images = [
|
|
286
|
+
createFile("cats.gif", 1234, "image/gif"),
|
|
287
|
+
createFile("dogs.gif", 2345, "image/jpeg"),
|
|
288
|
+
];
|
|
289
|
+
expect(utils.allFilesAccepted({ files, multiple: true })).toEqual(true);
|
|
290
|
+
expect(
|
|
291
|
+
utils.allFilesAccepted({ files, multiple: true, maxFiles: 10 })
|
|
292
|
+
).toEqual(true);
|
|
293
|
+
expect(
|
|
294
|
+
utils.allFilesAccepted({ files, multiple: false, maxFiles: 10 })
|
|
295
|
+
).toEqual(false);
|
|
296
|
+
expect(
|
|
297
|
+
utils.allFilesAccepted({ files, multiple: true, accept: "image/jpeg" })
|
|
298
|
+
).toEqual(false);
|
|
299
|
+
expect(
|
|
300
|
+
utils.allFilesAccepted({
|
|
301
|
+
files: images,
|
|
302
|
+
multiple: true,
|
|
303
|
+
accept: "image/*",
|
|
133
304
|
})
|
|
134
|
-
).
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
expect(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
it(
|
|
154
|
-
|
|
155
|
-
expect(
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
})
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
expect(
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
expect(utils.
|
|
250
|
-
|
|
251
|
-
|
|
305
|
+
).toEqual(true);
|
|
306
|
+
expect(
|
|
307
|
+
utils.allFilesAccepted({ files, multiple: true, minSize: 110 })
|
|
308
|
+
).toEqual(false);
|
|
309
|
+
expect(
|
|
310
|
+
utils.allFilesAccepted({ files, multiple: true, maxSize: 99 })
|
|
311
|
+
).toEqual(false);
|
|
312
|
+
expect(
|
|
313
|
+
utils.allFilesAccepted({ files, multiple: true, maxFiles: 1 })
|
|
314
|
+
).toEqual(false);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
describe("ErrorCode", () => {
|
|
319
|
+
let utils;
|
|
320
|
+
beforeEach(async () => {
|
|
321
|
+
utils = await import("./index");
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it("should exist and have known error code properties", () => {
|
|
325
|
+
expect(utils.ErrorCode.FileInvalidType).toEqual(utils.FILE_INVALID_TYPE);
|
|
326
|
+
expect(utils.ErrorCode.FileTooLarge).toEqual(utils.FILE_TOO_LARGE);
|
|
327
|
+
expect(utils.ErrorCode.FileTooSmall).toEqual(utils.FILE_TOO_SMALL);
|
|
328
|
+
expect(utils.ErrorCode.TooManyFiles).toEqual(utils.TOO_MANY_FILES);
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
describe("canUseFileSystemAccessAPI()", () => {
|
|
333
|
+
let utils;
|
|
334
|
+
beforeEach(async () => {
|
|
335
|
+
utils = await import("./index");
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it("should return false if not", () => {
|
|
339
|
+
expect(utils.canUseFileSystemAccessAPI()).toBe(false);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("should return true if yes", () => {
|
|
343
|
+
// TODO: If we use these in other tests, restore once test is done
|
|
344
|
+
window.showOpenFilePicker = jest.fn();
|
|
345
|
+
expect(utils.canUseFileSystemAccessAPI()).toBe(true);
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
describe("filePickerOptionsTypes()", () => {
|
|
350
|
+
let utils;
|
|
351
|
+
beforeEach(async () => {
|
|
352
|
+
utils = await import("./index");
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it("should return proper types when the arg is a MIME type", () => {
|
|
356
|
+
expect(utils.filePickerOptionsTypes("application/zip")).toEqual([
|
|
357
|
+
{
|
|
358
|
+
description: "everything",
|
|
359
|
+
accept: { "application/zip": [] },
|
|
360
|
+
},
|
|
361
|
+
]);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it("should return proper types when the arg is an array of MIME types", () => {
|
|
365
|
+
expect(
|
|
366
|
+
utils.filePickerOptionsTypes(["application/zip", "application/json"])
|
|
367
|
+
).toEqual([
|
|
368
|
+
{
|
|
369
|
+
description: "everything",
|
|
370
|
+
accept: {
|
|
371
|
+
"application/zip": [],
|
|
372
|
+
"application/json": [],
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
]);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it("should exclude anything that's not a MIME type", () => {
|
|
379
|
+
expect(
|
|
380
|
+
utils.filePickerOptionsTypes([
|
|
381
|
+
"audio/*",
|
|
382
|
+
"video/*",
|
|
383
|
+
"image/*",
|
|
384
|
+
".txt",
|
|
385
|
+
"text/*",
|
|
386
|
+
])
|
|
387
|
+
).toEqual([
|
|
388
|
+
{
|
|
389
|
+
description: "everything",
|
|
390
|
+
accept: {
|
|
391
|
+
"audio/*": [],
|
|
392
|
+
"video/*": [],
|
|
393
|
+
"image/*": [],
|
|
394
|
+
"text/*": [],
|
|
395
|
+
},
|
|
396
|
+
},
|
|
397
|
+
]);
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it("should work with comma separated string of MIME types", () => {
|
|
401
|
+
expect(
|
|
402
|
+
utils.filePickerOptionsTypes(
|
|
403
|
+
"audio/*,video/*,image/*,.txt,text/*,application/zip"
|
|
404
|
+
)
|
|
405
|
+
).toEqual([
|
|
406
|
+
{
|
|
407
|
+
description: "everything",
|
|
408
|
+
accept: {
|
|
409
|
+
"audio/*": [],
|
|
410
|
+
"video/*": [],
|
|
411
|
+
"image/*": [],
|
|
412
|
+
"text/*": [],
|
|
413
|
+
"application/zip": [],
|
|
414
|
+
},
|
|
415
|
+
},
|
|
416
|
+
]);
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it("should return empty otherwise", () => {
|
|
420
|
+
expect(utils.filePickerOptionsTypes("")).toEqual([
|
|
421
|
+
{
|
|
422
|
+
description: "everything",
|
|
423
|
+
accept: {},
|
|
424
|
+
},
|
|
425
|
+
]);
|
|
426
|
+
});
|
|
427
|
+
});
|
|
252
428
|
|
|
253
429
|
function createFile(name, size, type) {
|
|
254
|
-
const file = new File([], name, { type })
|
|
255
|
-
Object.defineProperty(file,
|
|
430
|
+
const file = new File([], name, { type });
|
|
431
|
+
Object.defineProperty(file, "size", {
|
|
256
432
|
get() {
|
|
257
|
-
return size
|
|
258
|
-
}
|
|
259
|
-
})
|
|
260
|
-
return file
|
|
433
|
+
return size;
|
|
434
|
+
},
|
|
435
|
+
});
|
|
436
|
+
return file;
|
|
261
437
|
}
|
|
262
|
-
|
|
263
|
-
describe('ErrorCode', () => {
|
|
264
|
-
let utils
|
|
265
|
-
beforeEach(async done => {
|
|
266
|
-
utils = await import('./index')
|
|
267
|
-
done()
|
|
268
|
-
})
|
|
269
|
-
|
|
270
|
-
it('should exist and have known error code properties', () => {
|
|
271
|
-
expect(utils.ErrorCode.FileInvalidType).toEqual(utils.FILE_INVALID_TYPE)
|
|
272
|
-
expect(utils.ErrorCode.FileTooLarge).toEqual(utils.FILE_TOO_LARGE)
|
|
273
|
-
expect(utils.ErrorCode.FileTooSmall).toEqual(utils.FILE_TOO_SMALL)
|
|
274
|
-
expect(utils.ErrorCode.TooManyFiles).toEqual(utils.TOO_MANY_FILES)
|
|
275
|
-
})
|
|
276
|
-
})
|
|
277
|
-
|