Commit 9eddc7be authored by Dhiraj Bodicherla's avatar Dhiraj Bodicherla

Clean up frequent items components

Moved frequent items list item specs to jest
Replaced js- attributes with refs
parent 37c42f87
<script> <script>
/* eslint-disable vue/require-default-prop */ /* eslint-disable vue/require-default-prop */
import { isEmpty, isString } from 'lodash';
import Identicon from '~/vue_shared/components/identicon.vue'; import Identicon from '~/vue_shared/components/identicon.vue';
import highlight from '~/lib/utils/highlight'; import highlight from '~/lib/utils/highlight';
import { truncateNamespace } from '~/lib/utils/text_utility'; import { truncateNamespace } from '~/lib/utils/text_utility';
...@@ -38,9 +37,6 @@ export default { ...@@ -38,9 +37,6 @@ export default {
}, },
}, },
computed: { computed: {
hasAvatar() {
return isString(this.avatarUrl) && !isEmpty(this.avatarUrl);
},
truncatedNamespace() { truncatedNamespace() {
return truncateNamespace(this.namespace); return truncateNamespace(this.namespace);
}, },
...@@ -54,8 +50,11 @@ export default { ...@@ -54,8 +50,11 @@ export default {
<template> <template>
<li class="frequent-items-list-item-container"> <li class="frequent-items-list-item-container">
<a :href="webUrl" class="clearfix"> <a :href="webUrl" class="clearfix">
<div class="frequent-items-item-avatar-container avatar-container rect-avatar s32"> <div
<img v-if="hasAvatar" :src="avatarUrl" class="avatar s32" /> ref="frequentItemsItemAvatarContainer"
class="frequent-items-item-avatar-container avatar-container rect-avatar s32"
>
<img v-if="avatarUrl" ref="frequentItemsItemAvatar" :src="avatarUrl" class="avatar s32" />
<identicon <identicon
v-else v-else
:entity-id="itemId" :entity-id="itemId"
...@@ -64,16 +63,18 @@ export default { ...@@ -64,16 +63,18 @@ export default {
class="rect-avatar" class="rect-avatar"
/> />
</div> </div>
<div class="frequent-items-item-metadata-container"> <div ref="frequentItemsItemMetadataContainer" class="frequent-items-item-metadata-container">
<div <div
ref="frequentItemsItemTitle"
:title="itemName" :title="itemName"
class="frequent-items-item-title js-frequent-items-item-title" class="frequent-items-item-title"
v-html="highlightedItemName" v-html="highlightedItemName"
></div> ></div>
<div <div
v-if="namespace" v-if="namespace"
ref="frequentItemsItemNamespace"
:title="namespace" :title="namespace"
class="frequent-items-item-namespace js-frequent-items-item-namespace" class="frequent-items-item-namespace"
> >
{{ truncatedNamespace }} {{ truncatedNamespace }}
</div> </div>
......
import { shallowMount, createLocalVue } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import { trimText } from 'spec/helpers/text_helper'; import { trimText } from 'helpers/text_helper';
import frequentItemsListItemComponent from '~/frequent_items/components/frequent_items_list_item.vue'; import frequentItemsListItemComponent from '~/frequent_items/components/frequent_items_list_item.vue';
import { mockProject } from '../mock_data'; // can also use 'mockGroup', but not useful to test here import mockData from '../mock_data'; // can also use 'mockGroup', but not useful to test here
const localVue = createLocalVue(); const mockProject = mockData();
describe('FrequentItemsListItemComponent', () => { describe('FrequentItemsListItemComponent', () => {
let wrapper; let wrapper;
const findTitle = () => wrapper.find({ ref: 'frequentItemsItemTitle' });
const findAvatar = () => wrapper.find({ ref: 'frequentItemsItemAvatar' });
const findAllTitles = () => wrapper.findAll({ ref: 'frequentItemsItemTitle' });
const findNamespace = () => wrapper.find({ ref: 'frequentItemsItemNamespace' });
const findAllAnchors = () => wrapper.findAll('a');
const findAllNamespace = () => wrapper.findAll({ ref: 'frequentItemsItemNamespace' });
const findAvatarContainer = () => wrapper.findAll({ ref: 'frequentItemsItemAvatarContainer' });
const findAllMetadataContainers = () =>
wrapper.findAll({ ref: 'frequentItemsItemMetadataContainer' });
const createComponent = (props = {}) => { const createComponent = (props = {}) => {
wrapper = shallowMount(localVue.extend(frequentItemsListItemComponent), { wrapper = shallowMount(frequentItemsListItemComponent, {
propsData: { propsData: {
itemId: mockProject.id, itemId: mockProject.id,
itemName: mockProject.name, itemName: mockProject.name,
...@@ -18,7 +28,6 @@ describe('FrequentItemsListItemComponent', () => { ...@@ -18,7 +28,6 @@ describe('FrequentItemsListItemComponent', () => {
avatarUrl: mockProject.avatarUrl, avatarUrl: mockProject.avatarUrl,
...props, ...props,
}, },
localVue,
}); });
}; };
...@@ -28,35 +37,17 @@ describe('FrequentItemsListItemComponent', () => { ...@@ -28,35 +37,17 @@ describe('FrequentItemsListItemComponent', () => {
}); });
describe('computed', () => { describe('computed', () => {
describe('hasAvatar', () => {
it('should return `true` or `false` if whether avatar is present or not', () => {
createComponent({ avatarUrl: 'path/to/avatar.png' });
expect(wrapper.vm.hasAvatar).toBe(true);
});
it('should return `false` if avatar is not present', () => {
createComponent({ avatarUrl: null });
expect(wrapper.vm.hasAvatar).toBe(false);
});
});
describe('highlightedItemName', () => { describe('highlightedItemName', () => {
it('should enclose part of project name in <b> & </b> which matches with `matcher` prop', () => { it('should enclose part of project name in <b> & </b> which matches with `matcher` prop', () => {
createComponent({ matcher: 'lab' }); createComponent({ matcher: 'lab' });
expect(wrapper.find('.js-frequent-items-item-title').html()).toContain( expect(findTitle().element.innerHTML).toContain('<b>L</b><b>a</b><b>b</b>');
'<b>L</b><b>a</b><b>b</b>',
);
}); });
it('should return project name as it is if `matcher` is not available', () => { it('should return project name as it is if `matcher` is not available', () => {
createComponent({ matcher: null }); createComponent({ matcher: null });
expect(trimText(wrapper.find('.js-frequent-items-item-title').text())).toBe( expect(trimText(findTitle().text())).toBe(mockProject.name);
mockProject.name,
);
}); });
}); });
...@@ -64,7 +55,7 @@ describe('FrequentItemsListItemComponent', () => { ...@@ -64,7 +55,7 @@ describe('FrequentItemsListItemComponent', () => {
it('should truncate project name from namespace string', () => { it('should truncate project name from namespace string', () => {
createComponent({ namespace: 'platform / nokia-3310' }); createComponent({ namespace: 'platform / nokia-3310' });
expect(trimText(wrapper.find('.js-frequent-items-item-namespace').text())).toBe('platform'); expect(trimText(findNamespace().text())).toBe('platform');
}); });
it('should truncate namespace string from the middle if it includes more than two groups in path', () => { it('should truncate namespace string from the middle if it includes more than two groups in path', () => {
...@@ -72,23 +63,41 @@ describe('FrequentItemsListItemComponent', () => { ...@@ -72,23 +63,41 @@ describe('FrequentItemsListItemComponent', () => {
namespace: 'platform / hardware / broadcom / Wifi Group / Mobile Chipset / nokia-3310', namespace: 'platform / hardware / broadcom / Wifi Group / Mobile Chipset / nokia-3310',
}); });
expect(trimText(wrapper.find('.js-frequent-items-item-namespace').text())).toBe( expect(trimText(findNamespace().text())).toBe('platform / ... / Mobile Chipset');
'platform / ... / Mobile Chipset',
);
}); });
}); });
}); });
describe('template', () => { describe('template', () => {
it('should render component element', () => { beforeEach(() => {
createComponent(); createComponent();
});
it('should render avatar if avatarUrl is present', () => {
wrapper.setProps({ avatarUrl: 'path/to/avatar.png' });
return wrapper.vm.$nextTick(() => {
expect(findAvatar().exists()).toBe(true);
});
});
it('should not render avatar if avatarUrl is not present', () => {
expect(findAvatar().exists()).toBe(false);
});
it('renders root element with the right classes', () => {
expect(wrapper.classes('frequent-items-list-item-container')).toBe(true);
});
expect(wrapper.classes()).toContain('frequent-items-list-item-container'); it.each`
expect(wrapper.findAll('a').length).toBe(1); name | selector | expected
expect(wrapper.findAll('.frequent-items-item-avatar-container').length).toBe(1); ${'anchor'} | ${findAllAnchors} | ${1}
expect(wrapper.findAll('.frequent-items-item-metadata-container').length).toBe(1); ${'avatar container'} | ${findAvatarContainer} | ${1}
expect(wrapper.findAll('.frequent-items-item-title').length).toBe(1); ${'metadata container'} | ${findAllMetadataContainers} | ${1}
expect(wrapper.findAll('.frequent-items-item-namespace').length).toBe(1); ${'title'} | ${findAllTitles} | ${1}
${'namespace'} | ${findAllNamespace} | ${1}
`('should render $expected $name', ({ selector, expected }) => {
expect(selector()).toHaveLength(expected);
}); });
}); });
}); });
import { TEST_HOST } from 'helpers/test_constants';
export default () => ({
id: 1,
name: 'GitLab Community Edition',
namespace: 'gitlab-org / gitlab-ce',
webUrl: `${TEST_HOST}/gitlab-org/gitlab-foss`,
avatarUrl: null,
});
Markdown is supported
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