-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(card-img-lazy): new card-img-lazy sub-component (#2647)
* feat(card-img-lazy): New card lazy load image sub-component * Update index.js * Update package.json * Update card-img-lazy.js * Create card-img-lazy.spec.js * Update card-img-lazy.js * Update card-img-lazy.spec.js * Update README.md * Update README.md * Update card-img-lazy.js * Update card-img-lazy.js * Update card-img-lazy.js * lint * Add `omit()` util * Prettify
- Loading branch information
1 parent
c3c65e4
commit d2e1f8a
Showing
7 changed files
with
263 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import BImgLazy from '../image/img-lazy' | ||
import { omit } from '../../utils/object' | ||
import { mergeData } from 'vue-functional-data-merge' | ||
|
||
// Copy of `<b-img-lazy>` props, and remove conflicting/non-applicable props | ||
// The `omit()` util creates a new object, so we can just pass the original props | ||
const lazyProps = omit(BImgLazy.props, [ | ||
'left', | ||
'right', | ||
'center', | ||
'block', | ||
'rounded', | ||
'thumbnail', | ||
'fluid', | ||
'fluidGrow' | ||
]) | ||
|
||
export const props = { | ||
...lazyProps, | ||
top: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
bottom: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
left: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
start: { | ||
type: Boolean, | ||
default: false | ||
// alias of 'left' | ||
}, | ||
right: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
end: { | ||
type: Boolean, | ||
default: false | ||
// alias of 'right' | ||
} | ||
} | ||
|
||
// @vue/component | ||
export default { | ||
name: 'BCardImgLazy', | ||
functional: true, | ||
props, | ||
render(h, { props, data }) { | ||
let baseClass = 'card-img' | ||
if (props.top) { | ||
baseClass += '-top' | ||
} else if (props.right || props.end) { | ||
baseClass += '-right' | ||
} else if (props.bottom) { | ||
baseClass += '-bottom' | ||
} else if (props.left || props.start) { | ||
baseClass += '-left' | ||
} | ||
|
||
// False out the left/center/right props before passing to b-img-lazy | ||
const lazyProps = { ...props, left: false, right: false, center: false } | ||
return h( | ||
BImgLazy, | ||
mergeData(data, { | ||
class: [baseClass], | ||
props: lazyProps | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import CardImgLazy from './card-img-lazy' | ||
import { mount } from '@vue/test-utils' | ||
|
||
describe('card-image', async () => { | ||
it('default has tag "img"', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25' | ||
} | ||
} | ||
}) | ||
expect(wrapper.is('img')).toBe(true) | ||
}) | ||
|
||
it('default has data src attribute', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25' | ||
} | ||
} | ||
}) | ||
expect(wrapper.attributes('src')).toContain('data:image/svg+xml') | ||
}) | ||
|
||
it('default does not have alt attribute', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25' | ||
} | ||
} | ||
}) | ||
expect(wrapper.attributes('alt')).not.toBeDefined() | ||
}) | ||
|
||
it('default has attributes width and height set to 1', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25' | ||
} | ||
} | ||
}) | ||
expect(wrapper.attributes('width')).toBeDefined() | ||
expect(wrapper.attributes('width')).toBe('1') | ||
expect(wrapper.attributes('height')).toBeDefined() | ||
expect(wrapper.attributes('height')).toBe('1') | ||
}) | ||
|
||
it('default has class "card-img"', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25' | ||
} | ||
} | ||
}) | ||
expect(wrapper.classes()).toContain('card-img') | ||
}) | ||
|
||
it('has class "card-img-top" when prop top=true', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25', | ||
top: true | ||
} | ||
} | ||
}) | ||
expect(wrapper.classes()).toContain('card-img-top') | ||
}) | ||
|
||
it('has class "card-img-bottom" when prop bottom=true', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25', | ||
bottom: true | ||
} | ||
} | ||
}) | ||
expect(wrapper.classes()).toContain('card-img-bottom') | ||
}) | ||
|
||
it('has class "card-img-top" when props top=true and bottom=true', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25', | ||
top: true, | ||
bottom: true | ||
} | ||
} | ||
}) | ||
expect(wrapper.classes()).toContain('card-img-top') | ||
}) | ||
|
||
it('has class "card-img-left" when prop left=true', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25', | ||
left: true | ||
} | ||
} | ||
}) | ||
expect(wrapper.classes()).toContain('card-img-left') | ||
}) | ||
|
||
it('has class "card-img-right" when prop right=true', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25', | ||
right: true | ||
} | ||
} | ||
}) | ||
expect(wrapper.classes()).toContain('card-img-right') | ||
}) | ||
|
||
it('has attribute alt when prop alt set', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25', | ||
alt: 'image' | ||
} | ||
} | ||
}) | ||
expect(wrapper.attributes('alt')).toBeDefined() | ||
expect(wrapper.attributes('alt')).toBe('image') | ||
}) | ||
|
||
it('has attribute width when prop width set', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25', | ||
width: '600' | ||
} | ||
} | ||
}) | ||
expect(wrapper.attributes('width')).toBeDefined() | ||
expect(wrapper.attributes('width')).toBe('600') | ||
}) | ||
|
||
it('has attribute heigth when prop height set', async () => { | ||
const wrapper = mount(CardImgLazy, { | ||
context: { | ||
props: { | ||
src: 'https://picsum.photos/600/300/?image=25', | ||
height: '300' | ||
} | ||
} | ||
}) | ||
expect(wrapper.attributes('height')).toBeDefined() | ||
expect(wrapper.attributes('height')).toBe('300') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
"BCardTitle", | ||
"BCardSubTitle", | ||
"BCardImg", | ||
"BCardImgLazy", | ||
"BCardText", | ||
"BCardGroup" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters