From dc9aaf87a8379e4ac722365271d59cfc1c7d72c2 Mon Sep 17 00:00:00 2001 From: Jannik Stehle Date: Fri, 18 Nov 2022 12:03:57 +0100 Subject: [PATCH] Add unit tests --- .../helpers/resource/actions/upload.spec.ts | 49 +++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/web-app-files/tests/unit/helpers/resource/actions/upload.spec.ts b/packages/web-app-files/tests/unit/helpers/resource/actions/upload.spec.ts index bc30274be71..a5ba1d79c7c 100644 --- a/packages/web-app-files/tests/unit/helpers/resource/actions/upload.spec.ts +++ b/packages/web-app-files/tests/unit/helpers/resource/actions/upload.spec.ts @@ -1,6 +1,6 @@ import { mockDeep } from 'jest-mock-extended' import { ResolveStrategy, ResourcesUpload } from 'web-app-files/src/helpers/resource' -import { SpaceResource } from 'web-client/src/helpers' +import { Resource, SpaceResource } from 'web-client/src/helpers' import { CreateDirectoryTreeResult, UppyResource } from 'web-runtime/src/composables/upload' import { UppyService } from 'web-runtime/src/services/uppyService' @@ -18,6 +18,7 @@ const spacesMock = [ const getResourcesUploadInstance = ({ space = mockDeep(), currentFolder = '/', + currentFiles = [], spaces = spacesMock, showMessage = jest.fn(), uppyService = mockDeep(), @@ -25,6 +26,7 @@ const getResourcesUploadInstance = ({ }: { space?: SpaceResource currentFolder?: string + currentFiles?: Resource[] spaces?: SpaceResource[] showMessage?: () => void uppyService?: UppyService @@ -37,7 +39,7 @@ const getResourcesUploadInstance = ({ } = {}) => { return new ResourcesUpload( [], - [], + currentFiles, jest.fn(() => []), uppyService, space, @@ -56,6 +58,47 @@ const getResourcesUploadInstance = ({ } describe('upload helper', () => { + describe('method "perform"', () => { + it('should handle the file upload when no conflicts found', () => { + const resourcesUpload = getResourcesUploadInstance() + const handleFileUppyloadStub = jest.fn() + resourcesUpload.handleUppyFileUpload = handleFileUppyloadStub + resourcesUpload.perform() + expect(handleFileUppyloadStub).toHaveBeenCalledTimes(1) + }) + it('should display the overwrite dialog when conflicts found', () => { + const resourcesUpload = getResourcesUploadInstance() + const displayOverwriteDialogStub = jest.fn() + const getConflictsStub = jest.fn(() => [{ name: 'name', type: 'file' }]) + resourcesUpload.displayOverwriteDialog = displayOverwriteDialogStub + resourcesUpload.getConflicts = getConflictsStub + resourcesUpload.perform() + expect(displayOverwriteDialogStub).toHaveBeenCalledTimes(1) + }) + }) + describe('method "getConflicts"', () => { + it('should return file and folder conflicts', () => { + const fileName = 'someFile.txt' + const folderName = 'someFolder' + const currentFiles = [ + mockDeep({ name: fileName }), + mockDeep({ name: folderName }) + ] + const filesToUpload = [ + mockDeep({ name: fileName, meta: { relativePath: '', relativeFolder: '' } }), + mockDeep({ + name: 'anotherFile', + meta: { relativePath: `/${folderName}/anotherFile` } + }) + ] + const resourcesUpload = getResourcesUploadInstance({ currentFiles }) + const conflicts = resourcesUpload.getConflicts(filesToUpload) + + expect(conflicts.length).toBe(2) + expect(conflicts).toContainEqual({ name: fileName, type: 'file' }) + expect(conflicts).toContainEqual({ name: folderName, type: 'folder' }) + }) + }) describe('method "checkQuotaExceeded"', () => { it('should be true if space quota exceeded', () => { const showMessageStub = jest.fn() @@ -142,7 +185,7 @@ describe('upload helper', () => { }) }) - describe('upload conflict dialog', () => { + describe('method "displayOverwriteDialog"', () => { it.each([ResolveStrategy.REPLACE, ResolveStrategy.KEEP_BOTH])( 'should upload file if user chooses replace or keep both', async (strategy) => {