Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Disable content link when result count is zero (#1790)

Zack Kridaeca2f14e3d11
diff --git a/src/components/VContentLink/VContentLink.vue b/src/components/VContentLink/VContentLink.vue
index 183be9b57..f3b782e9b 100644
--- a/src/components/VContentLink/VContentLink.vue
+++ b/src/components/VContentLink/VContentLink.vue
@@ -1,7 +1,15 @@
<template>
+ <!-- We 'disable' the link when there are 0 results by removing the href and setting aria-disabled. -->
<VLink
- :href="to"
- class="flex w-full flex-col items-start overflow-hidden rounded-sm border border-dark-charcoal/20 bg-white py-4 text-dark-charcoal ps-4 pe-12 hover:bg-dark-charcoal hover:text-white hover:no-underline focus:border-tx focus:outline-none focus-visible:ring focus-visible:ring-pink md:flex-row md:items-center md:justify-between md:p-6"
+ :href="hasResults ? to : undefined"
+ role="link"
+ :aria-disabled="!hasResults"
+ class="flex w-full flex-col items-start overflow-hidden rounded-sm border border-dark-charcoal/20 bg-white py-4 ps-4 pe-12 md:flex-row md:items-center md:justify-between md:p-6"
+ :class="
+ hasResults
+ ? ' text-dark-charcoal hover:bg-dark-charcoal hover:text-white hover:no-underline focus:border-tx focus:outline-none focus-visible:ring focus-visible:ring-pink'
+ : 'cursor-not-allowed text-dark-charcoal/40'
+ "
@keydown.native.shift.tab.exact="$emit('shift-tab', $event)"
>
<div class="flex flex-col items-start md:flex-row md:items-center">
@@ -48,7 +56,8 @@ export default defineComponent({
required: true,
},
/**
- * The number of results that the search returned.
+ * The number of results that the search returned. The link
+ * will be disabled if this value is zero.
*/
resultsCount: {
type: Number,
@@ -67,9 +76,15 @@ export default defineComponent({
setup(props) {
const iconPath = computed(() => iconMapping[props.mediaType])
const { getI18nCount } = useI18nResultsCount()
+ const hasResults = computed(() => props.resultsCount > 0)
const resultsCountLabel = computed(() => getI18nCount(props.resultsCount))
- return { iconPath, imageIcon, resultsCountLabel }
+ return {
+ iconPath,
+ imageIcon,
+ resultsCountLabel,
+ hasResults,
+ }
},
})
</script>
diff --git a/test/unit/specs/components/v-content-link.spec.js b/test/unit/specs/components/v-content-link.spec.js
index 53df1c114..ff724a64e 100644
--- a/test/unit/specs/components/v-content-link.spec.js
+++ b/test/unit/specs/components/v-content-link.spec.js
@@ -16,7 +16,8 @@ describe('VContentLink', () => {
beforeEach(() => {
options = {
- props: { mediaType: 'image', resultsCount: 123 },
+ props: { mediaType: 'image', resultsCount: 123, to: '/images' },
+ stubs: ['NuxtLink'],
mocks: {
$nuxt: {
context: {
@@ -27,17 +28,23 @@ describe('VContentLink', () => {
}
})
- xit('is not selected by default', () => {
+ it('is enabled when there are results', () => {
render(VContentLink, options)
- const btn = screen.getByRole('radio')
- expect(btn).not.toHaveAttribute('aria-checked')
+ const btn = screen.getByRole('link')
+
+ /** @todo: Mock NuxtLink and check for href instead. **/
+ expect(btn).toHaveAttribute('to')
+ expect(btn).not.toHaveAttribute('aria-disabled')
})
- xit('is marked as selected when indicated with the isSelected prop', () => {
- options.props.isSelected = true
+ it('is disabled when there are no results', () => {
+ options.props.resultsCount = 0
render(VContentLink, options)
- const btn = screen.getByRole('radio')
- expect(btn).toHaveAttribute('aria-checked')
- expect(btn.getAttribute('aria-checked')).toBeTruthy()
+ const btn = screen.getByRole('link')
+
+ /** @todo: Mock NuxtLink and check for href instead. **/
+ expect(btn).not.toHaveAttribute('to')
+ expect(btn).toHaveAttribute('aria-disabled')
+ expect(btn.getAttribute('aria-disabled')).toBeTruthy()
})
})