Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/RESTController.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,24 +169,24 @@ const RESTController = {
headers[key] = customHeaders[key];
}

function handleProgress(type, event) {
if (options && typeof options.progress === 'function') {
if (options && typeof options.progress === 'function') {
const handleProgress = function (type, event) {
if (event.lengthComputable) {
options.progress(event.loaded / event.total, event.loaded, event.total, { type });
} else {
options.progress(null, null, null, { type });
}
}
}

xhr.onprogress = event => {
handleProgress('download', event);
};
};

if (xhr.upload) {
xhr.upload.onprogress = event => {
handleProgress('upload', event);
xhr.onprogress = event => {
handleProgress('download', event);
};

if (xhr.upload) {
xhr.upload.onprogress = event => {
handleProgress('upload', event);
};
}
}

xhr.open(method, url, true);
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/RESTController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,20 @@ describe('RESTController', () => {
);
});

it('does not set upload progress listener when callback is not provided to avoid CORS pre-flight', () => {
const xhr = {
setRequestHeader: jest.fn(),
open: jest.fn(),
upload: jest.fn(),
send: jest.fn(),
};
RESTController._setXHR(function () {
return xhr;
});
RESTController.ajax('POST', 'users', {});
expect(xhr.upload.onprogress).toBeUndefined();
});

it('does not upload progress when total is uncomputable', done => {
const xhr = mockXHR([{ status: 200, response: { success: true } }], {
progress: {
Expand Down
10 changes: 8 additions & 2 deletions src/__tests__/test_helpers/mockXHR.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ function mockXHR(results, options = {}) {
this.readyState = 4;
attempts++;
this.onreadystatechange();
this.onprogress(options.progress);
this.upload.onprogress(options.progress);

if (typeof this.onprogress === 'function') {
this.onprogress(options.progress);
}

if (typeof this.upload.onprogress === 'function') {
this.upload.onprogress(options.progress);
}
},
};
return XHR;
Expand Down