Commit 755d9980 authored by Eric Eastwood's avatar Eric Eastwood
Browse files

Add lazy option to UserAvatarImage

parent f58fe3b66ead
......@@ -7,6 +7,7 @@
Sample configuration:
<user-avatar-image
:lazy="true"
:img-src="userAvatarSrc"
:img-alt="tooltipText"
:tooltip-text="tooltipText"
......@@ -16,8 +17,9 @@
*/
import defaultAvatarUrl from 'images/no_avatar.png';
import { placeholderImage } from '../../../lazy_loader';
import tooltip from '../../directives/tooltip';
export default {
name: 'UserAvatarImage',
props: {
......@@ -19,8 +21,13 @@
import tooltip from '../../directives/tooltip';
export default {
name: 'UserAvatarImage',
props: {
lazy: {
type: Boolean,
required: false,
default: false,
},
imgSrc: {
type: String,
required: false,
......@@ -56,9 +63,18 @@
tooltip,
},
computed: {
// API response sends null when gravatar is disabled and
// we provide an empty string when we use it inside user avatar link.
// In both cases we should render the defaultAvatarUrl
sanitizedSource() {
return this.imgSrc === '' || this.imgSrc === null ? defaultAvatarUrl : this.imgSrc;
},
resultantSrcAttribute() {
return this.lazy ? placeholderImage : this.sanitizedSource;
},
tooltipContainer() {
return this.tooltipText ? 'body' : null;
},
avatarSizeClass() {
return `s${this.size}`;
},
......@@ -59,15 +75,9 @@
tooltipContainer() {
return this.tooltipText ? 'body' : null;
},
avatarSizeClass() {
return `s${this.size}`;
},
// API response sends null when gravatar is disabled and
// we provide an empty string when we use it inside user avatar link.
// In both cases we should render the defaultAvatarUrl
imageSource() {
return this.imgSrc === '' || this.imgSrc === null ? defaultAvatarUrl : this.imgSrc;
},
},
};
</script>
......@@ -76,8 +86,12 @@
<img
v-tooltip
class="avatar"
:class="[avatarSizeClass, cssClasses]"
:src="imageSource"
:class="{
lazy,
[avatarSizeClass]: true,
[cssClasses]: true
}"
:src="resultantSrcAttribute"
:width="size"
:height="size"
:alt="imgAlt"
......@@ -81,6 +95,7 @@
:width="size"
:height="size"
:alt="imgAlt"
:data-src="sanitizedSource"
:data-container="tooltipContainer"
:data-placement="tooltipPlacement"
:title="tooltipText"
......
---
title: Add lazy option to UserAvatarImage
merge_request: 14895
author:
type: changed
import Vue from 'vue';
import UserAvatarImage from '~/vue_shared/components/user_avatar/user_avatar_image.vue';
import { placeholderImage } from '~/lazy_loader';
import userAvatarImage from '~/vue_shared/components/user_avatar/user_avatar_image.vue';
import mountComponent from '../../../helpers/vue_mount_component_helper';
......@@ -3,4 +5,11 @@
const UserAvatarImageComponent = Vue.extend(UserAvatarImage);
const DEFAULT_PROPS = {
size: 99,
imgSrc: 'myavatarurl.com',
imgAlt: 'mydisplayname',
cssClasses: 'myextraavatarclass',
tooltipText: 'tooltip text',
tooltipPlacement: 'bottom',
};
describe('User Avatar Image Component', function () {
......@@ -5,4 +14,11 @@
describe('User Avatar Image Component', function () {
let vm;
let UserAvatarImage;
beforeEach(() => {
UserAvatarImage = Vue.extend(userAvatarImage);
});
describe('Initialization', function () {
beforeEach(function () {
......@@ -7,17 +23,8 @@
describe('Initialization', function () {
beforeEach(function () {
this.propsData = {
size: 99,
imgSrc: 'myavatarurl.com',
imgAlt: 'mydisplayname',
cssClasses: 'myextraavatarclass',
tooltipText: 'tooltip text',
tooltipPlacement: 'bottom',
};
this.userAvatarImage = new UserAvatarImageComponent({
propsData: this.propsData,
vm = mountComponent(UserAvatarImage, {
...DEFAULT_PROPS,
}).$mount();
});
it('should return a defined Vue component', function () {
......@@ -20,8 +27,8 @@
}).$mount();
});
it('should return a defined Vue component', function () {
expect(this.userAvatarImage).toBeDefined();
expect(vm).toBeDefined();
});
it('should have <img> as a child element', function () {
......@@ -25,7 +32,10 @@
});
it('should have <img> as a child element', function () {
expect(this.userAvatarImage.$el.tagName).toBe('IMG');
expect(vm.$el.tagName).toBe('IMG');
expect(vm.$el.getAttribute('src')).toBe(DEFAULT_PROPS.imgSrc);
expect(vm.$el.getAttribute('data-src')).toBe(DEFAULT_PROPS.imgSrc);
expect(vm.$el.getAttribute('alt')).toBe(DEFAULT_PROPS.imgAlt);
});
it('should properly compute tooltipContainer', function () {
......@@ -29,7 +39,7 @@
});
it('should properly compute tooltipContainer', function () {
expect(this.userAvatarImage.tooltipContainer).toBe('body');
expect(vm.tooltipContainer).toBe('body');
});
it('should properly render tooltipContainer', function () {
......@@ -33,7 +43,7 @@
});
it('should properly render tooltipContainer', function () {
expect(this.userAvatarImage.$el.getAttribute('data-container')).toBe('body');
expect(vm.$el.getAttribute('data-container')).toBe('body');
});
it('should properly compute avatarSizeClass', function () {
......@@ -37,7 +47,7 @@
});
it('should properly compute avatarSizeClass', function () {
expect(this.userAvatarImage.avatarSizeClass).toBe('s99');
expect(vm.avatarSizeClass).toBe('s99');
});
it('should properly render img css', function () {
......@@ -41,6 +51,6 @@
});
it('should properly render img css', function () {
const classList = this.userAvatarImage.$el.classList;
const classList = vm.$el.classList;
const containsAvatar = classList.contains('avatar');
const containsSizeClass = classList.contains('s99');
......@@ -45,7 +55,8 @@
const containsAvatar = classList.contains('avatar');
const containsSizeClass = classList.contains('s99');
const containsCustomClass = classList.contains('myextraavatarclass');
const containsCustomClass = classList.contains(DEFAULT_PROPS.cssClasses);
const lazyClass = classList.contains('lazy');
expect(containsAvatar).toBe(true);
expect(containsSizeClass).toBe(true);
expect(containsCustomClass).toBe(true);
......@@ -48,7 +59,26 @@
expect(containsAvatar).toBe(true);
expect(containsSizeClass).toBe(true);
expect(containsCustomClass).toBe(true);
expect(lazyClass).toBe(false);
});
});
describe('Initialization when lazy', function () {
beforeEach(function () {
vm = mountComponent(UserAvatarImage, {
...DEFAULT_PROPS,
lazy: true,
}).$mount();
});
it('should add lazy attributes', function () {
const classList = vm.$el.classList;
const lazyClass = classList.contains('lazy');
expect(lazyClass).toBe(true);
expect(vm.$el.getAttribute('src')).toBe(placeholderImage);
expect(vm.$el.getAttribute('data-src')).toBe(DEFAULT_PROPS.imgSrc);
});
});
});
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment