Skip to content

Commit

Permalink
Add unit tests for the conflictDialog class
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Nov 18, 2022
1 parent 4a9c276 commit 938b7de
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,15 @@ export class ConflictDialog {
suggestMerge = false,
separateSkipHandling = false // separate skip-handling between files and folders
): Promise<ResolveConflict> {
const translatedSkipLabel = !separateSkipHandling
? this.$gettext('Apply to all %{count} conflicts')
: resource.isFolder
? this.$gettext('Apply to all %{count} folders')
: this.$gettext('Apply to all %{count} files')
let translatedSkipLabel

if (!separateSkipHandling) {
translatedSkipLabel = this.$gettext('Apply to all %{count} conflicts')
} else if (resource.isFolder) {
translatedSkipLabel = this.$gettext('Apply to all %{count} folders')
} else {
translatedSkipLabel = this.$gettext('Apply to all %{count} files')
}

return new Promise<ResolveConflict>((resolve) => {
let doForAllConflicts = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { mockDeep } from 'jest-mock-extended'
import { Resource } from 'web-client'
import { ConflictDialog, ResolveConflict } from 'web-app-files/src/helpers/resource'

const getConflictDialogInstance = ({ createModal = jest.fn() } = {}) => {
return new ConflictDialog(createModal, jest.fn(), jest.fn(), jest.fn(), jest.fn(), jest.fn())
}

describe('conflict dialog', () => {
describe('method "resolveAllConflicts"', () => {
it('should return the resolved conflicts including the resource(s) and the strategy', async () => {
const conflictDialog = getConflictDialogInstance()
const strategy = mockDeep<ResolveConflict>()
conflictDialog.resolveFileExists = jest.fn().mockImplementation(() => ({
strategy,
doForAllConflicts: false
}))
const resource = mockDeep<Resource>({ name: 'someFile.txt' })
const targetFolder = mockDeep<Resource>({ path: '/' })
const targetFolderResources = [mockDeep<Resource>({ path: '/someFile.txt' })]
const resolvedConflicts = await conflictDialog.resolveAllConflicts(
[resource],
targetFolder,
targetFolderResources
)

expect(resolvedConflicts.length).toBe(1)
expect(resolvedConflicts[0].resource).toEqual(resource)
expect(resolvedConflicts[0].strategy).toEqual(strategy)
})
})
describe('method "resolveFileExists"', () => {
it('should create the modal in the end', () => {
const createModal = jest.fn()
const conflictDialog = getConflictDialogInstance({ createModal })
conflictDialog.resolveFileExists(mockDeep<Resource>(), 2, true)
expect(createModal).toHaveBeenCalledTimes(1)
})
})
})

0 comments on commit 938b7de

Please sign in to comment.