Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Nov 18, 2022
1 parent cdd1313 commit dc9aaf8
Showing 1 changed file with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -18,13 +18,15 @@ const spacesMock = [
const getResourcesUploadInstance = ({
space = mockDeep<SpaceResource>(),
currentFolder = '/',
currentFiles = [],
spaces = spacesMock,
showMessage = jest.fn(),
uppyService = mockDeep<UppyService>(),
createDirectoryTree = jest.fn().mockImplementation(() => ({ failed: [], successful: [] }))
}: {
space?: SpaceResource
currentFolder?: string
currentFiles?: Resource[]
spaces?: SpaceResource[]
showMessage?: () => void
uppyService?: UppyService
Expand All @@ -37,7 +39,7 @@ const getResourcesUploadInstance = ({
} = {}) => {
return new ResourcesUpload(
[],
[],
currentFiles,
jest.fn(() => []),
uppyService,
space,
Expand All @@ -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<Resource>({ name: fileName }),
mockDeep<Resource>({ name: folderName })
]
const filesToUpload = [
mockDeep<UppyResource>({ name: fileName, meta: { relativePath: '', relativeFolder: '' } }),
mockDeep<UppyResource>({
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()
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit dc9aaf8

Please sign in to comment.