Skip to content

Commit b9e360f

Browse files
authored
Fix file upload progress (#1133)
* Add upload progress * Remove type parameter * Fix ParseFile test * Fix RESTController test
1 parent 2081dc2 commit b9e360f

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
lines changed

package-lock.json

Lines changed: 14 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RESTController.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,31 @@ const RESTController = {
158158
for (const key in customHeaders) {
159159
headers[key] = customHeaders[key];
160160
}
161-
xhr.onprogress = (event) => {
162-
if(options && typeof options.progress === 'function') {
161+
162+
function handleProgress(event) {
163+
if (options && typeof options.progress === 'function') {
163164
if (event.lengthComputable) {
164-
options.progress(event.loaded / event.total, event.loaded, event.total);
165+
options.progress(
166+
event.loaded / event.total,
167+
event.loaded,
168+
event.total
169+
);
165170
} else {
166171
options.progress(null);
167172
}
168173
}
174+
}
175+
176+
xhr.onprogress = (event) => {
177+
handleProgress(event);
169178
};
179+
180+
if (xhr.upload) {
181+
xhr.upload.onprogress = (event) => {
182+
handleProgress(event);
183+
}
184+
}
185+
170186
xhr.open(method, url, true);
171187

172188
for (const h in headers) {

src/__tests__/ParseFile-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jest.setMock('../LocalDatastore', mockLocalDatastore);
3030
function generateSaveMock(prefix) {
3131
return function(name, payload, options) {
3232
if (options && typeof options.progress === 'function') {
33-
options.progress(0.5);
33+
options.progress(0.5, 5, 10);
3434
}
3535
return Promise.resolve({
3636
name: name,
@@ -236,7 +236,7 @@ describe('ParseFile', () => {
236236
jest.spyOn(options, 'progress');
237237

238238
return file.save(options).then(function(f) {
239-
expect(options.progress).toHaveBeenCalledWith(0.5);
239+
expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10);
240240
expect(f).toBe(file);
241241
expect(f.name()).toBe('progress.txt');
242242
expect(f.url()).toBe('http://files.parsetfss.com/a/progress.txt');

src/__tests__/RESTController-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ describe('RESTController', () => {
428428
CoreManager.set('SERVER_AUTH_TOKEN', null);
429429
});
430430

431-
it('reports upload progress of the AJAX request when callback is provided', (done) => {
431+
it('reports upload/download progress of the AJAX request when callback is provided', (done) => {
432432
const xhr = mockXHR([{ status: 200, response: { success: true } }], {
433433
progress: {
434434
lengthComputable: true,
@@ -445,6 +445,7 @@ describe('RESTController', () => {
445445

446446
RESTController.ajax('POST', 'files/upload.txt', {}, {}, options).then(({ response, status }) => {
447447
expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10);
448+
expect(options.progress).toHaveBeenCalledTimes(2);
448449
expect(response).toEqual({ success: true });
449450
expect(status).toBe(200);
450451
done();

src/__tests__/test_helpers/mockXHR.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ function mockXHR(results, options = {}) {
2222
XHR.prototype = {
2323
open: function() { },
2424
setRequestHeader: function() { },
25+
upload: function() { },
2526
send: function() {
2627
this.status = results[attempts].status;
2728
this.responseText = JSON.stringify(results[attempts].response || {});
2829
this.readyState = 4;
2930
attempts++;
3031
this.onreadystatechange();
3132
this.onprogress(options.progress);
33+
this.upload.onprogress(options.progress);
3234
}
3335
};
3436
return XHR;

0 commit comments

Comments
 (0)