Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions core/src/components/PublicPageMenu/PublicPageMenuEntry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,24 @@ defineEmits<{
}>()

onMounted(() => {
const anchor = document.getElementById(`${props.id}--link`) as HTMLAnchorElement
// Make the `<a>` a menuitem
anchor.role = 'menuitem'
// Prevent native click handling if required
if (props.clickOnly) {
anchor.onclick = (event) => event.preventDefault()
let attempts = 0
const trySetRole = () => {
const anchor = document.getElementById(`${props.id}--link`) as HTMLAnchorElement | null
if (anchor) {
// Make the `<a>` a menuitem
anchor.role = 'menuitem'
// Prevent native click handling if required
if (props.clickOnly) {
anchor.onclick = (event) => event.preventDefault()
}
return
}
if (attempts < 10) {
attempts++
requestAnimationFrame(trySetRole)
}
}
requestAnimationFrame(trySetRole)
})
</script>

Expand Down
21 changes: 21 additions & 0 deletions core/src/tests/components/PublicPageMenuExternalEntry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { enableAutoUnmount, mount } from '@vue/test-utils'
import { afterEach, describe, expect, it } from 'vitest'
import { defineComponent, h } from 'vue'
import PublicPageMenuEntryReal from '../../components/PublicPageMenu/PublicPageMenuEntry.vue'
import PublicPageMenuExternalEntry from '../../components/PublicPageMenu/PublicPageMenuExternalEntry.vue'

const PublicPageMenuEntry = defineComponent({
Expand Down Expand Up @@ -61,3 +62,23 @@ describe('PublicPageMenuExternalEntry', () => {
expect(wrapper.get('li').attributes('data-details')).toBe('Nextcloud')
})
})

describe('PublicPageMenuEntry', () => {
it('does not throw when the anchor is missing in DOM', () => {
const NcListItemStub = defineComponent({
name: 'NcListItem',
setup() {
return () => h('li')
},
})
let wrapper
expect(() => {
wrapper = mount(PublicPageMenuEntryReal, {
props: { id: 'missing', label: 'Missing', href: 'https://example.com' },
global: { stubs: { NcListItem: NcListItemStub } },
})
}).not.toThrow()
expect(wrapper!.find('li').exists()).toBe(true)
expect(document.getElementById('missing--link')).toBeNull()
})
})
Loading