Skip to content

Commit

Permalink
fix: selection after space duplication
Browse files Browse the repository at this point in the history
Spaces are listed as regular resources on the projects page, hence we need to make sure to insert duplicated spaces as such.
  • Loading branch information
JammingBen committed Dec 8, 2023
1 parent 0a46bd8 commit 13dd7ac
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelog/unreleased/enhancement-duplicate-space
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ This includes copying the contents, the space name, subtitle, description, and i
like tags or members.

https://github.com/owncloud/web/pull/10024
https://github.com/owncloud/web/pull/10132
https://github.com/owncloud/web/issues/10016
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ import { buildSpace, isProjectSpaceResource } from '@ownclouders/web-client/src/
import { Drive } from '@ownclouders/web-client/src/generated'
import { resolveFileNameDuplicate } from '../../../helpers'
import PQueue from 'p-queue'
import { useRouter } from '../../router'
import { isLocationSpacesActive } from '../../../router'

export const useSpaceActionsDuplicate = ({
store
}: {
store?: Store<any>
} = {}) => {
store = store || useStore()
const router = useRouter()
const { $gettext } = useGettext()
const ability = useAbility()
const clientService = useClientService()
const loadingService = useLoadingService()

const isProjectsLocation = isLocationSpacesActive(router, 'files-spaces-projects')

const duplicateSpace = async (existingSpace: SpaceResource) => {
const projectSpaces: SpaceResource[] = store.getters['runtime/spaces/spaces'].filter(
(space: SpaceResource) => isProjectSpaceResource(space)
Expand Down Expand Up @@ -96,6 +101,10 @@ export const useSpaceActionsDuplicate = ({
}

store.commit('runtime/spaces/UPSERT_SPACE', duplicatedSpace)
if (isProjectsLocation) {
store.commit('Files/UPSERT_RESOURCE', duplicatedSpace)
}

store.dispatch('showMessage', {
title: $gettext('Space "%{space}" was duplicated successfully', {
space: existingSpace.name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useSpaceActionsDuplicate } from '../../../../../src/composables/actions'
import { SpaceResource } from '@ownclouders/web-client/src/helpers'
import { AbilityRule, SpaceResource } from '@ownclouders/web-client/src/helpers'
import { mock } from 'jest-mock-extended'
import {
createStore,
Expand All @@ -24,14 +24,14 @@ const spaces = [
describe('restore', () => {
describe('isEnabled property', () => {
it('should be false when no resource given', () => {
const { wrapper } = getWrapper({
getWrapper({
setup: ({ actions }, { storeOptions }) => {
expect(unref(actions)[0].isEnabled({ resources: [] })).toBe(false)
}
})
})
it('should be false when the space is disabled', () => {
const { wrapper } = getWrapper({
getWrapper({
setup: ({ actions }, { storeOptions }) => {
expect(
unref(actions)[0].isEnabled({
Expand All @@ -47,7 +47,7 @@ describe('restore', () => {
})
})
it('should be false when the space is no project space', () => {
const { wrapper } = getWrapper({
getWrapper({
setup: ({ actions }, { storeOptions }) => {
expect(
unref(actions)[0].isEnabled({
Expand All @@ -63,7 +63,7 @@ describe('restore', () => {
})
})
it('should be false when the current user can not create spaces', () => {
const { wrapper } = getWrapper({
getWrapper({
abilities: [],
setup: ({ actions }, { storeOptions }) => {
expect(
Expand All @@ -75,7 +75,7 @@ describe('restore', () => {
})
})
it('should be true when the current user can create spaces', () => {
const { wrapper } = getWrapper({
getWrapper({
setup: ({ actions }, { storeOptions }) => {
expect(
unref(actions)[0].isEnabled({
Expand All @@ -90,19 +90,18 @@ describe('restore', () => {
})
})
describe('method "duplicateSpace"', () => {
it('should show error message on error', async () => {
it('should show error message on error', () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
const { wrapper } = getWrapper({
getWrapper({
setup: async ({ duplicateSpace }, { storeOptions, clientService }) => {
clientService.graphAuthenticated.drives.createDrive.mockRejectedValue(new Error())
await duplicateSpace(spaces[0])
expect(storeOptions.actions.showErrorMessage).toHaveBeenCalledTimes(1)
}
})
})
it('should show message on success', async () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
const { wrapper } = getWrapper({
it('should show message on success', () => {
getWrapper({
setup: async ({ duplicateSpace }, { storeOptions, clientService }) => {
clientService.graphAuthenticated.drives.createDrive.mockResolvedValue(
mockAxiosResolve({
Expand All @@ -127,12 +126,30 @@ describe('restore', () => {
}
})
})
it('should upsert a space as resource on the projects page', () => {
getWrapper({
currentRouteName: 'files-spaces-projects',
setup: async ({ duplicateSpace }, { storeOptions, clientService }) => {
clientService.graphAuthenticated.drives.createDrive.mockResolvedValue(
mockAxiosResolve({
id: '1',
name: 'Moon (1)',
special: []
})
)
clientService.webdav.listFiles.mockResolvedValue({ children: [] } as ListFilesResult)
await duplicateSpace(spaces[0])
expect(storeOptions.modules.Files.mutations.UPSERT_RESOURCE).toHaveBeenCalled()
}
})
})
})
})

function getWrapper({
setup,
abilities = [{ action: 'create-all', subject: 'Drive' }]
abilities = [{ action: 'create-all', subject: 'Drive' }],
currentRouteName = 'files-spaces-generic'
}: {
setup: (
instance: ReturnType<typeof useSpaceActionsDuplicate>,
Expand All @@ -144,15 +161,16 @@ function getWrapper({
clientService: ReturnType<typeof defaultComponentMocks>['$clientService']
}
) => void
abilities?
abilities?: AbilityRule[]
currentRouteName?: string
}) {
const storeOptions = {
...defaultStoreMockOptions
}
storeOptions.modules.runtime.modules.spaces.getters.spaces = jest.fn(() => spaces)
const store = createStore(storeOptions)
const mocks = defaultComponentMocks({
currentRoute: mock<RouteLocation>({ name: 'files-spaces-projects' })
currentRoute: mock<RouteLocation>({ name: currentRouteName })
})
return {
mocks,
Expand Down

0 comments on commit 13dd7ac

Please sign in to comment.