From 42547e5df0c9e6f0ac00172134a4e2f1ff682955 Mon Sep 17 00:00:00 2001 From: Jannik Stehle Date: Wed, 15 Mar 2023 13:42:02 +0100 Subject: [PATCH] Improve types of the loadingService --- .../src/helpers/resource/actions/transfer.ts | 11 ++++------- .../web-app-files/src/mixins/actions/restore.ts | 8 +++++--- .../web-app-files/src/mixins/deleteResources.ts | 4 ++-- packages/web-app-files/src/store/actions.ts | 7 ++++--- .../unit/mixins/actions/emptyTrashBin.spec.ts | 4 ++-- .../tests/unit/mixins/actions/restore.spec.ts | 15 ++++++++++++--- packages/web-pkg/src/services/loadingService.ts | 10 +++++++--- .../src/mocks/defaultComponentMocks.ts | 4 ++-- 8 files changed, 38 insertions(+), 25 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 edc3ecb0d2c..82e3d5da69a 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: false } ) @@ -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/mixins/actions/restore.ts b/packages/web-app-files/src/mixins/actions/restore.ts index 8ad35cc862b..4734f2b8df1 100644 --- a/packages/web-app-files/src/mixins/actions/restore.ts +++ b/packages/web-app-files/src/mixins/actions/restore.ts @@ -2,7 +2,7 @@ import { mapActions, mapGetters, mapMutations, mapState } from 'vuex' import { dirname } from 'path' import { isLocationTrashActive } from '../../router' -import { clientService, LoadingTaskState } from 'web-pkg/src/services' +import { clientService, LoadingTaskCallbackArguments } from 'web-pkg/src/services' import { Resource, isProjectSpaceResource, extractExtensionFromFile } from 'web-client/src/helpers' import { ResolveStrategy, @@ -168,7 +168,7 @@ export default { async $_restore_restoreResources( resources: Resource[], missingFolderPaths: string[], - setProgress: (args: LoadingTaskState) => void + { setProgress }: LoadingTaskCallbackArguments ) { const restoredResources = [] const failedResources = [] @@ -283,7 +283,9 @@ export default { } return this.$loadingService.addTask( ({ setProgress }) => { - return this.$_restore_restoreResources(resolvedResources, missingFolderPaths, setProgress) + return this.$_restore_restoreResources(resolvedResources, missingFolderPaths, { + setProgress + }) }, { indeterminate: false } ) diff --git a/packages/web-app-files/src/mixins/deleteResources.ts b/packages/web-app-files/src/mixins/deleteResources.ts index 04eb65e518c..b5fc1053279 100644 --- a/packages/web-app-files/src/mixins/deleteResources.ts +++ b/packages/web-app-files/src/mixins/deleteResources.ts @@ -154,13 +154,13 @@ export default { $_deleteResources_filesList_delete() { return this.$loadingService.addTask( - ({ setProgress }) => { + (loadingCallbackArgs) => { return this.deleteFiles({ ...this.$language, space: this.space, files: this.$_deleteResources_resources, clientService: this.$clientService, - setProgress + loadingCallbackArgs }).then(async () => { this.hideModal() this.toggleModalConfirmButton() 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/mixins/actions/emptyTrashBin.spec.ts b/packages/web-app-files/tests/unit/mixins/actions/emptyTrashBin.spec.ts index 3019270cce8..6762352f990 100644 --- a/packages/web-app-files/tests/unit/mixins/actions/emptyTrashBin.spec.ts +++ b/packages/web-app-files/tests/unit/mixins/actions/emptyTrashBin.spec.ts @@ -3,7 +3,7 @@ import { createLocationTrash, createLocationSpaces } from '../../../../src/route import { mock, mockDeep } from 'jest-mock-extended' import { OwnCloudSdk } from 'web-client/src/types' import { createStore, defaultPlugins, mount, defaultStoreMockOptions } from 'web-test-helpers' -import { LoadingService } from 'web-pkg' +import { LoadingService, LoadingTaskCallbackArguments } from 'web-pkg' const Component = { template: '
', @@ -87,7 +87,7 @@ function getWrapper({ space: { driveType, isEditor: () => false, isManager: () => false }, $loadingService: mock({ addTask: (callback) => { - return callback({ setProgress: jest.fn() }) + return callback(mock()) } }), $client: { diff --git a/packages/web-app-files/tests/unit/mixins/actions/restore.spec.ts b/packages/web-app-files/tests/unit/mixins/actions/restore.spec.ts index 859642cd5b1..8041a844c50 100644 --- a/packages/web-app-files/tests/unit/mixins/actions/restore.spec.ts +++ b/packages/web-app-files/tests/unit/mixins/actions/restore.spec.ts @@ -1,8 +1,9 @@ import Restore from 'web-app-files/src/mixins/actions/restore' import { createLocationTrash, createLocationSpaces } from 'web-app-files/src/router' -import { mockDeep } from 'jest-mock-extended' +import { mock, mockDeep } from 'jest-mock-extended' import { OwnCloudSdk } from 'web-client/src/types' import { createStore, defaultPlugins, mount, defaultStoreMockOptions } from 'web-test-helpers' +import { LoadingTaskCallbackArguments } from 'web-pkg' const Component = { template: '
', @@ -46,7 +47,11 @@ describe('restore', () => { const { wrapper } = getWrapper() const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage') const removeFilesFromTrashbinStub = jest.spyOn(wrapper.vm, 'removeFilesFromTrashbin') - await wrapper.vm.$_restore_restoreResources([{ id: '1', path: '/1' }], [], jest.fn()) + await wrapper.vm.$_restore_restoreResources( + [{ id: '1', path: '/1' }], + [], + mock() + ) expect(showMessageStub).toHaveBeenCalledTimes(1) expect(removeFilesFromTrashbinStub).toHaveBeenCalledTimes(1) @@ -58,7 +63,11 @@ describe('restore', () => { const { wrapper } = getWrapper({ resolveClearTrashBin: false }) const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage') const removeFilesFromTrashbinStub = jest.spyOn(wrapper.vm, 'removeFilesFromTrashbin') - await wrapper.vm.$_restore_restoreResources([{ id: '1', path: '/1' }], [], jest.fn()) + await wrapper.vm.$_restore_restoreResources( + [{ id: '1', path: '/1' }], + [], + mock() + ) expect(showMessageStub).toHaveBeenCalledTimes(1) expect(removeFilesFromTrashbinStub).toHaveBeenCalledTimes(0) 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, + public addTask( + callback: ({ setProgress }: LoadingTaskCallbackArguments) => Promise, { debounceTime = DEFAULT_DEBOUNCE_TIME, indeterminate = true }: { debounceTime?: number; indeterminate?: boolean } = {} - ): Promise { + ): Promise { 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(), $loadingService: mock({ addTask: (callback) => { - return callback({ setProgress: jest.fn() }) + return callback(mock()) } }) }