Skip to content

Commit 2842c05

Browse files
Alexander LichterTheAlexLichter
Alexander Lichter
authored andcommitted
fix: allow custom attrs for external links
1 parent f5a7333 commit 2842c05

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ const vueLinkFactory = slashes => ({
4646

4747
return isLinkedToExternal
4848
? h('a', {
49-
...data,
5049
attrs: {
50+
...data.attrs,
5151
href: data.props.to || undefined,
5252
rel: data.props.rel || 'noopener',
5353
target: data.props.target || (data.props.newTab ? '_blank' : undefined)

test/VueLink.spec.js

+48
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@ describe('VueLink', () => {
3737

3838
expect(link.vm.$props.to).toBe('/test')
3939
})
40+
it('does apply custom props', () => {
41+
const wrapper = mount(VueLink, {
42+
localVue,
43+
attachToDocument: true,
44+
stubs: {
45+
RouterLink: RouterLinkStub
46+
},
47+
context: {
48+
props: {
49+
to: '/test',
50+
title: 'ABC'
51+
}
52+
},
53+
slots: {
54+
default: '<div>Hi</div>'
55+
}
56+
})
57+
58+
expect(wrapper.isVueInstance()).toBe(true)
59+
expect(wrapper.contains(RouterLinkStub)).toBe(true)
60+
61+
const link = wrapper.find(RouterLinkStub)
62+
63+
expect(link.vm.$props.to).toBe('/test')
64+
expect(wrapper.html()).toMatchSnapshot()
65+
})
4066
})
4167
describe('external', () => {
4268
it('does trigger external on http link', () => {
@@ -212,6 +238,28 @@ describe('VueLink', () => {
212238
expect(wrapper.isVueInstance()).toBe(false)
213239
expect(wrapper.contains(RouterLinkStub)).toBe(false)
214240

241+
expect(wrapper.html()).toMatchSnapshot()
242+
})
243+
it('does bind custom prop', () => {
244+
const wrapper = mount(VueLink, {
245+
localVue,
246+
attachToDocument: true,
247+
stubs: {
248+
RouterLink: RouterLinkStub
249+
},
250+
context: {
251+
attrs: {
252+
title: 'ABC'
253+
}
254+
},
255+
slots: {
256+
default: '<div>Hi</div>'
257+
}
258+
})
259+
260+
expect(wrapper.isVueInstance()).toBe(false)
261+
expect(wrapper.contains(RouterLinkStub)).toBe(false)
262+
215263
expect(wrapper.html()).toMatchSnapshot()
216264
})
217265
})

test/__snapshots__/VueLink.spec.js.snap

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

3+
exports[`VueLink external does bind custom prop 1`] = `
4+
<a title="ABC" rel="noopener">
5+
<div>Hi</div>
6+
</a>
7+
`;
8+
39
exports[`VueLink external does not bind href if unset 1`] = `
410
<a rel="noopener">
511
<div>Hi</div>
@@ -47,3 +53,9 @@ exports[`VueLink external does trigger external on https link 1`] = `
4753
<div>Hi</div>
4854
</a>
4955
`;
56+
57+
exports[`VueLink router-link does apply custom props 1`] = `
58+
<a>
59+
<div>Hi</div>
60+
</a>
61+
`;

0 commit comments

Comments
 (0)