-
-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathfileAddSpec.js
More file actions
224 lines (199 loc) · 6.81 KB
/
Copy pathfileAddSpec.js
File metadata and controls
224 lines (199 loc) · 6.81 KB
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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
describe('fileAdd event', function() {
/**
* @type {Flow}
*/
var flow;
beforeEach(function () {
flow = new Flow({
generateUniqueIdentifier: function (file) {
return file.size;
}
});
});
it('should call file-added event', async function() {
var valid = false;
flow.on('file-added', (file) => {
expect(file.file instanceof Blob).toBeTruthy();
valid = true;
});
await flow.addFile(new Blob(['file part']));
expect(valid).toBeTruthy();
});
it('should call files-added event', async function() {
var count = 0;
flow.on('files-added', (files) => {
count = files.length;
});
await flow.addFiles([
new Blob(['file part']),
new Blob(['file 2 part'])
]);
expect(count).toBe(2);
expect(flow.files.length).toBe(2);
});
it('should call file-added only when bound', async function() {
var event = jasmine.createSpy('event');
flow.on('file-added', event);
await flow.addFile(new File(['file part'], 'a.bin'));
expect(event).toHaveBeenCalledTimes(1);
flow.off('file-added');
await flow.addFile(new File(['file part'], 'b.bin'));
expect(event).toHaveBeenCalledTimes(1);
});
it('file-added can be removed specifying the callback', async function() {
var event = jasmine.createSpy('event');
flow.on('file-added', event);
await flow.addFile(new File(['file part'], 'c.bin'));
expect(event).toHaveBeenCalledTimes(1);
flow.off('file-added', event);
await flow.addFile(new File(['file part'], 'd.bin'));
expect(event).toHaveBeenCalledTimes(1);
});
it('should validate file-added', async function() {
flow.on('file-added', (file) => {
delete file.file;
return false;
});
await flow.addFile(new Blob(['file part']));
expect(flow.files.length).toBe(0);
});
it('should validate async file-added', async function() {
flow.on('file-added', async (file) => {
await sleep(200);
delete file.file;
return false;
});
await flow.addFile(new Blob(['file part']));
expect(flow.files.length).toBe(0);
});
it('should keeps file from being queued', async function() {
spyOn(console, 'warn');
flow.on('filter-file', () => false);
await flow.addFile(new Blob(['file part']));
expect(flow.files.length).toBe(0);
expect(console.warn).not.toHaveBeenCalled();
});
it('should validate filter-file and files-added', async function() {
flow.on('filter-file', () => false);
var valid = false;
flow.on('files-added', (files) => {
valid = files.length === 0;
});
await flow.addFile(new Blob(['file part']));
expect(valid).toBeTruthy();
});
it('should validate file-added filtering before files-added', async function() {
var valid = false;
flow.on('file-added', (flowFile) => {
if(flowFile.name === 'f2') {
delete flowFile.file;
}
});
flow.on('files-added', (files) => {
valid = files.length === 1;
});
await flow.addFiles([
new File(['file'], 'f1'),
new File(['file2'], 'f2')
]);
expect(valid).toBeTruthy();
});
it('should validate multiple filter-file hooks', async function() {
const customFunction = jasmine.createSpy('fn');
flow.on('filter-file', async () => {
customFunction();
return true;
});
flow.on('filter-file', async () => {
customFunction();
return false; // a single hook returning false should prevail
});
flow.on('filter-file', async () => {
customFunction();
return true;
});
let valid = false;
flow.on('files-added', (files) => {
valid = files.length === 0;
});
await flow.addFile(new Blob(['file part']));
expect(valid).toBeTruthy();
expect(customFunction).toHaveBeenCalledTimes(3);
});
describe('async/sync hooks', function () {
beforeAll(function() {
jasmine.getEnv().addReporter({
specStarted: result => (jasmine.currentTest = result),
specDone: result => (jasmine.currentTest = result),
});
});
it('An async files-added hook', async function() {
var flowfiles,
content = gen_file(4, 512),
sample_file = new File([content], `foobar-asyncInitFileFn-${jasmine.currentTest.id}.bin`),
customFunction1 = jasmine.createSpy('fn'),
customFunction2 = jasmine.createSpy('fn');
flow.on('files-added', (files) => {
customFunction1();
});
flow.on('files-added', async (files) => {
await sleep(250);
customFunction2();
});
flowfiles = await flow.addFiles([sample_file]);
expect(customFunction1).toHaveBeenCalledTimes(1);
expect(customFunction2).toHaveBeenCalledTimes(1);
});
it('file-added hook can actually change one file', async () => {
flow.on('file-added', (flowfile) => {
// Synchronous hook changes the filename of the first file
if (flowfile.name.includes('aaa')) {
flowfile.name = 'bbb.bin';
}
});
flow.on('file-added', async (flowfile) => {
await sleep(250);
// Asynchronous hook replace the content
var text = (await flowfile.file.text()).replace(/a/g, 'x');
flowfile.file = new File([text], flowfile.name);
});
var flowfiles = await flow.addFiles([
new File(['aaa'], 'aaa.bin'),
new File(['GGG'], 'GGG.bin'),
]);
expect(flowfiles[0].name).toBe('bbb.bin');
await expectAsync(flowfiles[0].file.text()).toBeResolvedTo('xxx');
});
it('A files-added hook can actually change files', async function() {
var flowfiles,
customFunction = jasmine.createSpy('fn'),
files = [
new File(['abc'], `foobar-abc-${jasmine.currentTest.id}.bin`),
new File(['def'], `foobar-def-${jasmine.currentTest.id}.bin`)
];
flow.on('files-added', (files, event) => {
customFunction();
expect(files.length).toEqual(2);
files.reverse();
});
flowfiles = await flow.addFiles(files);
expect(customFunction).toHaveBeenCalledTimes(1);
expect(flowfiles.length).toEqual(2);
// The files have been inverted by the hook
expect(flowfiles[0].name).toEqual(`foobar-def-${jasmine.currentTest.id}.bin`);
});
it('A files-added hook can actually change files', async function() {
var flowfiles,
customFunction = jasmine.createSpy('fn'),
files = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu']
.map(e => new File([e], `foobar-${e}-${jasmine.currentTest.id}.bin`));
flow.on('filter-file', (flowFile, event) => {
customFunction();
return /\b(abc|def)\b/.test(flowFile.name);
});
flowfiles = await flow.addFiles(files);
expect(customFunction).toHaveBeenCalledTimes(7);
expect(flowfiles.length).toEqual(2);
});
});
});