debian-mirror-gitlab/spec/frontend/groups/components/group_item_spec.js

265 lines
8.3 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import Vue from 'vue';
2020-05-24 23:13:21 +05:30
import mountComponent from 'helpers/vue_mount_component_helper';
2018-03-17 18:26:18 +05:30
import groupFolderComponent from '~/groups/components/group_folder.vue';
2021-03-11 19:13:27 +05:30
import groupItemComponent from '~/groups/components/group_item.vue';
2018-03-17 18:26:18 +05:30
import eventHub from '~/groups/event_hub';
2021-03-11 19:13:27 +05:30
import { getGroupItemMicrodata } from '~/groups/store/utils';
2020-05-24 23:13:21 +05:30
import * as urlUtilities from '~/lib/utils/url_utility';
2018-03-17 18:26:18 +05:30
import { mockParentGroupItem, mockChildren } from '../mock_data';
const createComponent = (group = mockParentGroupItem, parentGroup = mockChildren[0]) => {
const Component = Vue.extend(groupItemComponent);
return mountComponent(Component, {
group,
parentGroup,
});
};
describe('GroupItemComponent', () => {
let vm;
2020-05-24 23:13:21 +05:30
beforeEach(() => {
2021-03-11 19:13:27 +05:30
Vue.component('GroupFolder', groupFolderComponent);
2018-03-17 18:26:18 +05:30
vm = createComponent();
2020-05-24 23:13:21 +05:30
return Vue.nextTick();
2018-03-17 18:26:18 +05:30
});
afterEach(() => {
vm.$destroy();
});
2021-03-08 18:12:59 +05:30
const withMicrodata = (group) => ({
2021-02-22 17:27:13 +05:30
...group,
microdata: getGroupItemMicrodata(group),
});
2018-03-17 18:26:18 +05:30
describe('computed', () => {
describe('groupDomId', () => {
it('should return ID string suffixed with group ID', () => {
expect(vm.groupDomId).toBe('group-55');
});
});
describe('rowClass', () => {
it('should return map of classes based on group details', () => {
const classes = ['is-open', 'has-children', 'has-description', 'being-removed'];
2018-11-08 19:23:39 +05:30
const { rowClass } = vm;
2018-03-17 18:26:18 +05:30
expect(Object.keys(rowClass).length).toBe(classes.length);
2021-03-08 18:12:59 +05:30
Object.keys(rowClass).forEach((className) => {
2018-12-13 13:39:08 +05:30
expect(classes.indexOf(className)).toBeGreaterThan(-1);
2018-03-17 18:26:18 +05:30
});
});
});
describe('hasChildren', () => {
it('should return boolean value representing if group has any children present', () => {
let newVm;
2020-05-24 23:13:21 +05:30
const group = { ...mockParentGroupItem };
2018-03-17 18:26:18 +05:30
group.childrenCount = 5;
newVm = createComponent(group);
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect(newVm.hasChildren).toBeTruthy();
newVm.$destroy();
group.childrenCount = 0;
newVm = createComponent(group);
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect(newVm.hasChildren).toBeFalsy();
newVm.$destroy();
});
});
describe('hasAvatar', () => {
it('should return boolean value representing if group has any avatar present', () => {
let newVm;
2020-05-24 23:13:21 +05:30
const group = { ...mockParentGroupItem };
2018-03-17 18:26:18 +05:30
group.avatarUrl = null;
newVm = createComponent(group);
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect(newVm.hasAvatar).toBeFalsy();
newVm.$destroy();
group.avatarUrl = '/uploads/group_avatar.png';
newVm = createComponent(group);
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect(newVm.hasAvatar).toBeTruthy();
newVm.$destroy();
});
});
describe('isGroup', () => {
it('should return boolean value representing if group item is of type `group` or not', () => {
let newVm;
2020-05-24 23:13:21 +05:30
const group = { ...mockParentGroupItem };
2018-03-17 18:26:18 +05:30
group.type = 'group';
newVm = createComponent(group);
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect(newVm.isGroup).toBeTruthy();
newVm.$destroy();
group.type = 'project';
newVm = createComponent(group);
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect(newVm.isGroup).toBeFalsy();
newVm.$destroy();
});
});
});
describe('methods', () => {
describe('onClickRowGroup', () => {
let event;
beforeEach(() => {
const classList = {
contains() {
return false;
},
};
event = {
target: {
classList,
parentElement: {
classList,
},
},
};
});
it('should emit `toggleChildren` event when expand is clicked on a group and it has children present', () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
2018-03-17 18:26:18 +05:30
vm.onClickRowGroup(event);
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect(eventHub.$emit).toHaveBeenCalledWith('toggleChildren', vm.group);
});
2020-05-24 23:13:21 +05:30
it('should navigate page to group homepage if group does not have any children present', () => {
jest.spyOn(urlUtilities, 'visitUrl').mockImplementation();
const group = { ...mockParentGroupItem };
2018-03-17 18:26:18 +05:30
group.childrenCount = 0;
const newVm = createComponent(group);
2020-05-24 23:13:21 +05:30
jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
2018-03-17 18:26:18 +05:30
newVm.onClickRowGroup(event);
2020-05-24 23:13:21 +05:30
expect(eventHub.$emit).not.toHaveBeenCalled();
expect(urlUtilities.visitUrl).toHaveBeenCalledWith(newVm.group.relativePath);
2018-03-17 18:26:18 +05:30
});
});
});
describe('template', () => {
2020-03-13 15:44:24 +05:30
let group = null;
describe('for a group pending deletion', () => {
beforeEach(() => {
group = { ...mockParentGroupItem, pendingRemoval: true };
vm = createComponent(group);
});
it('renders the group pending removal badge', () => {
const badgeEl = vm.$el.querySelector('.badge-warning');
expect(badgeEl).toBeDefined();
2020-05-24 23:13:21 +05:30
expect(badgeEl.innerHTML).toContain('pending removal');
2020-03-13 15:44:24 +05:30
});
});
describe('for a group not scheduled for deletion', () => {
beforeEach(() => {
group = { ...mockParentGroupItem, pendingRemoval: false };
vm = createComponent(group);
});
it('does not render the group pending removal badge', () => {
const groupTextContainer = vm.$el.querySelector('.group-text-container');
2020-05-24 23:13:21 +05:30
expect(groupTextContainer).not.toContain('pending removal');
2020-03-13 15:44:24 +05:30
});
});
2018-03-17 18:26:18 +05:30
it('should render component template correctly', () => {
2019-09-30 21:07:59 +05:30
const visibilityIconEl = vm.$el.querySelector('.item-visibility');
2018-03-17 18:26:18 +05:30
expect(vm.$el.getAttribute('id')).toBe('group-55');
expect(vm.$el.classList.contains('group-row')).toBeTruthy();
expect(vm.$el.querySelector('.group-row-contents')).toBeDefined();
expect(vm.$el.querySelector('.group-row-contents .controls')).toBeDefined();
expect(vm.$el.querySelector('.group-row-contents .stats')).toBeDefined();
expect(vm.$el.querySelector('.folder-toggle-wrap')).toBeDefined();
expect(vm.$el.querySelector('.folder-toggle-wrap .folder-caret')).toBeDefined();
expect(vm.$el.querySelector('.folder-toggle-wrap .item-type-icon')).toBeDefined();
expect(vm.$el.querySelector('.avatar-container')).toBeDefined();
expect(vm.$el.querySelector('.avatar-container a.no-expand')).toBeDefined();
expect(vm.$el.querySelector('.avatar-container .avatar')).toBeDefined();
expect(vm.$el.querySelector('.title')).toBeDefined();
expect(vm.$el.querySelector('.title a.no-expand')).toBeDefined();
2019-09-30 21:07:59 +05:30
expect(visibilityIconEl).not.toBe(null);
2021-01-03 14:25:43 +05:30
expect(visibilityIconEl.title).toBe(vm.visibilityTooltip);
2019-09-30 21:07:59 +05:30
expect(visibilityIconEl.querySelectorAll('svg').length).toBeGreaterThan(0);
2018-03-17 18:26:18 +05:30
expect(vm.$el.querySelector('.access-type')).toBeDefined();
expect(vm.$el.querySelector('.description')).toBeDefined();
expect(vm.$el.querySelector('.group-list-tree')).toBeDefined();
});
});
2021-02-22 17:27:13 +05:30
describe('schema.org props', () => {
describe('when showSchemaMarkup is disabled on the group', () => {
2021-03-08 18:12:59 +05:30
it.each(['itemprop', 'itemtype', 'itemscope'], 'it does not set %s', (attr) => {
2021-02-22 17:27:13 +05:30
expect(vm.$el.getAttribute(attr)).toBeNull();
});
it.each(
['.js-group-avatar', '.js-group-name', '.js-group-description'],
'it does not set `itemprop` on sub-nodes',
2021-03-08 18:12:59 +05:30
(selector) => {
2021-02-22 17:27:13 +05:30
expect(vm.$el.querySelector(selector).getAttribute('itemprop')).toBeNull();
},
);
});
describe('when group has microdata', () => {
beforeEach(() => {
const group = withMicrodata({
...mockParentGroupItem,
avatarUrl: 'http://foo.bar',
description: 'Foo Bar',
});
vm = createComponent(group);
});
it.each`
attr | value
${'itemscope'} | ${'itemscope'}
${'itemtype'} | ${'https://schema.org/Organization'}
${'itemprop'} | ${'subOrganization'}
`('it does set correct $attr', ({ attr, value } = {}) => {
expect(vm.$el.getAttribute(attr)).toBe(value);
});
it.each`
selector | propValue
${'[data-testid="group-avatar"]'} | ${'logo'}
${'[data-testid="group-name"]'} | ${'name'}
${'[data-testid="group-description"]'} | ${'description'}
`('it does set correct $selector', ({ selector, propValue } = {}) => {
expect(vm.$el.querySelector(selector).getAttribute('itemprop')).toBe(propValue);
});
});
});
2018-03-17 18:26:18 +05:30
});