From 65c8e760de1b7c32a5c6db4d2ec0dfa86ab69cfb Mon Sep 17 00:00:00 2001
From: Jannik Stehle <jannik.stehle@gmail.com>
Date: Wed, 15 Mar 2023 13:42:02 +0100
Subject: [PATCH] Improve types of the loadingService

---
 .../src/helpers/resource/actions/transfer.ts          | 11 ++++-------
 packages/web-app-files/src/store/actions.ts           |  7 ++++---
 .../unit/helpers/resource/resourcesTransfer.spec.ts   |  4 ++--
 packages/web-pkg/src/services/loadingService.ts       | 10 +++++++---
 .../src/mocks/defaultComponentMocks.ts                |  4 ++--
 5 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/packages/web-app-files/src/helpers/resource/actions/transfer.ts b/packages/web-app-files/src/helpers/resource/actions/transfer.ts
index 83c5c778493..f6161402e7e 100644
--- a/packages/web-app-files/src/helpers/resource/actions/transfer.ts
+++ b/packages/web-app-files/src/helpers/resource/actions/transfer.ts
@@ -1,7 +1,7 @@
 import { Resource } from 'web-client'
 import { join } from 'path'
 import { SpaceResource } from 'web-client/src/helpers'
-import { ClientService, LoadingService, LoadingTaskState } from 'web-pkg/src/services'
+import { ClientService, LoadingService, LoadingTaskCallbackArguments } from 'web-pkg/src/services'
 import {
   ConflictDialog,
   ResolveStrategy,
@@ -120,12 +120,9 @@ export class ResourceTransfer extends ConflictDialog {
 
     return this.loadingService.addTask(
       ({ setProgress }) => {
-        return this.moveResources(
-          resolvedConflicts,
-          targetFolderResources,
-          transferType,
+        return this.moveResources(resolvedConflicts, targetFolderResources, transferType, {
           setProgress
-        )
+        })
       },
       { indeterminate: transferType === TransferType.COPY }
     )
@@ -135,7 +132,7 @@ export class ResourceTransfer extends ConflictDialog {
     resolvedConflicts: FileConflict[],
     targetFolderResources: Resource[],
     transferType: TransferType,
-    setProgress: (args: LoadingTaskState) => void
+    { setProgress }: LoadingTaskCallbackArguments
   ) {
     const movedResources: Resource[] = []
     const errors = []
diff --git a/packages/web-app-files/src/store/actions.ts b/packages/web-app-files/src/store/actions.ts
index 75787c49eb1..521b9dcf9ec 100644
--- a/packages/web-app-files/src/store/actions.ts
+++ b/packages/web-app-files/src/store/actions.ts
@@ -17,7 +17,7 @@ import {
   SpaceResource
 } from 'web-client/src/helpers'
 import { WebDAV } from 'web-client/src/webdav'
-import { ClientService, LoadingTaskState } from 'web-pkg/src/services'
+import { ClientService, LoadingTaskCallbackArguments } from 'web-pkg/src/services'
 import { Language } from 'vue3-gettext'
 import { DavProperty } from 'web-client/src/webdav/constants'
 import { AncestorMetaData } from 'web-app-files/src/helpers/resource/ancestorMetaData'
@@ -160,7 +160,7 @@ export default {
       space: SpaceResource
       files: Resource[]
       clientService: ClientService
-      setProgress: (args: LoadingTaskState) => void
+      loadingCallbackArgs: LoadingTaskCallbackArguments
       firstRun: boolean
     } & Language
   ) {
@@ -170,9 +170,10 @@ export default {
       space,
       files,
       clientService,
-      setProgress,
+      loadingCallbackArgs,
       firstRun = true
     } = options
+    const { setProgress } = loadingCallbackArgs
     const promises = []
     const removedFiles = []
     for (const file of files) {
diff --git a/packages/web-app-files/tests/unit/helpers/resource/resourcesTransfer.spec.ts b/packages/web-app-files/tests/unit/helpers/resource/resourcesTransfer.spec.ts
index e47714cf674..5f501a0ae70 100644
--- a/packages/web-app-files/tests/unit/helpers/resource/resourcesTransfer.spec.ts
+++ b/packages/web-app-files/tests/unit/helpers/resource/resourcesTransfer.spec.ts
@@ -1,4 +1,4 @@
-import { ClientService, LoadingService } from 'web-pkg/src/services'
+import { ClientService, LoadingService, LoadingTaskCallbackArguments } from 'web-pkg/src/services'
 import {
   ResolveConflict,
   ResourceTransfer,
@@ -12,7 +12,7 @@ import { ListFilesResult } from 'web-client/src/webdav/listFiles'
 const clientServiceMock = mockDeep<ClientService>()
 const loadingServiceMock = mock<LoadingService>({
   addTask: (callback) => {
-    return callback({ setProgress: jest.fn() })
+    return callback(mock<LoadingTaskCallbackArguments>())
   }
 })
 let resourcesToMove
diff --git a/packages/web-pkg/src/services/loadingService.ts b/packages/web-pkg/src/services/loadingService.ts
index 44e6ed50d6c..7cb30e50956 100644
--- a/packages/web-pkg/src/services/loadingService.ts
+++ b/packages/web-pkg/src/services/loadingService.ts
@@ -19,6 +19,10 @@ export interface LoadingTask {
   state?: LoadingTaskState
 }
 
+export interface LoadingTaskCallbackArguments {
+  setProgress: (args: LoadingTaskState) => void
+}
+
 // time until a loading task is being set active
 const DEFAULT_DEBOUNCE_TIME = 200
 
@@ -51,13 +55,13 @@ export class LoadingService {
     return Math.round((progress / tasks.length) * 100)
   }
 
-  public addTask(
-    callback: ({ setProgress }: { setProgress: (args: LoadingTaskState) => void }) => Promise<any>,
+  public addTask<T>(
+    callback: ({ setProgress }: LoadingTaskCallbackArguments) => Promise<T>,
     {
       debounceTime = DEFAULT_DEBOUNCE_TIME,
       indeterminate = true
     }: { debounceTime?: number; indeterminate?: boolean } = {}
-  ): Promise<any> {
+  ): Promise<T> {
     const task = {
       id: uuid.v4(),
       active: false,
diff --git a/packages/web-test-helpers/src/mocks/defaultComponentMocks.ts b/packages/web-test-helpers/src/mocks/defaultComponentMocks.ts
index b6c26333212..e8ae4971c52 100644
--- a/packages/web-test-helpers/src/mocks/defaultComponentMocks.ts
+++ b/packages/web-test-helpers/src/mocks/defaultComponentMocks.ts
@@ -1,5 +1,5 @@
 import { mock, mockDeep } from 'jest-mock-extended'
-import { ClientService, LoadingService } from 'web-pkg/src/services'
+import { ClientService, LoadingService, LoadingTaskCallbackArguments } from 'web-pkg/src/services'
 import { Router, RouteLocationNormalizedLoaded, RouteLocationRaw, RouteLocation } from 'vue-router'
 import { UppyService } from 'web-runtime/src/services/uppyService'
 import { OwnCloudSdk } from 'web-client/src/types'
@@ -25,7 +25,7 @@ export const defaultComponentMocks = ({ currentRoute = undefined }: ComponentMoc
     $uppyService: mockDeep<UppyService>(),
     $loadingService: mock<LoadingService>({
       addTask: (callback) => {
-        return callback({ setProgress: jest.fn() })
+        return callback(mock<LoadingTaskCallbackArguments>())
       }
     })
   }