gitlab-org--gitlab-foss/spec/frontend/vue_shared/components/commit_spec.js

232 lines
6.7 KiB
JavaScript
Raw Normal View History

import { shallowMount } from '@vue/test-utils';
import { GlIcon } from '@gitlab/ui';
import CommitComponent from '~/vue_shared/components/commit.vue';
import UserAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';
describe('Commit component', () => {
let props;
let wrapper;
const findIcon = name => {
const icons = wrapper.findAll(GlIcon).filter(c => c.attributes('name') === name);
return icons.length ? icons.at(0) : icons;
};
const findUserAvatar = () => wrapper.find(UserAvatarLink);
const createComponent = propsData => {
wrapper = shallowMount(CommitComponent, {
propsData,
});
};
2018-04-14 16:02:04 -04:00
afterEach(() => {
wrapper.destroy();
2018-04-14 16:02:04 -04:00
});
2017-12-06 13:38:35 -05:00
it('should render a fork icon if it does not represent a tag', () => {
createComponent({
2018-04-14 16:02:04 -04:00
tag: false,
commitRef: {
name: 'master',
ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
},
2018-04-14 16:02:04 -04:00
commitUrl:
'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
2018-04-14 16:02:04 -04:00
shortSha: 'b7836edd',
title: 'Commit message',
author: {
avatar_url: 'https://gitlab.com/uploads/-/system/user/avatar/300478/avatar.png',
web_url: 'https://gitlab.com/jschatz1',
path: '/jschatz1',
username: 'jschatz1',
},
});
expect(
wrapper
.find('.icon-container')
.find(GlIcon)
.exists(),
).toBe(true);
});
describe('Given all the props', () => {
beforeEach(() => {
props = {
tag: true,
2016-12-08 07:06:49 -05:00
commitRef: {
name: 'master',
ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
},
2018-04-14 16:02:04 -04:00
commitUrl:
'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
2016-12-08 07:06:49 -05:00
shortSha: 'b7836edd',
title: 'Commit message',
author: {
2017-07-17 09:46:59 -04:00
avatar_url: 'https://gitlab.com/uploads/-/system/user/avatar/300478/avatar.png',
web_url: 'https://gitlab.com/jschatz1',
path: '/jschatz1',
username: 'jschatz1',
},
};
createComponent(props);
});
it('should render a tag icon if it represents a tag', () => {
expect(findIcon('tag').exists()).toBe(true);
});
it('should render a link to the ref url', () => {
expect(wrapper.find('.ref-name').attributes('href')).toBe(props.commitRef.ref_url);
});
it('should render the ref name', () => {
expect(wrapper.find('.ref-name').text()).toContain(props.commitRef.name);
});
it('should render the commit short sha with a link to the commit url', () => {
expect(wrapper.find('.commit-sha').attributes('href')).toEqual(props.commitUrl);
2018-10-09 14:03:09 -04:00
expect(wrapper.find('.commit-sha').text()).toContain(props.shortSha);
2016-12-08 07:06:49 -05:00
});
it('should render icon for commit', () => {
expect(findIcon('commit').exists()).toBe(true);
});
describe('Given commit title and author props', () => {
2016-12-08 07:06:49 -05:00
it('should render a link to the author profile', () => {
const userAvatar = findUserAvatar();
expect(userAvatar.props('linkHref')).toBe(props.author.path);
});
it('Should render the author avatar with title and alt attributes', () => {
const userAvatar = findUserAvatar();
expect(userAvatar.exists()).toBe(true);
expect(userAvatar.props('imgAlt')).toBe(`${props.author.username}'s avatar`);
});
});
it('should render the commit title', () => {
expect(wrapper.find('.commit-row-message').attributes('href')).toEqual(props.commitUrl);
2018-10-09 14:03:09 -04:00
expect(wrapper.find('.commit-row-message').text()).toContain(props.title);
});
});
describe('When commit title is not provided', () => {
2016-12-08 07:06:49 -05:00
it('should render default message', () => {
props = {
tag: false,
2016-12-08 07:06:49 -05:00
commitRef: {
name: 'master',
ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
},
2018-04-14 16:02:04 -04:00
commitUrl:
'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
2016-12-08 07:06:49 -05:00
shortSha: 'b7836edd',
title: null,
author: {},
};
createComponent(props);
expect(wrapper.find('.commit-title span').text()).toContain(
2018-04-23 07:34:05 -04:00
"Can't find HEAD commit for this branch",
2018-04-14 16:02:04 -04:00
);
});
});
describe('When commit ref is provided, but merge ref is not', () => {
it('should render the commit ref', () => {
props = {
tag: false,
commitRef: {
name: 'master',
ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
},
commitUrl:
'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
shortSha: 'b7836edd',
title: null,
author: {},
};
createComponent(props);
const refEl = wrapper.find('.ref-name');
expect(refEl.text()).toContain('master');
expect(refEl.attributes('href')).toBe(props.commitRef.ref_url);
expect(refEl.attributes('title')).toBe(props.commitRef.name);
expect(findIcon('branch').exists()).toBe(true);
});
});
describe('When both commit and merge ref are provided', () => {
it('should render the merge ref', () => {
props = {
tag: false,
commitRef: {
name: 'master',
ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
},
commitUrl:
'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
mergeRequestRef: {
iid: 1234,
path: 'https://example.com/path/to/mr',
title: 'Test MR',
},
shortSha: 'b7836edd',
title: null,
author: {},
};
createComponent(props);
const refEl = wrapper.find('.ref-name');
expect(refEl.text()).toContain('1234');
expect(refEl.attributes('href')).toBe(props.mergeRequestRef.path);
expect(refEl.attributes('title')).toBe(props.mergeRequestRef.title);
expect(findIcon('git-merge').exists()).toBe(true);
});
});
describe('When showRefInfo === false', () => {
it('should not render any ref info', () => {
props = {
tag: false,
commitRef: {
name: 'master',
ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
},
commitUrl:
'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
mergeRequestRef: {
iid: 1234,
path: '/path/to/mr',
title: 'Test MR',
},
shortSha: 'b7836edd',
title: null,
author: {},
showRefInfo: false,
};
createComponent(props);
expect(wrapper.find('.ref-name').exists()).toBe(false);
});
});
});