Skip to content

Commit 40b19a7

Browse files
authored
fix(nav-item): move listeners to link element (bootstrap-vue#2755)
1 parent c462e0a commit 40b19a7

File tree

2 files changed

+127
-3
lines changed

2 files changed

+127
-3
lines changed

src/components/nav/nav-item.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,40 @@ export const props = linkPropsFactory()
77
export default {
88
name: 'BNavItem',
99
functional: true,
10-
props,
11-
render(h, { props, data, children }) {
10+
props: {
11+
...props,
12+
linkAttrs: {
13+
type: Object,
14+
default() {
15+
return {}
16+
}
17+
},
18+
linkClasses: {
19+
type: [String, Object, Array],
20+
default: null
21+
}
22+
},
23+
render(h, { props, data, listeners, children }) {
24+
// We transfer the listeners to the link
25+
delete data.on
1226
return h(
1327
'li',
1428
mergeData(data, {
1529
staticClass: 'nav-item'
1630
}),
17-
[h(BLink, { staticClass: 'nav-link', props }, children)]
31+
[
32+
h(
33+
BLink,
34+
{
35+
staticClass: 'nav-link',
36+
class: props.linkClasses,
37+
attrs: props.linkAttrs,
38+
props,
39+
on: listeners
40+
},
41+
children
42+
)
43+
]
1844
)
1945
}
2046
}

src/components/nav/nav-item.spec.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import NavItem from './nav-item'
2+
import Link from '../link/link'
3+
import { mount } from '@vue/test-utils'
4+
5+
describe('nav-item', async () => {
6+
it('has expected default structure', async () => {
7+
const wrapper = mount(NavItem)
8+
expect(wrapper.is('li')).toBe(true)
9+
expect(wrapper.classes()).toContain('nav-item')
10+
expect(wrapper.classes().length).toBe(1)
11+
12+
const link = wrapper.find('a')
13+
expect(link).toBeDefined()
14+
expect(link.is('a')).toBe(true)
15+
expect(link.is(Link)).toBe(true)
16+
expect(link.classes()).toContain('nav-link')
17+
expect(link.classes().length).toBe(1)
18+
expect(link.attributes('href')).toBeDefined()
19+
expect(link.attributes('href')).toBe('#')
20+
expect(link.attributes('role')).not.toBeDefined()
21+
})
22+
23+
it('has attrs on link when link-attrs set', async () => {
24+
const wrapper = mount(NavItem, {
25+
context: {
26+
props: {
27+
linkAttrs: { role: 'tab' }
28+
}
29+
}
30+
})
31+
expect(wrapper.attributes('role')).not.toBeDefined()
32+
const link = wrapper.find(Link)
33+
expect(link).toBeDefined()
34+
expect(link.attributes('role')).toBeDefined()
35+
expect(link.attributes('role')).toBe('tab')
36+
})
37+
38+
it('has custom classes on link when link-classes set', async () => {
39+
const wrapper = mount(NavItem, {
40+
context: {
41+
props: {
42+
linkClasses: ['foo', { bar: true }]
43+
}
44+
}
45+
})
46+
const link = wrapper.find(Link)
47+
expect(link).toBeDefined()
48+
expect(link.classes()).toContain('foo')
49+
expect(link.classes()).toContain('bar')
50+
expect(link.classes()).toContain('nav-link')
51+
})
52+
53+
it('has class "disabled" on link when disabled set', async () => {
54+
const wrapper = mount(NavItem, {
55+
context: {
56+
props: { disabled: true }
57+
}
58+
})
59+
const link = wrapper.find(Link)
60+
expect(link).toBeDefined()
61+
expect(link.classes()).toContain('disabled')
62+
})
63+
64+
it('emits click event when clicked', async () => {
65+
const spy = jest.fn()
66+
const wrapper = mount(NavItem, {
67+
context: {
68+
on: { click: spy }
69+
}
70+
})
71+
expect(spy).not.toHaveBeenCalled()
72+
wrapper.trigger('click')
73+
expect(spy).not.toHaveBeenCalled()
74+
75+
const link = wrapper.find(Link)
76+
expect(link).toBeDefined()
77+
link.trigger('click')
78+
expect(spy).toHaveBeenCalled()
79+
})
80+
81+
it('does not emit a click event when clicked and disabled', async () => {
82+
const spy = jest.fn()
83+
const wrapper = mount(NavItem, {
84+
context: {
85+
props: { disabled: true },
86+
on: { click: spy }
87+
}
88+
})
89+
expect(spy).not.toHaveBeenCalled()
90+
wrapper.trigger('click')
91+
expect(spy).not.toHaveBeenCalled()
92+
93+
const link = wrapper.find(Link)
94+
expect(link).toBeDefined()
95+
link.trigger('click')
96+
expect(spy).not.toHaveBeenCalled()
97+
})
98+
})

0 commit comments

Comments
 (0)