react-dropzone 11.7.0 → 12.0.2
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/.eslintrc +1 -3
- package/README.md +6 -1
- package/commitlint.config.js +1 -1
- package/dist/es/index.js +55 -43
- package/dist/es/utils/index.js +22 -22
- package/dist/index.js +3 -3
- package/examples/accept/README.md +10 -3
- package/examples/no-jsx/README.md +32 -0
- package/package.json +7 -6
- package/rollup.config.js +20 -20
- package/src/.eslintrc +1 -2
- package/src/index.js +330 -277
- package/src/index.spec.js +1784 -1498
- package/src/utils/index.js +100 -73
- package/src/utils/index.spec.js +386 -289
- package/styleguide.config.js +57 -50
- package/testSetup.js +7 -1
- package/typings/.eslintrc +1 -1
- package/typings/react-dropzone.d.ts +25 -14
- package/typings/tests/accept.tsx +10 -9
- package/typings/tests/all.tsx +7 -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/src/utils/index.spec.js
CHANGED
|
@@ -1,340 +1,437 @@
|
|
|
1
1
|
beforeEach(() => {
|
|
2
|
-
jest.resetModules()
|
|
3
|
-
})
|
|
2
|
+
jest.resetModules();
|
|
3
|
+
});
|
|
4
4
|
|
|
5
|
-
describe(
|
|
6
|
-
let utils
|
|
5
|
+
describe("fileMatchSize()", () => {
|
|
6
|
+
let utils;
|
|
7
7
|
beforeEach(async () => {
|
|
8
|
-
utils = await import(
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
it(
|
|
12
|
-
expect(utils.fileMatchSize({})).toEqual([true, null])
|
|
13
|
-
expect(utils.fileMatchSize({size: null})).toEqual([true, null])
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
it(
|
|
17
|
-
expect(utils.fileMatchSize({size: 100})).toEqual([true, null])
|
|
18
|
-
expect(utils.fileMatchSize({size: 100}, undefined, undefined)).toEqual([
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it(
|
|
29
|
-
expect(utils.fileMatchSize({size: 100},
|
|
30
|
-
expect(utils.fileMatchSize({size: 100},
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
expect(utils.fileMatchSize({size: 100},
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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;
|
|
43
67
|
beforeEach(async () => {
|
|
44
|
-
utils = await import(
|
|
45
|
-
})
|
|
68
|
+
utils = await import("./index");
|
|
69
|
+
});
|
|
46
70
|
|
|
47
|
-
it(
|
|
71
|
+
it("should return true for IE10", () => {
|
|
48
72
|
const userAgent =
|
|
49
|
-
|
|
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)";
|
|
50
74
|
|
|
51
|
-
expect(utils.isIeOrEdge(userAgent)).toBe(true)
|
|
52
|
-
})
|
|
75
|
+
expect(utils.isIeOrEdge(userAgent)).toBe(true);
|
|
76
|
+
});
|
|
53
77
|
|
|
54
|
-
it(
|
|
78
|
+
it("should return true for IE11", () => {
|
|
55
79
|
const userAgent =
|
|
56
|
-
|
|
57
|
-
expect(utils.isIeOrEdge(userAgent)).toBe(true)
|
|
58
|
-
})
|
|
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
|
+
});
|
|
59
83
|
|
|
60
|
-
it(
|
|
84
|
+
it("should return true for Edge", () => {
|
|
61
85
|
const userAgent =
|
|
62
|
-
|
|
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";
|
|
63
87
|
|
|
64
|
-
expect(utils.isIeOrEdge(userAgent)).toBe(true)
|
|
65
|
-
})
|
|
88
|
+
expect(utils.isIeOrEdge(userAgent)).toBe(true);
|
|
89
|
+
});
|
|
66
90
|
|
|
67
|
-
it(
|
|
91
|
+
it("should return false for Chrome", () => {
|
|
68
92
|
const userAgent =
|
|
69
|
-
|
|
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";
|
|
70
94
|
|
|
71
|
-
expect(utils.isIeOrEdge(userAgent)).toBe(false)
|
|
72
|
-
})
|
|
73
|
-
})
|
|
95
|
+
expect(utils.isIeOrEdge(userAgent)).toBe(false);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
74
98
|
|
|
75
|
-
describe(
|
|
99
|
+
describe("isKindFile()", () => {
|
|
76
100
|
it('should return true for DataTransferItem of kind "file"', async () => {
|
|
77
|
-
const utils = await import(
|
|
78
|
-
expect(utils.isKindFile({ kind:
|
|
79
|
-
expect(utils.isKindFile({ kind:
|
|
80
|
-
expect(utils.isKindFile({})).toBe(false)
|
|
81
|
-
expect(utils.isKindFile(null)).toBe(false)
|
|
82
|
-
})
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
describe(
|
|
86
|
-
const trueFn = jest.fn(() => true)
|
|
87
|
-
|
|
88
|
-
let utils
|
|
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;
|
|
89
113
|
beforeEach(async () => {
|
|
90
|
-
utils = await import(
|
|
91
|
-
})
|
|
114
|
+
utils = await import("./index");
|
|
115
|
+
});
|
|
92
116
|
|
|
93
|
-
it(
|
|
94
|
-
expect(utils.isPropagationStopped({ isPropagationStopped: trueFn })).toBe(
|
|
95
|
-
|
|
117
|
+
it("should return result of isPropagationStopped() if isPropagationStopped exists", () => {
|
|
118
|
+
expect(utils.isPropagationStopped({ isPropagationStopped: trueFn })).toBe(
|
|
119
|
+
true
|
|
120
|
+
);
|
|
121
|
+
});
|
|
96
122
|
|
|
97
|
-
it(
|
|
98
|
-
expect(utils.isPropagationStopped({ cancelBubble: true })).toBe(true)
|
|
99
|
-
})
|
|
123
|
+
it("should return value of cancelBubble if isPropagationStopped doesnt exist and cancelBubble exists", () => {
|
|
124
|
+
expect(utils.isPropagationStopped({ cancelBubble: true })).toBe(true);
|
|
125
|
+
});
|
|
100
126
|
|
|
101
|
-
it(
|
|
102
|
-
expect(utils.isPropagationStopped({})).toBe(false)
|
|
103
|
-
})
|
|
104
|
-
})
|
|
127
|
+
it("should return false if isPropagationStopped and cancelBubble are missing", () => {
|
|
128
|
+
expect(utils.isPropagationStopped({})).toBe(false);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
105
131
|
|
|
106
|
-
describe(
|
|
107
|
-
let utils
|
|
132
|
+
describe("isEvtWithFiles()", () => {
|
|
133
|
+
let utils;
|
|
108
134
|
beforeEach(async () => {
|
|
109
|
-
utils = await import(
|
|
110
|
-
})
|
|
135
|
+
utils = await import("./index");
|
|
136
|
+
});
|
|
111
137
|
|
|
112
|
-
it(
|
|
113
|
-
expect(utils.isEvtWithFiles({ dataTransfer: { types: [
|
|
114
|
-
|
|
138
|
+
it("should return true if some dragged types are files", () => {
|
|
139
|
+
expect(utils.isEvtWithFiles({ dataTransfer: { types: ["Files"] } })).toBe(
|
|
140
|
+
true
|
|
141
|
+
);
|
|
115
142
|
expect(
|
|
116
143
|
utils.isEvtWithFiles({
|
|
117
|
-
dataTransfer: { types: [
|
|
144
|
+
dataTransfer: { types: ["application/x-moz-file"] },
|
|
118
145
|
})
|
|
119
|
-
).toBe(true)
|
|
120
|
-
expect(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
)
|
|
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);
|
|
125
163
|
expect(
|
|
126
164
|
utils.isEvtWithFiles({
|
|
127
|
-
dataTransfer: { types: [
|
|
165
|
+
dataTransfer: { types: ["application/x-moz-file", "application/test"] },
|
|
128
166
|
})
|
|
129
|
-
).toBe(true)
|
|
130
|
-
})
|
|
167
|
+
).toBe(true);
|
|
168
|
+
});
|
|
131
169
|
|
|
132
|
-
it(
|
|
133
|
-
expect(utils.isEvtWithFiles({ target: { files: [] } })).toBe(true)
|
|
134
|
-
})
|
|
170
|
+
it("should return true if the event has a target with files", () => {
|
|
171
|
+
expect(utils.isEvtWithFiles({ target: { files: [] } })).toBe(true);
|
|
172
|
+
});
|
|
135
173
|
|
|
136
|
-
it(
|
|
137
|
-
expect(utils.isEvtWithFiles({})).toBe(false)
|
|
138
|
-
})
|
|
139
|
-
})
|
|
174
|
+
it("should return false otherwise", () => {
|
|
175
|
+
expect(utils.isEvtWithFiles({})).toBe(false);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
140
178
|
|
|
141
|
-
describe(
|
|
142
|
-
let utils
|
|
179
|
+
describe("composeEventHandlers", () => {
|
|
180
|
+
let utils;
|
|
143
181
|
beforeEach(async () => {
|
|
144
|
-
utils = await import(
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
it(
|
|
148
|
-
const fn = utils.composeEventHandlers(() => {})
|
|
149
|
-
expect(typeof fn).toBe(
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
it(
|
|
153
|
-
const fn1 = jest.fn()
|
|
154
|
-
const fn2 = jest.fn()
|
|
155
|
-
const fn = utils.composeEventHandlers(fn1, fn2)
|
|
156
|
-
const event = { type:
|
|
157
|
-
const data = { ping: true }
|
|
158
|
-
fn(event, data)
|
|
159
|
-
expect(fn1).toHaveBeenCalledWith(event, data)
|
|
160
|
-
expect(fn2).toHaveBeenCalledWith(event, data)
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
it(
|
|
164
|
-
const fn1 = jest.fn().mockImplementation(event => {
|
|
165
|
-
Object.defineProperty(event,
|
|
166
|
-
return event
|
|
167
|
-
})
|
|
168
|
-
const fn2 = jest.fn()
|
|
169
|
-
const fn = utils.composeEventHandlers(fn1, fn2)
|
|
170
|
-
const event = new MouseEvent(
|
|
171
|
-
fn(event)
|
|
172
|
-
expect(fn1).toHaveBeenCalledWith(event)
|
|
173
|
-
expect(fn2).not.toHaveBeenCalled()
|
|
174
|
-
})
|
|
175
|
-
|
|
176
|
-
it(
|
|
177
|
-
const fn1 = jest.fn()
|
|
178
|
-
const fn2 = jest.fn()
|
|
179
|
-
const fn = utils.composeEventHandlers(fn1, fn2)
|
|
180
|
-
const event = new MouseEvent(
|
|
181
|
-
Object.defineProperty(event,
|
|
182
|
-
fn(event)
|
|
183
|
-
expect(fn1).not.toHaveBeenCalled()
|
|
184
|
-
expect(fn2).not.toHaveBeenCalled()
|
|
185
|
-
})
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
describe(
|
|
189
|
-
let utils
|
|
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;
|
|
190
228
|
beforeEach(async () => {
|
|
191
|
-
utils = await import(
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
it(
|
|
195
|
-
const file = createFile(
|
|
196
|
-
expect(utils.fileAccepted(file,
|
|
197
|
-
})
|
|
198
|
-
|
|
199
|
-
it(
|
|
200
|
-
const file = createFile(
|
|
201
|
-
expect(utils.fileAccepted(file,
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
it(
|
|
205
|
-
const file = createFile(
|
|
206
|
-
expect(utils.fileAccepted(file, [
|
|
207
|
-
})
|
|
208
|
-
|
|
209
|
-
it(
|
|
210
|
-
const file = createFile(
|
|
211
|
-
expect(utils.fileAccepted(file,
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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;
|
|
228
277
|
beforeEach(async () => {
|
|
229
|
-
utils = await import(
|
|
230
|
-
})
|
|
231
|
-
it(
|
|
232
|
-
const files = [
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
expect(utils.allFilesAccepted({ files, multiple: true
|
|
241
|
-
expect(
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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/*",
|
|
304
|
+
})
|
|
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;
|
|
247
320
|
beforeEach(async () => {
|
|
248
|
-
utils = await import(
|
|
249
|
-
})
|
|
250
|
-
|
|
251
|
-
it(
|
|
252
|
-
expect(utils.ErrorCode.FileInvalidType).toEqual(utils.FILE_INVALID_TYPE)
|
|
253
|
-
expect(utils.ErrorCode.FileTooLarge).toEqual(utils.FILE_TOO_LARGE)
|
|
254
|
-
expect(utils.ErrorCode.FileTooSmall).toEqual(utils.FILE_TOO_SMALL)
|
|
255
|
-
expect(utils.ErrorCode.TooManyFiles).toEqual(utils.TOO_MANY_FILES)
|
|
256
|
-
})
|
|
257
|
-
})
|
|
258
|
-
|
|
259
|
-
describe(
|
|
260
|
-
let utils
|
|
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;
|
|
261
334
|
beforeEach(async () => {
|
|
262
|
-
utils = await import(
|
|
263
|
-
})
|
|
335
|
+
utils = await import("./index");
|
|
336
|
+
});
|
|
264
337
|
|
|
265
|
-
it(
|
|
266
|
-
expect(utils.canUseFileSystemAccessAPI()).toBe(false)
|
|
267
|
-
})
|
|
338
|
+
it("should return false if not", () => {
|
|
339
|
+
expect(utils.canUseFileSystemAccessAPI()).toBe(false);
|
|
340
|
+
});
|
|
268
341
|
|
|
269
|
-
it(
|
|
342
|
+
it("should return true if yes", () => {
|
|
270
343
|
// TODO: If we use these in other tests, restore once test is done
|
|
271
|
-
window.showOpenFilePicker = jest.fn()
|
|
272
|
-
expect(utils.canUseFileSystemAccessAPI()).toBe(true)
|
|
273
|
-
})
|
|
274
|
-
})
|
|
344
|
+
window.showOpenFilePicker = jest.fn();
|
|
345
|
+
expect(utils.canUseFileSystemAccessAPI()).toBe(true);
|
|
346
|
+
});
|
|
347
|
+
});
|
|
275
348
|
|
|
276
|
-
describe(
|
|
277
|
-
let utils
|
|
349
|
+
describe("filePickerOptionsTypes()", () => {
|
|
350
|
+
let utils;
|
|
278
351
|
beforeEach(async () => {
|
|
279
|
-
utils = await import(
|
|
280
|
-
})
|
|
281
|
-
|
|
282
|
-
it(
|
|
283
|
-
expect(utils.filePickerOptionsTypes(
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
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
|
+
});
|
|
298
377
|
|
|
299
378
|
it("should exclude anything that's not a MIME type", () => {
|
|
300
|
-
expect(
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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
|
+
});
|
|
310
399
|
|
|
311
400
|
it("should work with comma separated string of MIME types", () => {
|
|
312
|
-
expect(
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
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
|
+
});
|
|
331
428
|
|
|
332
429
|
function createFile(name, size, type) {
|
|
333
|
-
const file = new File([], name, { type })
|
|
334
|
-
Object.defineProperty(file,
|
|
430
|
+
const file = new File([], name, { type });
|
|
431
|
+
Object.defineProperty(file, "size", {
|
|
335
432
|
get() {
|
|
336
|
-
return size
|
|
337
|
-
}
|
|
338
|
-
})
|
|
339
|
-
return file
|
|
433
|
+
return size;
|
|
434
|
+
},
|
|
435
|
+
});
|
|
436
|
+
return file;
|
|
340
437
|
}
|