Skip to content
Permalink
Browse files
fix(b-img): Allow empty alt prop (fixes #5524) (#5545)
* Allow empty `alt`

* default to null to avoid check

* remove unused import

* add avatar support

* add test cases

* spelling

Co-authored-by: Jacob Müller <jacob.mueller.elz@gmail.com>
  • Loading branch information
Hiws and jacobmllr95 committed Jul 6, 2020
1 parent 190325e commit b22829d064b6e3820ef66168ec766a57520f31eb
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 7 deletions.
@@ -202,7 +202,7 @@ export const BAvatar = /*#__PURE__*/ Vue.extend({
} = this
const link = !button && isLink(this)
const tag = button ? BButton : link ? BLink : 'span'
const alt = this.alt || null
const alt = this.alt
const ariaLabel = this.ariaLabel || null

let $content = null
@@ -317,4 +317,34 @@ describe('avatar', () => {

wrapper2.destroy()
})

it('should render `alt` attribute if `alt` prop is empty string', async () => {
const wrapper = mount(BAvatar, {
propsData: {
src: '/foo/bar',
alt: ''
}
})
expect(wrapper.vm).toBeDefined()
expect(wrapper.find('img').exists()).toBe(true)
expect(wrapper.find('img').attributes('src')).toEqual('/foo/bar')
expect(wrapper.find('img').attributes('alt')).toEqual('')

wrapper.destroy()
})

it('should not render `alt` attribute if `alt` prop is null', async () => {
const wrapper = mount(BAvatar, {
propsData: {
src: '/foo/bar',
alt: null
}
})
expect(wrapper.vm).toBeDefined()
expect(wrapper.find('img').exists()).toBe(true)
expect(wrapper.find('img').attributes('src')).toEqual('/foo/bar')
expect(wrapper.find('img').attributes('alt')).not.toBeDefined()

wrapper.destroy()
})
})
@@ -154,6 +154,22 @@ describe('card-image', () => {
wrapper.destroy()
})

it('has attribute alt when prop `alt` is empty', async () => {
const wrapper = mount(BCardImgLazy, {
context: {
props: {
src: 'https://picsum.photos/600/300/?image=25',
alt: ''
}
}
})

expect(wrapper.attributes('alt')).toBeDefined()
expect(wrapper.attributes('alt')).toBe('')

wrapper.destroy()
})

it('has attribute width when prop width set', async () => {
const wrapper = mount(BCardImgLazy, {
context: {
@@ -7,8 +7,8 @@ export const props = {
required: true
},
alt: {
type: String
// default: null
type: String,
default: null
},
top: {
type: Boolean,
@@ -69,7 +69,7 @@ export const BCardImg = /*#__PURE__*/ Vue.extend({
class: [baseClass],
attrs: {
src: props.src || null,
alt: props.alt || null,
alt: props.alt,
height: props.height || null,
width: props.width || null
}
@@ -158,6 +158,22 @@ describe('card-image', () => {
wrapper.destroy()
})

it('has attribute alt when prop `alt` is empty', async () => {
const wrapper = mount(BCardImg, {
context: {
props: {
src: 'https://picsum.photos/600/300/?image=25',
alt: ''
}
}
})

expect(wrapper.attributes('alt')).toBeDefined()
expect(wrapper.attributes('alt')).toBe('')

wrapper.destroy()
})

it('has attribute width when prop width set', async () => {
const wrapper = mount(BCardImg, {
context: {
@@ -33,8 +33,8 @@ export const props = {
// default: null
},
alt: {
type: String
// default: null
type: String,
default: null
},
width: {
type: [Number, String]
@@ -153,7 +153,7 @@ export const BImg = /*#__PURE__*/ Vue.extend({
mergeData(data, {
attrs: {
src: src,
alt: props.alt || null,
alt: props.alt,
width: width ? toString(width) : null,
height: height ? toString(height) : null,
srcset: srcset || null,
@@ -30,6 +30,22 @@ describe('img', () => {
wrapper.destroy()
})

it('default does not have attributes alt, width, or height', async () => {
const wrapper = mount(BImg, {
context: {
props: {
src: 'https://picsum.photos/600/300/?image=25'
}
}
})

expect(wrapper.attributes('alt')).not.toBeDefined()
expect(wrapper.attributes('width')).not.toBeDefined()
expect(wrapper.attributes('height')).not.toBeDefined()

wrapper.destroy()
})

it('should have class "img-fluid" when prop fluid set', async () => {
const wrapper = mount(BImg, {
propsData: {
@@ -223,4 +239,17 @@ describe('img', () => {

wrapper.destroy()
})

it('should have alt attribute when `alt` prop is empty', async () => {
const wrapper = mount(BImg, {
propsData: {
alt: ''
}
})

expect(wrapper.attributes('alt')).toBeDefined()
expect(wrapper.attributes('alt')).toEqual('')

wrapper.destroy()
})
})

1 comment on commit b22829d

@vercel
Copy link

@vercel vercel bot commented on b22829d Jul 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.