gitlab-org--gitlab-foss/spec/javascripts/vue_shared/components/user_avatar/user_avatar_link_spec.js

110 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-12-19 10:12:32 +00:00
import _ from 'underscore';
2017-04-15 23:38:07 +00:00
import Vue from 'vue';
import UserAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';
import { TEST_HOST } from 'spec/test_constants';
2017-04-15 23:38:07 +00:00
2018-10-17 07:13:26 +00:00
describe('User Avatar Link Component', function() {
beforeEach(function() {
2017-04-15 23:38:07 +00:00
this.propsData = {
linkHref: `${TEST_HOST}/myavatarurl.com`,
2017-04-15 23:38:07 +00:00
imgSize: 99,
imgSrc: `${TEST_HOST}/myavatarurl.com`,
2017-04-15 23:38:07 +00:00
imgAlt: 'mydisplayname',
imgCssClasses: 'myextraavatarclass',
tooltipText: 'tooltip text',
tooltipPlacement: 'bottom',
username: 'username',
2017-04-15 23:38:07 +00:00
};
const UserAvatarLinkComponent = Vue.extend(UserAvatarLink);
this.userAvatarLink = new UserAvatarLinkComponent({
propsData: this.propsData,
}).$mount();
[this.userAvatarImage] = this.userAvatarLink.$children;
2017-04-15 23:38:07 +00:00
});
2018-10-17 07:13:26 +00:00
it('should return a defined Vue component', function() {
2017-04-15 23:38:07 +00:00
expect(this.userAvatarLink).toBeDefined();
});
2018-10-17 07:13:26 +00:00
it('should have user-avatar-image registered as child component', function() {
2017-04-15 23:38:07 +00:00
expect(this.userAvatarLink.$options.components.userAvatarImage).toBeDefined();
});
2018-10-17 07:13:26 +00:00
it('user-avatar-link should have user-avatar-image as child component', function() {
2017-04-15 23:38:07 +00:00
expect(this.userAvatarImage).toBeDefined();
});
2018-10-17 07:13:26 +00:00
it('should render <a> as a child element', function() {
const link = this.userAvatarLink.$el;
expect(link.tagName).toBe('A');
expect(link.href).toBe(this.propsData.linkHref);
2017-04-15 23:38:07 +00:00
});
it('renders imgSrc with imgSize as image', function() {
const { imgSrc, imgSize } = this.propsData;
const image = this.userAvatarLink.$el.querySelector('img');
expect(image).not.toBeNull();
expect(image.src).toBe(`${imgSrc}?width=${imgSize}`);
2017-04-15 23:38:07 +00:00
});
2018-10-30 10:53:01 +00:00
it('should return necessary props as defined', function() {
2017-04-15 23:38:07 +00:00
_.each(this.propsData, (val, key) => {
expect(this.userAvatarLink[key]).toBeDefined();
});
});
2018-10-17 07:13:26 +00:00
describe('no username', function() {
beforeEach(function(done) {
this.userAvatarLink.username = '';
Vue.nextTick(done);
});
2018-10-17 07:13:26 +00:00
it('should only render image tag in link', function() {
2017-10-27 08:20:53 +00:00
const childElements = this.userAvatarLink.$el.childNodes;
2018-10-09 18:03:09 +00:00
2018-11-07 17:20:17 +00:00
expect(this.userAvatarLink.$el.querySelector('img')).not.toBe('null');
2017-10-27 08:20:53 +00:00
// Vue will render the hidden component as <!---->
expect(childElements[1].tagName).toBeUndefined();
});
2018-10-17 07:13:26 +00:00
it('should render avatar image tooltip', function() {
2018-11-07 17:20:17 +00:00
expect(this.userAvatarLink.shouldShowUsername).toBe(false);
expect(this.userAvatarLink.avatarTooltipText).toEqual(this.propsData.tooltipText);
});
});
2018-10-17 07:13:26 +00:00
describe('username', function() {
it('should not render avatar image tooltip', function() {
expect(this.userAvatarLink.$el.querySelector('.js-user-avatar-image-toolip')).toBeNull();
});
2018-10-17 07:13:26 +00:00
it('should render username prop in <span>', function() {
2018-11-07 17:20:17 +00:00
expect(
this.userAvatarLink.$el.querySelector('.js-user-avatar-link-username').innerText.trim(),
).toEqual(this.propsData.username);
});
2018-10-17 07:13:26 +00:00
it('should render text tooltip for <span>', function() {
2018-11-07 17:20:17 +00:00
expect(
this.userAvatarLink.$el.querySelector('.js-user-avatar-link-username').dataset
.originalTitle,
).toEqual(this.propsData.tooltipText);
});
2018-10-17 07:13:26 +00:00
it('should render text tooltip placement for <span>', function() {
expect(
2018-11-07 17:20:17 +00:00
this.userAvatarLink.$el
.querySelector('.js-user-avatar-link-username')
.getAttribute('tooltip-placement'),
2018-10-17 07:13:26 +00:00
).toEqual(this.propsData.tooltipPlacement);
});
});
2017-04-15 23:38:07 +00:00
});