-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a80589
commit 59a22f4
Showing
5 changed files
with
159 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
packages/web-pkg/tests/unit/composables/actions/spaces/useSpaceActionsSetIcon.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
) | ||
} | ||
} |