Skip to content

Commit

Permalink
test: unit tests for useSideBar
Browse files Browse the repository at this point in the history
  • Loading branch information
kulmann committed Aug 31, 2022
1 parent 8a3d049 commit 9c106b3
Show file tree
Hide file tree
Showing 21 changed files with 181 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<script>
import { bus } from 'web-pkg/src/instance'
import { SideBarEventTopics } from '../../composables/sidebar'
export default {
props: {
Expand All @@ -33,7 +34,7 @@ export default {
},
methods: {
toggleSideBar() {
bus.publish('app.files.sidebar.toggle')
bus.publish(SideBarEventTopics.toggle)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ import { ClipboardActions } from '../../helpers/clipboardActions'
import { ShareTypes } from 'web-client/src/helpers/share'
import { createLocationSpaces } from '../../router'
import { formatDateFromJSDate, formatRelativeDateFromJSDate } from 'web-pkg/src/helpers'
import { SideBarEventTopics } from '../../composables/sidebar'
const mapResourceFields = (resource: Resource, mapping = {}) => {
return Object.keys(mapping).reduce((result, resourceKey) => {
Expand Down Expand Up @@ -583,10 +584,10 @@ export default defineComponent({
},
openSharingSidebar(file) {
if (file.share?.shareType === ShareTypes.link.value) {
bus.publish('app.files.sidebar.openWithPanel', 'sharing-item#linkShares')
bus.publish(SideBarEventTopics.openWithPanel, 'sharing-item#linkShares')
return
}
bus.publish('app.files.sidebar.openWithPanel', 'sharing-item#peopleShares')
bus.publish(SideBarEventTopics.openWithPanel, 'sharing-item#peopleShares')
},
folderLink(file) {
return this.createFolderLink(file.path, file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ import copyToClipboard from 'copy-to-clipboard'
import { encodePath } from 'web-pkg/src/utils'
import { formatDateFromHTTP, formatFileSize } from 'web-pkg/src/helpers'
import { bus } from 'web-pkg/src/instance'
import { SideBarEventTopics } from '../../../composables/sidebar'
export default defineComponent({
name: 'FileDetails',
Expand Down Expand Up @@ -429,7 +430,7 @@ export default defineComponent({
return null
},
expandVersionsPanel() {
bus.publish('app.files.sidebar.setActivePanel', 'versions-item')
bus.publish(SideBarEventTopics.setActivePanel, 'versions-item')
},
async loadData() {
const calls = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import SpaceQuota from '../../SpaceQuota.vue'
import { formatDateFromISO } from 'web-pkg/src/helpers'
import { configurationManager } from 'web-pkg/src/configuration'
import { bus } from 'web-pkg/src/instance'
import { SideBarEventTopics } from '../../../composables/sidebar'
export default defineComponent({
name: 'SpaceDetails',
Expand Down Expand Up @@ -249,7 +250,7 @@ export default defineComponent({
},
methods: {
expandSharesPanel() {
bus.publish('app.files.sidebar.setActivePanel', 'space-share-item')
bus.publish(SideBarEventTopics.setActivePanel, 'space-share-item')
}
}
})
Expand Down
7 changes: 4 additions & 3 deletions packages/web-app-files/src/components/SideBar/SideBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
useStore
} from 'web-pkg/src/composables'
import { bus } from 'web-pkg/src/instance'
import { SideBarEventTopics } from '../../composables/sidebar'
export default defineComponent({
components: { FileInfo, SpaceInfo, SideBar },
Expand All @@ -78,10 +79,10 @@ export default defineComponent({
const store = useStore()
const closeSideBar = () => {
bus.publish('app.files.sidebar.close')
bus.publish(SideBarEventTopics.close)
}
const setActiveSideBarPanel = (panelName) => {
bus.publish('app.files.sidebar.setActivePanel', panelName)
bus.publish(SideBarEventTopics.setActivePanel, panelName)
}
const focusSideBar = (component, event) => {
Expand All @@ -94,7 +95,7 @@ export default defineComponent({
const destroySideBar = (component, event) => {
focusSideBar(component, event)
bus.publish('app.files.sidebar.close')
bus.publish(SideBarEventTopics.close)
}
return {
Expand Down
7 changes: 7 additions & 0 deletions packages/web-app-files/src/composables/sidebar/eventTopics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export abstract class SideBarEventTopics {
public static readonly open = 'app.files.sidebar.open'
public static readonly close = 'app.files.sidebar.close'
public static readonly toggle = 'app.files.sidebar.toggle'
public static readonly openWithPanel = 'app.files.sidebar.openWithPanel'
public static readonly setActivePanel = 'app.files.sidebar.setActivePanel'
}
1 change: 1 addition & 0 deletions packages/web-app-files/src/composables/sidebar/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './eventTopics'
export * from './useSideBar'
33 changes: 20 additions & 13 deletions packages/web-app-files/src/composables/sidebar/useSideBar.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
import { onBeforeUnmount, ref, Ref, unref } from '@vue/composition-api'
import { bus } from 'web-pkg/src/instance'
import { EventBus } from 'web-pkg/src/event'
import { SideBarEventTopics } from './eventTopics'

interface SideBarResult {
sideBarOpen: Ref<boolean>
sideBarActivePanel: Ref<string>
}

export const useSideBar = (): SideBarResult => {
interface SideBarOptions {
bus?: EventBus
}

export const useSideBar = (options?: SideBarOptions): SideBarResult => {
const eventBus = options?.bus || bus
const sideBarOpen = ref(false)
const sideBarActivePanel = ref(null)
const toggleSideBarToken = bus.subscribe('app.files.sidebar.toggle', () => {
const toggleSideBarToken = eventBus.subscribe(SideBarEventTopics.toggle, () => {
sideBarOpen.value = !unref(sideBarOpen)
})
const closeSideBarToken = bus.subscribe('app.files.sidebar.close', () => {
const closeSideBarToken = eventBus.subscribe(SideBarEventTopics.close, () => {
sideBarOpen.value = false
sideBarActivePanel.value = null
})
const openSideBarToken = bus.subscribe('app.files.sidebar.open', () => {
const openSideBarToken = eventBus.subscribe(SideBarEventTopics.open, () => {
sideBarOpen.value = true
sideBarActivePanel.value = null
})
const openSideBarWithPanelToken = bus.subscribe(
'app.files.sidebar.openWithPanel',
const openSideBarWithPanelToken = eventBus.subscribe(
SideBarEventTopics.openWithPanel,
(panelName) => {
sideBarOpen.value = true
sideBarActivePanel.value = panelName
}
)
const setActiveSideBarPanelToken = bus.subscribe(
'app.files.sidebar.setActivePanel',
const setActiveSideBarPanelToken = eventBus.subscribe(
SideBarEventTopics.setActivePanel,
(panelName) => {
sideBarActivePanel.value = panelName
}
)
onBeforeUnmount(() => {
bus.unsubscribe('app.files.sidebar.toggle', toggleSideBarToken)
bus.unsubscribe('app.files.sidebar.close', closeSideBarToken)
bus.unsubscribe('app.files.sidebar.open', openSideBarToken)
bus.unsubscribe('app.files.sidebar.openWithPanel', openSideBarWithPanelToken)
bus.unsubscribe('app.files.sidebar.setActivePanel', setActiveSideBarPanelToken)
eventBus.unsubscribe(SideBarEventTopics.toggle, toggleSideBarToken)
eventBus.unsubscribe(SideBarEventTopics.close, closeSideBarToken)
eventBus.unsubscribe(SideBarEventTopics.open, openSideBarToken)
eventBus.unsubscribe(SideBarEventTopics.openWithPanel, openSideBarWithPanelToken)
eventBus.unsubscribe(SideBarEventTopics.setActivePanel, setActiveSideBarPanelToken)
})

return {
Expand Down
5 changes: 3 additions & 2 deletions packages/web-app-files/src/helpers/statusIndicators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getParentPaths } from './path'
import { $gettext } from '../gettext'
import { ShareTypes } from 'web-client/src/helpers/share'
import { bus } from 'web-pkg/src/instance'
import { SideBarEventTopics } from '../composables/sidebar'

const $shareTypes = (resource) => {
if (typeof resource.shareTypes !== 'undefined') {
Expand Down Expand Up @@ -100,7 +101,7 @@ export const getIndicators = (resource, sharesTree, hasShareJail = false) => {
target: 'sharing-item',
type: isDirectUserShare(resource) ? 'user-direct' : 'user-indirect',
handler: (resource, panel) => {
bus.publish('app.files.sidebar.openWithPanel', `${panel}#peopleShares`)
bus.publish(SideBarEventTopics.openWithPanel, `${panel}#peopleShares`)
}
},
{
Expand All @@ -112,7 +113,7 @@ export const getIndicators = (resource, sharesTree, hasShareJail = false) => {
target: 'sharing-item',
type: isDirectLinkShare(resource) ? 'link-direct' : 'link-indirect',
handler: (resource, panel) => {
bus.publish('app.files.sidebar.openWithPanel', `${panel}#linkShares`)
bus.publish(SideBarEventTopics.openWithPanel, `${panel}#linkShares`)
}
}
]
Expand Down
3 changes: 2 additions & 1 deletion packages/web-app-files/src/mixins/actions/createQuicklink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ShareStatus } from 'web-client/src/helpers/share'

import { isLocationSharesActive } from '../../router'
import { bus } from 'web-pkg/src/instance'
import { SideBarEventTopics } from '../../composables/sidebar'

export default {
computed: {
Expand Down Expand Up @@ -42,7 +43,7 @@ export default {
store
})

bus.publish('app.files.sidebar.openWithPanel', 'sharing-item#linkShares')
bus.publish(SideBarEventTopics.openWithPanel, 'sharing-item#linkShares')
}
}
}
3 changes: 2 additions & 1 deletion packages/web-app-files/src/mixins/actions/showActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isLocationTrashActive } from '../../router'
import isFilesAppActive from './helpers/isFilesAppActive'
import { bus } from 'web-pkg/src/instance'
import { SideBarEventTopics } from '../../composables/sidebar'

export default {
mixins: [isFilesAppActive],
Expand Down Expand Up @@ -39,7 +40,7 @@ export default {
isLocationTrashActive(this.$router, 'files-trash-spaces-project')
? null
: 'actions-item'
bus.publish('app.files.sidebar.openWithPanel', panelName)
bus.publish(SideBarEventTopics.openWithPanel, panelName)
}
}
}
3 changes: 2 additions & 1 deletion packages/web-app-files/src/mixins/actions/showDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { mapMutations } from 'vuex'
import { isLocationTrashActive } from '../../router'
import isFilesAppActive from './helpers/isFilesAppActive'
import { bus } from 'web-pkg/src/instance'
import { SideBarEventTopics } from '../../composables/sidebar'

export default {
mixins: [isFilesAppActive],
Expand Down Expand Up @@ -40,7 +41,7 @@ export default {

$_showDetails_trigger({ resources }) {
this.SET_FILE_SELECTION(resources)
bus.publish('app.files.sidebar.open')
bus.publish(SideBarEventTopics.open)
}
}
}
3 changes: 2 additions & 1 deletion packages/web-app-files/src/mixins/actions/showShares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ShareStatus } from 'web-client/src/helpers/share'
import isFilesAppActive from './helpers/isFilesAppActive'
import { mapMutations } from 'vuex'
import { bus } from 'web-pkg/src/instance'
import { SideBarEventTopics } from '../../composables/sidebar'

export default {
mixins: [isFilesAppActive],
Expand Down Expand Up @@ -49,7 +50,7 @@ export default {

$_showShares_trigger({ resources }) {
this.SET_FILE_SELECTION(resources)
bus.publish('app.files.sidebar.openWithPanel', 'sharing-item#peopleShares')
bus.publish(SideBarEventTopics.openWithPanel, 'sharing-item#peopleShares')
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { mapMutations } from 'vuex'
import { bus } from 'web-pkg/src/instance'
import { SideBarEventTopics } from '../../../composables/sidebar'

export default {
computed: {
Expand Down Expand Up @@ -30,7 +31,7 @@ export default {
},

$_showDetails_openSideBar() {
bus.publish('app.files.sidebar.open')
bus.publish(SideBarEventTopics.open)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { mapMutations } from 'vuex'
import { bus } from 'web-pkg/src/instance'
import { SideBarEventTopics } from '../../../composables/sidebar'

export default {
computed: {
Expand All @@ -22,7 +23,7 @@ export default {

$_showMembers_trigger({ resources }) {
this.SET_FILE_SELECTION(resources)
bus.publish('app.files.sidebar.openWithPanel', 'space-share-item')
bus.publish(SideBarEventTopics.openWithPanel, 'space-share-item')
}
}
}
7 changes: 4 additions & 3 deletions packages/web-app-files/src/quickActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { $gettext } from './gettext'
import { createQuicklink } from './helpers/share'
import { bus } from 'web-pkg/src/instance'
import { SideBarEventTopics } from './composables/sidebar'

export function canShare(item, store) {
const { capabilities } = store.state.user
Expand Down Expand Up @@ -51,7 +52,7 @@ export default {
id: 'collaborators',
label: ($gettext) => $gettext('Add people'),
icon: 'user-add',
handler: () => bus.publish('app.files.sidebar.openWithPanel', 'sharing-item#peopleShares'),
handler: () => bus.publish(SideBarEventTopics.openWithPanel, 'sharing-item#peopleShares'),
displayed: canShare
},
quicklink: {
Expand All @@ -66,12 +67,12 @@ export default {
if (passwordEnforced) {
return showQuickLinkPasswordModal(ctx, async (password) => {
await createQuicklink({ ...ctx, resource: ctx.item, password })
bus.publish('app.files.sidebar.openWithPanel', 'sharing-item#linkShares')
bus.publish(SideBarEventTopics.openWithPanel, 'sharing-item#linkShares')
})
}

await createQuicklink({ ...ctx, resource: ctx.item })
bus.publish('app.files.sidebar.openWithPanel', 'sharing-item#linkShares')
bus.publish(SideBarEventTopics.openWithPanel, 'sharing-item#linkShares')
},
displayed: canShare
}
Expand Down
3 changes: 2 additions & 1 deletion packages/web-app-files/src/views/spaces/Project.vue
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ import { configurationManager } from 'web-pkg/src/configuration'
import { buildWebDavSpacesPath } from 'web-client/src/helpers'
import SideBar from '../../components/SideBar/SideBar.vue'
import FilesViewWrapper from '../../components/FilesViewWrapper.vue'
import { SideBarEventTopics } from '../../composables/sidebar'
const visibilityObserver = new VisibilityObserver()
Expand Down Expand Up @@ -453,7 +454,7 @@ export default defineComponent({
},
openSidebarSharePanel() {
this.selectedResources = [this.space]
bus.publish('app.files.sidebar.openWithPanel', 'space-share-item')
bus.publish(SideBarEventTopics.openWithPanel, 'space-share-item')
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions packages/web-app-files/src/views/spaces/Projects.vue
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ import { buildSpace, buildWebDavSpacesPath } from 'web-client/src/helpers'
import SideBar from '../../components/SideBar/SideBar.vue'
import FilesViewWrapper from '../../components/FilesViewWrapper.vue'
import { bus } from 'web-pkg/src/instance'
import { useSideBar } from '../../composables/sidebar'
import { SideBarEventTopics, useSideBar } from '../../composables/sidebar'
export default defineComponent({
components: {
Expand Down Expand Up @@ -296,7 +296,7 @@ export default defineComponent({
openSidebarSharePanel(space) {
this.SET_FILE_SELECTION([space])
bus.publish('app.files.sidebar.openWithPanel', 'space-share-item')
bus.publish(SideBarEventTopics.openWithPanel, 'space-share-item')
},
getSpaceLinkProps(space) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const localVue = createLocalVue()
localVue.use(VueCompositionAPI)
localVue.use(Vuex)

export const createWrapper = (setup: SetupFunction<Data, Data>): Wrapper<Vue> =>
export const createComposableWrapper = (setup: SetupFunction<Data, Data>): Wrapper<Vue> =>
mount(
defineComponent({
template: `<div></div>`,
Expand Down
Loading

0 comments on commit 9c106b3

Please sign in to comment.