Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Add a click handler to VLink if it uses NuxtLink (#1829)
Olga Bulat9db37ace1748
diff --git a/src/components/VLink.vue b/src/components/VLink.vueindex 0cc53c80a..fa8cb84a8 100644--- a/src/components/VLink.vue+++ b/src/components/VLink.vue@@ -1,9 +1,12 @@+<!-- eslint-disable vue/no-restricted-syntax --><template>- <Component- :is="linkComponent"- v-bind="linkProperties"+ <NuxtLink+ v-if="linkComponent === 'NuxtLink'"+ v-bind="$attrs":class="{ 'inline-flex flex-row items-center gap-2': showExternalIcon }"+ :to="linkTo"v-on="$listeners"+ @click.native="$emit('click', $event)"><slot /><VIconv-if="showExternalIcon && !isInternal"@@ -12,7 +15,37 @@:size="4"rtl-flip/>- </Component>+ </NuxtLink>+ <a+ v-else-if="linkComponent === 'a'"+ v-bind="$attrs"+ :href="href"+ target="_blank"+ rel="noopener noreferrer"+ :class="{ 'inline-flex flex-row items-center gap-2': showExternalIcon }"+ v-on="$listeners"+ >+ <slot /><VIcon+ v-if="showExternalIcon && !isInternal"+ :icon-path="externalLinkIcon"+ class="inline-block"+ :size="4"+ rtl-flip+ />+ </a>+ <span+ v-else+ v-bind="$attrs"+ :class="{ 'inline-flex flex-row items-center gap-2': showExternalIcon }"+ >+ <slot /><VIcon+ v-if="showExternalIcon && !isInternal"+ :icon-path="externalLinkIcon"+ class="inline-block"+ :size="4"+ rtl-flip+ />+ </span></template><script lang="ts">@@ -31,14 +64,10 @@ import VIcon from '~/components/VIcon/VIcon.vue'import externalLinkIcon from '~/assets/icons/external-link.svg'-const defaultProps = Object.freeze({- target: '_blank',- rel: 'noopener noreferrer',-})-export default defineComponent({name: 'VLink',components: { VIcon },+ inheritAttrs: false,props: {href: {type: String,@@ -71,15 +100,18 @@ export default defineComponent({hasHref.value ? (isInternal.value ? 'NuxtLink' : 'a') : 'span')- let linkProperties = computed(() =>- checkHref(props)- ? isInternal.value- ? { to: app?.localePath(props.href) ?? props.href }- : { ...defaultProps, href: props.href }+ let linkTo = computed(() =>+ checkHref(props) && isInternal.value+ ? app?.localePath(props.href) ?? props.href: null)- return { linkProperties, linkComponent, isInternal, externalLinkIcon }+ return {+ linkTo,+ linkComponent,+ isInternal,+ externalLinkIcon,+ }},})</script>diff --git a/test/unit/setup.js b/test/unit/setup.jsindex bf2f307f2..2eea4f547 100644--- a/test/unit/setup.js+++ b/test/unit/setup.js@@ -13,6 +13,14 @@ Vue.config.productionTip = falseVue.config.devtools = falseVue.use(VueI18n)+/**+ * Simplified mock of a NuxtLink component.+ */+config.stubs['nuxt-link'] = Vue.component('NuxtLink', {+ props: ['to'],+ template: '<a :href="to" v-on="$listeners"><slot /></a>',+})+config.mocks.$t = (key) => keyconfig.mocks.localePath = (i) => iglobal.matchMedia =diff --git a/test/unit/specs/components/AudioTrack/v-audio-track.spec.js b/test/unit/specs/components/AudioTrack/v-audio-track.spec.jsindex 67d2af256..04ba36f4a 100644--- a/test/unit/specs/components/AudioTrack/v-audio-track.spec.js+++ b/test/unit/specs/components/AudioTrack/v-audio-track.spec.js@@ -74,7 +74,6 @@ const configureVue = (vue) => {const stubs = {VPlayPause: true,- NuxtLink: true,VLicense: true,VWaveform: true,}diff --git a/test/unit/specs/components/image-grid.spec.js b/test/unit/specs/components/image-grid.spec.jsindex d539a0c50..8482e530d 100644--- a/test/unit/specs/components/image-grid.spec.js+++ b/test/unit/specs/components/image-grid.spec.js@@ -39,7 +39,7 @@ describe('VImageGrid', () => {localVue,pinia,props: propsData,- stubs: ['NuxtLink', 'VLicense'],+ stubs: ['VLicense'],mocks: { $nuxt: { context: { i18n } } },}})diff --git a/test/unit/specs/components/related-images.spec.js b/test/unit/specs/components/related-images.spec.jsindex edb72161a..5ae78d960 100644--- a/test/unit/specs/components/related-images.spec.js+++ b/test/unit/specs/components/related-images.spec.js@@ -31,7 +31,7 @@ describe('RelatedImage', () => {localVue,pinia,propsData: props,- stubs: ['VLicense', 'NuxtLink'],+ stubs: ['VLicense'],mocks: { $nuxt: { context: { i18n } } },}})diff --git a/test/unit/specs/components/v-content-link.spec.js b/test/unit/specs/components/v-content-link.spec.jsindex ff724a64e..dfff92ca4 100644--- a/test/unit/specs/components/v-content-link.spec.js+++ b/test/unit/specs/components/v-content-link.spec.js@@ -17,7 +17,6 @@ describe('VContentLink', () => {beforeEach(() => {options = {props: { mediaType: 'image', resultsCount: 123, to: '/images' },- stubs: ['NuxtLink'],mocks: {$nuxt: {context: {@@ -32,8 +31,7 @@ describe('VContentLink', () => {render(VContentLink, options)const btn = screen.getByRole('link')- /** @todo: Mock NuxtLink and check for href instead. **/- expect(btn).toHaveAttribute('to')+ expect(btn).toHaveAttribute('href')expect(btn).not.toHaveAttribute('aria-disabled')})@@ -42,8 +40,7 @@ describe('VContentLink', () => {render(VContentLink, options)const btn = screen.getByRole('link')- /** @todo: Mock NuxtLink and check for href instead. **/- expect(btn).not.toHaveAttribute('to')+ expect(btn).not.toHaveAttribute('href')expect(btn).toHaveAttribute('aria-disabled')expect(btn.getAttribute('aria-disabled')).toBeTruthy()})diff --git a/test/unit/specs/components/v-link.spec.js b/test/unit/specs/components/v-link.spec.jsindex b644aed96..67801fd06 100644--- a/test/unit/specs/components/v-link.spec.js+++ b/test/unit/specs/components/v-link.spec.js@@ -6,14 +6,6 @@ import VLink from '~/components/VLink.vue'const nuxtContextMock = {$nuxt: { context: { app: { localePath: jest.fn((v) => v) } } },}-/**- * Simplified mock of a NuxtLink component.- */-// eslint-disable-next-line vue/one-component-per-file-const NuxtLink = Vue.component('NuxtLink', {- props: ['to'],- template: '<a :href="to" v-on="$listeners"><slot /></a>',-})describe('VLink', () => {it.each`@@ -23,17 +15,11 @@ describe('VLink', () => {`('Creates a correct link component based on href',({ href, target, rel }) => {- render(- VLink,- {- props: { href },- slots: { default: 'Code is Poetry' },- mocks: nuxtContextMock,- },- (localVue) => {- localVue.component('NuxtLink', NuxtLink)- }- )+ render(VLink, {+ props: { href },+ slots: { default: 'Code is Poetry' },+ mocks: nuxtContextMock,+ })const link = screen.getByRole('link')const expectedHref = href.startsWith('/')? `http://localhost${href}`@@ -62,7 +48,6 @@ describe('VLink', () => {})const WrapperComponent = createVLinkWrapper(href)render(WrapperComponent, { mocks: nuxtContextMock }, (localVue) => {- localVue.component('NuxtLink', NuxtLink)localVue.component('VLink', VLink)})const linkBefore = await screen.getByRole('link')diff --git a/test/unit/specs/components/v-modal.spec.js b/test/unit/specs/components/v-modal.spec.jsindex 03f7dc4ca..7a5940c14 100644--- a/test/unit/specs/components/v-modal.spec.js+++ b/test/unit/specs/components/v-modal.spec.js@@ -64,7 +64,6 @@ describe('VModal', () => {beforeEach(() => {options = {props: { useCustomInitialFocus: false },- stubs: ['NuxtLink'],}jest.resetAllMocks()})