Skip to content

Commit b183cca

Browse files
feat: dedicated nuxt export (#20)
1 parent df2d944 commit b183cca

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

lib/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const isExternal = props => !props.to ||
3232
/^(http|\/\/)/.test(props.to) ||
3333
props.external
3434

35-
const vueLinkFactory = slashes => ({
35+
const vueLinkFactory = (slashes, isNuxt) => ({
3636
functional: true,
3737
render: (h, { data, children, props }) => {
3838
data.props = props
@@ -44,6 +44,8 @@ const vueLinkFactory = slashes => ({
4444
data.props.to = modifySlashes(data.props.slashes, data.props.to)
4545
}
4646

47+
const linkComponent = isNuxt ? 'NuxtLink' : 'RouterLink'
48+
4749
return isLinkedToExternal
4850
? h('a', {
4951
...data,
@@ -54,11 +56,12 @@ const vueLinkFactory = slashes => ({
5456
target: data.props.target || (data.props.newTab ? '_blank' : undefined)
5557
}
5658
}, children)
57-
: h('router-link', data, children)
59+
: h(linkComponent, data, children)
5860
}
5961
})
6062

6163
export const VueLink = vueLinkFactory()
64+
export const ForNuxt = vueLinkFactory(undefined, true)
6265
export const VueLinkAddSlash = vueLinkFactory(slashOptions.ADD)
6366
export const VueLinkStripSlash = vueLinkFactory(slashOptions.STRIP)
6467

test/VueLink.spec.js

+56-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-console */
22
import { createLocalVue, mount, RouterLinkStub } from '@vue/test-utils'
33
import VueRouter from 'vue-router'
4-
import { VueLink, VueLinkAddSlash, VueLinkStripSlash } from '../lib'
4+
import { VueLink, VueLinkAddSlash, VueLinkStripSlash, ForNuxt } from '../lib'
55

66
const localVue = createLocalVue()
77
localVue.use(VueRouter)
@@ -285,6 +285,61 @@ describe('VueLink', () => {
285285
})
286286
})
287287

288+
describe('ForNuxt', () => {
289+
describe('nuxt-link', () => {
290+
it('does link to the correct internal page', () => {
291+
const wrapper = mount(ForNuxt, {
292+
localVue,
293+
attachToDocument: true,
294+
stubs: {
295+
NuxtLink: RouterLinkStub
296+
},
297+
context: {
298+
props: {
299+
to: '/test'
300+
}
301+
},
302+
slots: {
303+
default: hiComponent
304+
}
305+
})
306+
307+
expect(wrapper.isVueInstance()).toBe(true)
308+
expect(wrapper.contains(RouterLinkStub)).toBe(true)
309+
310+
const link = wrapper.find(RouterLinkStub)
311+
312+
expect(link.vm.$props.to).toBe('/test')
313+
})
314+
it('does apply custom props', () => {
315+
const wrapper = mount(ForNuxt, {
316+
localVue,
317+
attachToDocument: true,
318+
stubs: {
319+
NuxtLink: RouterLinkStub
320+
},
321+
context: {
322+
props: {
323+
to: '/test',
324+
title: 'ABC'
325+
}
326+
},
327+
slots: {
328+
default: '<div>Hi</div>'
329+
}
330+
})
331+
332+
expect(wrapper.isVueInstance()).toBe(true)
333+
expect(wrapper.contains(RouterLinkStub)).toBe(true)
334+
335+
const link = wrapper.find(RouterLinkStub)
336+
337+
expect(link.vm.$props.to).toBe('/test')
338+
expect(wrapper.html()).toMatchSnapshot()
339+
})
340+
})
341+
})
342+
288343
describe('VueLinkStripSlash', () => {
289344
it('strips slashes', () => {
290345
const wrapper = mount(VueLinkStripSlash, {

test/__snapshots__/VueLink.spec.js.snap

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`ForNuxt nuxt-link does apply custom props 1`] = `
4+
<a>
5+
<div>Hi</div>
6+
</a>
7+
`;
8+
39
exports[`VueLink external does bind classp 1`] = `
410
<a rel="noopener" class="static">
511
<div>Hi</div>

0 commit comments

Comments
 (0)