Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAndBear committed Mar 6, 2024
1 parent 6a80589 commit 59a22f4
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/web-client/src/webdav/client/dav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class DAV {

public put(
path: string,
content: string,
content: string | ArrayBuffer,
{
headers = {},
onUploadProgress,
Expand Down Expand Up @@ -168,7 +168,7 @@ export class DAV {
headers,
options
}: {
body?: string
body?: string | ArrayBuffer
headers?: Headers
options?: Partial<RequestOptionsCustom>
} = {}
Expand Down
2 changes: 1 addition & 1 deletion packages/web-client/src/webdav/putFileContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const PutFileContentsFactory = (
onUploadProgress = null
}: {
path?: string
content?: string
content?: string | ArrayBuffer
previousEntityTag?: string
headers?: Record<string, string>
overwrite?: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

<script lang="ts">
import { computed, defineComponent, onMounted, PropType, unref } from 'vue'
import { SpaceResource } from '@ownclouders/web-client/src'
import { Modal, useThemeStore } from '../../composables'
import { storeToRefs } from 'pinia'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ export const useSpaceActionsSetIcon = () => {
const spacesStore = useSpacesStore()
const { dispatchModal } = useModals()
const handler = ({ resources }: SpaceActionOptions) => {
if (resources.length !== 1) {
return
}

dispatchModal({
title: $gettext('Set icon for %{space}', { space: resources[0].name }),
hideConfirmButton: true,
customComponent: EmojiPickerModal,
onConfirm: (emoji: string) => uploadEmoji(resources[0], emoji)
onConfirm: (emoji: string) => setIconSpace(resources[0], emoji)
})
}

Expand All @@ -52,9 +56,9 @@ export const useSpaceActionsSetIcon = () => {
return blobToArrayBuffer(blob)
}

const uploadEmoji = async (space: SpaceResource, emoji: string) => {
const setIconSpace = async (space: SpaceResource, emoji: string) => {
const graphClient = clientService.graphAuthenticated
const content = (await generateEmojiImage(emoji)) as string
const content = await generateEmojiImage(emoji)

try {
await clientService.webdav.getFileInfo(space, { path: '.space' })
Expand Down Expand Up @@ -132,6 +136,7 @@ export const useSpaceActionsSetIcon = () => {
])

return {
actions
actions,
setIconSpace
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { useSpaceActionsSetIcon } from '../../../../../src/composables/actions/spaces/useSpaceActionsSetIcon'
import { useMessages, useModals } from '../../../../../src/composables/piniaStores'
import {
defaultComponentMocks,
mockAxiosResolve,
RouteLocation,
getComposableWrapper
} from 'web-test-helpers'
import { unref } from 'vue'
import { SpaceResource } from '@ownclouders/web-client/src'
import { mock } from 'vitest-mock-extended'

describe('setIcon', () => {
beforeEach(() => {
const createElementMock = vi.spyOn(document, 'createElement')
createElementMock.mockImplementation(() => {
return {
insertBefore: vi.fn(),
toBlob: () => new Blob(),
getContext: () => ({
fillText: vi.fn()
})
} as any
})
})
describe('isVisible property', () => {
it('should be false when no resource given', () => {
getWrapper({
setup: ({ actions }) => {
expect(unref(actions)[0].isVisible({ resources: [] })).toBe(false)
}
})
})
it('should be false when multiple resources are given', () => {
getWrapper({
setup: ({ actions }) => {
expect(
unref(actions)[0].isVisible({
resources: [mock<SpaceResource>(), mock<SpaceResource>()]
})
).toBe(false)
}
})
})
it('should be false when permission is not granted', () => {
getWrapper({
setup: ({ actions }) => {
expect(
unref(actions)[0].isVisible({
resources: [mock<SpaceResource>({ canEditImage: () => false })]
})
).toBe(false)
}
})
})
it('should be true when permission is granted', () => {
getWrapper({
setup: ({ actions }) => {
expect(
unref(actions)[0].isVisible({
resources: [mock<SpaceResource>({ canEditImage: () => true })]
})
).toBe(true)
}
})
})
})
describe('handler', () => {
it('should trigger the setIcon modal window with one resource', () => {
getWrapper({
setup: async ({ actions }) => {
const { dispatchModal } = useModals()
await unref(actions)[0].handler({ resources: [{ id: '1' } as SpaceResource] })

expect(dispatchModal).toHaveBeenCalledTimes(1)
}
})
})
it('should not trigger the setIcon modal window with no resource', () => {
getWrapper({
setup: async ({ actions }) => {
const { dispatchModal } = useModals()
await unref(actions)[0].handler({ resources: [] })

expect(dispatchModal).toHaveBeenCalledTimes(0)
}
})
})
})
describe('method "setIconSpace"', () => {
it('should show message on success', () => {
getWrapper({
setup: async ({ setIconSpace }, { clientService }) => {
clientService.graphAuthenticated.drives.updateDrive.mockResolvedValue(mockAxiosResolve())
await setIconSpace(mock<SpaceResource>(), '🐻')

const { showMessage } = useMessages()
expect(showMessage).toHaveBeenCalledTimes(1)
}
})
})

it('should show message on error', () => {
vi.spyOn(console, 'error').mockImplementation(() => undefined)
getWrapper({
setup: async ({ setIconSpace }, { clientService }) => {
clientService.graphAuthenticated.drives.updateDrive.mockRejectedValue(new Error())
await setIconSpace(mock<SpaceResource>(), '🐻')

const { showErrorMessage } = useMessages()
expect(showErrorMessage).toHaveBeenCalledTimes(1)
}
})
})
})
})

function getWrapper({
setup
}: {
setup: (
instance: ReturnType<typeof useSpaceActionsSetIcon>,
{
clientService
}: {
clientService: ReturnType<typeof defaultComponentMocks>['$clientService']
}
) => void
}) {
const mocks = defaultComponentMocks({
currentRoute: mock<RouteLocation>({ name: 'files-spaces-generic' })
})

return {
mocks,
wrapper: getComposableWrapper(
() => {
const instance = useSpaceActionsSetIcon()
setup(instance, { clientService: mocks.$clientService })
},
{
mocks,
provide: mocks
}
)
}
}

0 comments on commit 59a22f4

Please sign in to comment.