Repository: parse-community/Parse-SDK-JS
Pull request #1140 (2020-04-07 08:17:22)

Fix File Progress Type
According to @ladigitale from [last PR](https://github.com/parse-community/Parse-SDK-JS/pull/1133)

> I just found the time to test the pacth.
> As expected at the end of the upload with this code you will yet have 2 consecutive callbacks of the same function with different meanings :
> 
> 1 14535341 14535341 (meaning end of the upload)
> 1 165 165 (meaning end of the download sent as one chunk because of the low size of the response)
> 
> Thats why aditionnal imformations / api evolution would have been usefull.



JeromeDeLeon: I was pushing it at the beginning, or

```js
progress(event.loaded / event.total, {
  type,
  event
});
```
ladigitale: Is there any issue to put the type at last position, for backward compatibility ?

codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1140?src=pr&el=h1) Report
> Merging [#1140](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1140?src=pr&el=desc) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/b9e360f33060a51e4257afed359da88528d243fc&el=desc) will **not change** coverage by `%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1140/graphs/tree.svg?width=650&height=150&src=pr&token=8nCENM0QHj)](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1140?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master    #1140   +/-   ##
=======================================
  Coverage   92.30%   92.30%           
=======================================
  Files          54       54           
  Lines        5235     5235           
  Branches     1169     1169           
=======================================
  Hits         4832     4832           
  Misses        403      403           
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1140?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/RESTController.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1140/diff?src=pr&el=tree#diff-c3JjL1JFU1RDb250cm9sbGVyLmpz) | `84.51% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1140?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1140?src=pr&el=footer). Last update [b9e360f...d7233f0](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1140?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/ParseFile.js b/src/ParseFile.js
index eb69c62..6c70304 100644
--- a/src/ParseFile.js
+++ b/src/ParseFile.js
@@ -233,7 +233,17 @@ class ParseFile {
    *     be used for this request.
    *   <li>sessionToken: A valid session token, used for making a request on
    *     behalf of a specific user.
-   *   <li>progress: In Browser only, callback for upload progress
+   *   <li>progress: In Browser only, callback for upload progress. For example:
+   * <pre>
+   * let parseFile = new Parse.File(name, file);
+   * parseFile.save({
+   *   progress: (progressValue, loaded, total, { type }) => {
+   *     if (type === "upload" && progressValue !== null) {
+   *       // Update the UI using progressValue
+   *     }
+   *   }
+   * });
+   * </pre>
    * </ul>
    * @return {Promise} Promise that is resolved when the save finishes.
    */
diff --git a/src/RESTController.js b/src/RESTController.js
index d24226b..1b3006a 100644
--- a/src/RESTController.js
+++ b/src/RESTController.js
@@ -159,27 +159,28 @@ const RESTController = {
         headers[key] = customHeaders[key];
       }
 
-      function handleProgress(event) {
+      function handleProgress(type, event) {
         if (options && typeof options.progress === 'function') {
           if (event.lengthComputable) {
             options.progress(
               event.loaded / event.total,
               event.loaded,
-              event.total
+              event.total,
+              { type }
             );
           } else {
-            options.progress(null);
+            options.progress(null, null, null, { type });
           }
         }
       }
 
       xhr.onprogress = (event) => {
-        handleProgress(event);
+        handleProgress('download', event);
       };
 
       if (xhr.upload) {
         xhr.upload.onprogress = (event) => {
-          handleProgress(event);
+          handleProgress('upload', event);
         }
       }
 


Test patch:
diff --git a/src/__tests__/ParseFile-test.js b/src/__tests__/ParseFile-test.js
index 4c14d50..5497c52 100644
--- a/src/__tests__/ParseFile-test.js
+++ b/src/__tests__/ParseFile-test.js
@@ -30,7 +30,7 @@ jest.setMock('../LocalDatastore', mockLocalDatastore);
 function generateSaveMock(prefix) {
   return function(name, payload, options) {
     if (options && typeof options.progress === 'function') {
-      options.progress(0.5, 5, 10);
+      options.progress(0.5, 5, 10, { type: 'upload' });
     }
     return Promise.resolve({
       name: name,
@@ -236,7 +236,7 @@ describe('ParseFile', () => {
     jest.spyOn(options, 'progress');
 
     return file.save(options).then(function(f) {
-      expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10);
+      expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10, { type: 'upload' });
       expect(f).toBe(file);
       expect(f.name()).toBe('progress.txt');
       expect(f.url()).toBe('http://files.parsetfss.com/a/progress.txt');
diff --git a/src/__tests__/RESTController-test.js b/src/__tests__/RESTController-test.js
index 7859a12..89352ab 100644
--- a/src/__tests__/RESTController-test.js
+++ b/src/__tests__/RESTController-test.js
@@ -444,8 +444,8 @@ describe('RESTController', () => {
     jest.spyOn(options, 'progress');
 
     RESTController.ajax('POST', 'files/upload.txt', {}, {}, options).then(({ response, status }) => {
-      expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10);
-      expect(options.progress).toHaveBeenCalledTimes(2);
+      expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10, { type: 'download' });
+      expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10, { type: 'upload' });
       expect(response).toEqual({ success: true });
       expect(status).toBe(200);
       done();
@@ -468,7 +468,7 @@ describe('RESTController', () => {
     jest.spyOn(options, 'progress');
 
     RESTController.ajax('POST', 'files/upload.txt', {}, {}, options).then(({ response, status }) => {
-      expect(options.progress).toHaveBeenCalledWith(null);
+      expect(options.progress).toHaveBeenCalledWith(null, null, null, { type: 'upload' });
       expect(response).toEqual({ success: true });
       expect(status).toBe(200);
       done();