Skip to content

fix(b-link): ensure href prop is not passed to router-links (fixes #3066) #3084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/components/dropdown/dropdown-item.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Vue from 'vue'
import BLink, { propsFactory as linkPropsFactory } from '../link/link'
import { requestAF } from '../../utils/dom'

export const props = linkPropsFactory()

Expand All @@ -14,9 +15,12 @@ export default Vue.extend({
props,
methods: {
closeDropdown() {
if (this.bvDropdown) {
this.bvDropdown.hide(true)
}
// Close on next animation frame to allow <b-link> time to process
requestAF(() => {
if (this.bvDropdown) {
this.bvDropdown.hide(true)
}
})
},
onClick(evt) {
this.$emit('click', evt)
Expand Down
64 changes: 61 additions & 3 deletions src/components/dropdown/dropdown-item.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import BDropdownItem from './dropdown-item'
import { mount } from '@vue/test-utils'
import VueRouter from 'vue-router'
import { mount, createLocalVue as CreateLocalVue } from '@vue/test-utils'

const waitNT = ctx => new Promise(resolve => ctx.$nextTick(resolve))
const waitRAF = () => new Promise(resolve => requestAnimationFrame(resolve))

describe('dropdown-item', () => {
it('renders with tag "a" and href="#" by default', async () => {
Expand Down Expand Up @@ -42,7 +46,8 @@ describe('dropdown-item', () => {
const item = wrapper.find('a')
expect(item).toBeDefined()
item.trigger('click')
await wrapper.vm.$nextTick()
await waitNT(wrapper.vm)
await waitRAF()
expect(called).toBe(true)
expect(refocus).toBe(true)

Expand All @@ -68,10 +73,63 @@ describe('dropdown-item', () => {
const item = wrapper.find('a')
expect(item).toBeDefined()
item.trigger('click')
await wrapper.vm.$nextTick()
await waitNT(wrapper.vm)
await waitRAF()
expect(called).toBe(false)
expect(refocus).toBe(null)

wrapper.destroy()
})

describe('router-link support', () => {
it('works', async () => {
const localVue = new CreateLocalVue()
localVue.use(VueRouter)

const router = new VueRouter({
mode: 'abstract',
routes: [
{ path: '/', component: { name: 'R', template: '<div class="r">ROOT</div>' } },
{ path: '/a', component: { name: 'A', template: '<div class="a">A</div>' } },
{ path: '/b', component: { name: 'B', template: '<div class="a">B</div>' } }
]
})

const App = localVue.extend({
router,
render(h) {
return h('ul', {}, [
h(BDropdownItem, { props: { to: '/a' } }, ['to-a']),
h(BDropdownItem, { props: { href: '/a' } }, ['href-a']),
h(BDropdownItem, { props: { to: { path: '/b' } } }, ['to-path-b']),
h(BDropdownItem, { props: { href: '/b' } }, ['href-a']),
h('router-view')
])
}
})

const wrapper = mount(App, {
localVue: localVue,
attachToDocument: true
})

expect(wrapper.isVueInstance()).toBe(true)
expect(wrapper.is('ul')).toBe(true)

expect(wrapper.findAll('li').length).toBe(4)
expect(wrapper.findAll('a').length).toBe(4)

const $links = wrapper.findAll('a')

expect($links.at(0).isVueInstance()).toBe(true)
expect($links.at(1).isVueInstance()).toBe(false)
expect($links.at(2).isVueInstance()).toBe(true)
expect($links.at(3).isVueInstance()).toBe(false)

expect($links.at(0).vm.$options.name).toBe('RouterLink')
expect($links.at(2).vm.$options.name).toBe('RouterLink')

wrapper.destroy()
})
})
})
Loading