Commit 1c7cbc25 authored by Samantha Ming's avatar Samantha Ming Committed by Paul Slaughter

Apply patch and update spec per MR comments

- Use wrapper instead of global Vue object
- Use vue utils helper method
- Remove redundant spec tests
parent 96b994bd
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import { joinPaths } from '~/lib/utils/url_utility'; import { joinPaths } from '~/lib/utils/url_utility';
import userDataMock from '../../user_data_mock'; import { TEST_HOST } from 'helpers/test_constants';
import AssigneeAvatarLink from '~/sidebar/components/assignees/assignee_avatar_link.vue'; import AssigneeAvatarLink from '~/sidebar/components/assignees/assignee_avatar_link.vue';
import AssigneeAvatar from '~/sidebar/components/assignees/assignee_avatar.vue';
import userDataMock from '../../user_data_mock';
const TOOLTIP_PLACEMENT = 'bottom'; const TOOLTIP_PLACEMENT = 'bottom';
const { name: USER_NAME } = userDataMock(); const { name: USER_NAME, username: USER_USERNAME } = userDataMock();
const TEST_ISSUABLE_TYPE = 'merge_request';
describe('AssigneeAvatarLink component', () => { describe('AssigneeAvatarLink component', () => {
let wrapper; let wrapper;
...@@ -13,10 +16,10 @@ describe('AssigneeAvatarLink component', () => { ...@@ -13,10 +16,10 @@ describe('AssigneeAvatarLink component', () => {
const propsData = { const propsData = {
user: userDataMock(), user: userDataMock(),
showLess: true, showLess: true,
rootPath: 'http://localhost:3000/', rootPath: TEST_HOST,
tooltipPlacement: TOOLTIP_PLACEMENT, tooltipPlacement: TOOLTIP_PLACEMENT,
singleUser: false, singleUser: false,
issuableType: 'merge_request', issuableType: TEST_ISSUABLE_TYPE,
...props, ...props,
}; };
...@@ -32,24 +35,22 @@ describe('AssigneeAvatarLink component', () => { ...@@ -32,24 +35,22 @@ describe('AssigneeAvatarLink component', () => {
const findTooltipText = () => wrapper.attributes('data-original-title'); const findTooltipText = () => wrapper.attributes('data-original-title');
it('user who cannot merge has "cannot merge" in tooltip', () => { it('has the root url present in the assigneeUrl method', () => {
createComponent({ createComponent();
user: { const assigneeUrl = joinPaths(TEST_HOST, USER_USERNAME);
can_merge: false,
},
});
expect(findTooltipText().includes('cannot merge')).toBe(true); expect(wrapper.attributes().href).toEqual(assigneeUrl);
}); });
it('has the root url present in the assigneeUrl method', () => { it('renders assignee avatar', () => {
createComponent(); createComponent();
const assigneeUrl = joinPaths(
`${wrapper.props('rootPath')}`,
`${wrapper.props('user').username}`,
);
expect(wrapper.attributes().href).toEqual(assigneeUrl); expect(wrapper.find(AssigneeAvatar).props()).toEqual(
expect.objectContaining({
issuableType: TEST_ISSUABLE_TYPE,
user: userDataMock(),
}),
);
}); });
describe.each` describe.each`
......
...@@ -39,7 +39,7 @@ describe('AssigneeAvatar', () => { ...@@ -39,7 +39,7 @@ describe('AssigneeAvatar', () => {
it('does not show warning icon if assignee can merge', () => { it('does not show warning icon if assignee can merge', () => {
createComponent(); createComponent();
expect(wrapper.element.querySelector('.merge-icon')).toBeNull(); expect(wrapper.find('.merge-icon').exists()).toBe(false);
}); });
it('shows warning icon if assignee cannot merge', () => { it('shows warning icon if assignee cannot merge', () => {
...@@ -49,7 +49,7 @@ describe('AssigneeAvatar', () => { ...@@ -49,7 +49,7 @@ describe('AssigneeAvatar', () => {
}, },
}); });
expect(wrapper.element.querySelector('.merge-icon')).not.toBeNull(); expect(wrapper.find('.merge-icon').exists()).toBe(true);
}); });
it('does not show warning icon for issuableType = "issue"', () => { it('does not show warning icon for issuableType = "issue"', () => {
...@@ -57,7 +57,7 @@ describe('AssigneeAvatar', () => { ...@@ -57,7 +57,7 @@ describe('AssigneeAvatar', () => {
issuableType: 'issue', issuableType: 'issue',
}); });
expect(wrapper.element.querySelector('.merge-icon')).toBeNull(); expect(wrapper.find('.merge-icon').exists()).toBe(false);
}); });
it.each` it.each`
......
...@@ -77,54 +77,30 @@ describe('CollapsedAssigneeList component', () => { ...@@ -77,54 +77,30 @@ describe('CollapsedAssigneeList component', () => {
beforeEach(() => { beforeEach(() => {
users = UsersMockHelper.createNumberRandomUsers(2); users = UsersMockHelper.createNumberRandomUsers(2);
});
describe('default', () => {
beforeEach(() => {
createComponent({ users });
});
it('has multiple-users class', () => {
expect(wrapper.classes('multiple-users')).toBe(true);
});
it('does not display an avatar count', () => {
expect(findAvatarCounter().exists()).toBe(false);
});
it('returns just two collapsed users', () => { createComponent({ users });
expect(findAssignees().length).toBe(2);
});
}); });
it('has correct "cannot merge" tooltip when no user can merge', () => { it('has multiple-users class', () => {
users[0].can_merge = false; expect(wrapper.classes('multiple-users')).toBe(true);
users[1].can_merge = false;
createComponent({
users,
});
expect(getTooltipTitle()).toEqual(`${users[0].name}, ${users[1].name} (no one can merge)`);
}); });
it('does not have "merge" word in tooltip if everyone can merge', () => { it('does not display an avatar count', () => {
users[0].can_merge = true; expect(findAvatarCounter().exists()).toBe(false);
users[1].can_merge = true; });
createComponent({
users,
});
expect(getTooltipTitle()).toEqual(`${users[0].name}, ${users[1].name}`); it('returns just two collapsed users', () => {
expect(findAssignees().length).toBe(2);
}); });
}); });
describe('More than two assignees/users', () => { describe('More than two assignees/users', () => {
let users; let users;
let userNames;
beforeEach(() => { beforeEach(() => {
users = UsersMockHelper.createNumberRandomUsers(3); users = UsersMockHelper.createNumberRandomUsers(3);
userNames = users.map(x => x.name).join(', ');
}); });
describe('default', () => { describe('default', () => {
...@@ -142,6 +118,18 @@ describe('CollapsedAssigneeList component', () => { ...@@ -142,6 +118,18 @@ describe('CollapsedAssigneeList component', () => {
}); });
}); });
it('has corrent "no one can merge" tooltip when no one can merge', () => {
users[0].can_merge = false;
users[1].can_merge = false;
users[2].can_merge = false;
createComponent({
users,
});
expect(getTooltipTitle()).toEqual(`${userNames} (no one can merge)`);
});
it('has correct "cannot merge" tooltip when one user can merge', () => { it('has correct "cannot merge" tooltip when one user can merge', () => {
users[0].can_merge = true; users[0].can_merge = true;
users[1].can_merge = false; users[1].can_merge = false;
...@@ -151,7 +139,7 @@ describe('CollapsedAssigneeList component', () => { ...@@ -151,7 +139,7 @@ describe('CollapsedAssigneeList component', () => {
users, users,
}); });
expect(getTooltipTitle()).toContain('1/3 can merge'); expect(getTooltipTitle()).toEqual(`${userNames} (1/3 can merge)`);
}); });
it('has correct "cannot merge" tooltip when more than one user can merge', () => { it('has correct "cannot merge" tooltip when more than one user can merge', () => {
...@@ -163,7 +151,7 @@ describe('CollapsedAssigneeList component', () => { ...@@ -163,7 +151,7 @@ describe('CollapsedAssigneeList component', () => {
users, users,
}); });
expect(getTooltipTitle()).toContain('2/3 can merge'); expect(getTooltipTitle()).toEqual(`${userNames} (2/3 can merge)`);
}); });
it('does not have "merge" in tooltip if everyone can merge', () => { it('does not have "merge" in tooltip if everyone can merge', () => {
...@@ -175,10 +163,10 @@ describe('CollapsedAssigneeList component', () => { ...@@ -175,10 +163,10 @@ describe('CollapsedAssigneeList component', () => {
users, users,
}); });
expect(getTooltipTitle()).not.toContain('merge'); expect(getTooltipTitle()).toEqual(userNames);
}); });
it('displays the correct avatar count via a computed property if less than default max counter', () => { it('displays the correct avatar count', () => {
users = UsersMockHelper.createNumberRandomUsers(5); users = UsersMockHelper.createNumberRandomUsers(5);
createComponent({ createComponent({
......
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import CollapsedAssignee from '~/sidebar/components/assignees/collapsed_assignee.vue'; import CollapsedAssignee from '~/sidebar/components/assignees/collapsed_assignee.vue';
import AssigneeAvatar from '~/sidebar/components/assignees/assignee_avatar.vue';
import userDataMock from '../../user_data_mock'; import userDataMock from '../../user_data_mock';
const TEST_USER = userDataMock();
const TEST_ISSUABLE_TYPE = 'merge_request';
describe('CollapsedAssignee assignee component', () => { describe('CollapsedAssignee assignee component', () => {
let wrapper; let wrapper;
function createComponent(props = {}) { function createComponent(props = {}) {
const propsData = { const propsData = {
user: userDataMock(), user: userDataMock(),
issuableType: TEST_ISSUABLE_TYPE,
...props, ...props,
}; };
...@@ -29,6 +34,16 @@ describe('CollapsedAssignee assignee component', () => { ...@@ -29,6 +34,16 @@ describe('CollapsedAssignee assignee component', () => {
.find('.author') .find('.author')
.text() .text()
.trim(), .trim(),
).toEqual(wrapper.vm.user.name); ).toEqual(TEST_USER.name);
});
it('has assignee avatar', () => {
createComponent();
expect(wrapper.find(AssigneeAvatar).props()).toEqual({
imgSize: 24,
user: TEST_USER,
issuableType: TEST_ISSUABLE_TYPE,
});
}); });
}); });
import Vue from 'vue';
import { mount } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import UncollapsedAssigneeList from '~/sidebar/components/assignees/uncollapsed_assignee_list.vue'; import UncollapsedAssigneeList from '~/sidebar/components/assignees/uncollapsed_assignee_list.vue';
import AssigneeAvatarLink from '~/sidebar/components/assignees/assignee_avatar_link.vue'; import AssigneeAvatarLink from '~/sidebar/components/assignees/assignee_avatar_link.vue';
...@@ -56,79 +55,48 @@ describe('UncollapsedAssigneeList component', () => { ...@@ -56,79 +55,48 @@ describe('UncollapsedAssigneeList component', () => {
}); });
}); });
describe('Two or more assignees/users', () => { describe('n+ more label', () => {
beforeEach(() => { describe('when users count is rendered users', () => {
createComponent({ beforeEach(() => {
users: UsersMockHelper.createNumberRandomUsers(3), createComponent({
}); users: UsersMockHelper.createNumberRandomUsers(DEFAULT_RENDER_COUNT),
}); });
it('more than one user', () => {
expect(wrapper.findAll(AssigneeAvatarLink).length).toBe(3);
});
it('shows the "show-less" assignees label', done => {
const users = UsersMockHelper.createNumberRandomUsers(6);
createComponent({
users,
}); });
expect(wrapper.vm.$el.querySelectorAll('.user-item').length).toEqual(DEFAULT_RENDER_COUNT); it('does not show more label', () => {
expect(findMoreButton().exists()).toBe(false);
expect(wrapper.vm.$el.querySelector('.user-list-more')).not.toBe(null);
const usersLabelExpectation = users.length - DEFAULT_RENDER_COUNT;
expect(wrapper.vm.$el.querySelector('.user-list-more .btn-link').innerText.trim()).not.toBe(
`+${usersLabelExpectation} more`,
);
wrapper.vm.toggleShowLess();
Vue.nextTick(() => {
expect(wrapper.vm.$el.querySelector('.user-list-more .btn-link').innerText.trim()).toBe(
'- show less',
);
done();
}); });
}); });
it('shows the "show-less" when "n+ more " label is clicked', done => { describe('when more than rendered users', () => {
createComponent({ beforeEach(() => {
users: UsersMockHelper.createNumberRandomUsers(6), createComponent({
users: UsersMockHelper.createNumberRandomUsers(DEFAULT_RENDER_COUNT + 1),
});
}); });
wrapper.vm.$el.querySelector('.user-list-more .btn-link').click(); it('shows "+1 more" label', () => {
Vue.nextTick(() => { expect(findMoreButton().text()).toBe('+ 1 more');
expect(wrapper.vm.$el.querySelector('.user-list-more .btn-link').innerText.trim()).toBe(
'- show less',
);
done();
}); });
});
it('does not show n+ more label when less than render count', () => {
expect(findMoreButton().exists()).toBe(false);
});
});
describe('n+ more label', () => { it('shows truncated users', () => {
beforeEach(() => { expect(wrapper.findAll(AssigneeAvatarLink).length).toBe(DEFAULT_RENDER_COUNT);
createComponent({
users: UsersMockHelper.createNumberRandomUsers(DEFAULT_RENDER_COUNT + 1),
}); });
});
it('shows "+1 more" label', () => { describe('when more button is clicked', () => {
expect(findMoreButton().text()).toBe('+ 1 more'); beforeEach(() => {
expect(wrapper.findAll(AssigneeAvatarLink).length).toBe(DEFAULT_RENDER_COUNT); findMoreButton().trigger('click');
});
return wrapper.vm.$nextTick();
});
it('shows "show less" label', done => { it('shows "show less" label', () => {
findMoreButton().trigger('click'); expect(findMoreButton().text()).toBe('- show less');
});
Vue.nextTick(() => { it('shows all users', () => {
expect(findMoreButton().text()).toBe('- show less'); expect(wrapper.findAll(AssigneeAvatarLink).length).toBe(DEFAULT_RENDER_COUNT + 1);
expect(wrapper.findAll(AssigneeAvatarLink).length).toBe(DEFAULT_RENDER_COUNT + 1); });
done();
}); });
}); });
}); });
......
...@@ -224,7 +224,6 @@ describe('Assignee component', () => { ...@@ -224,7 +224,6 @@ describe('Assignee component', () => {
expect(userItems.length).toBe(3); expect(userItems.length).toBe(3);
expect(userItems[0].dataset.originalTitle).toBe(users[2].name); expect(userItems[0].dataset.originalTitle).toBe(users[2].name);
expect(userItems[0].dataset.originalTitle).not.toBe(users[0].name);
}); });
it('passes the sorted assignees to the collapsed-assignee-list', () => { it('passes the sorted assignees to the collapsed-assignee-list', () => {
...@@ -244,7 +243,6 @@ describe('Assignee component', () => { ...@@ -244,7 +243,6 @@ describe('Assignee component', () => {
const collapsedButton = component.$el.querySelector('.sidebar-collapsed-user button'); const collapsedButton = component.$el.querySelector('.sidebar-collapsed-user button');
expect(collapsedButton.innerText.trim()).toBe(users[2].name); expect(collapsedButton.innerText.trim()).toBe(users[2].name);
expect(collapsedButton.innerText.trim()).not.toBe(users[0].name);
}); });
}); });
}); });
export const userDataMock = {
avatar_url: 'mock_path',
id: 1,
name: 'Root',
state: 'active',
username: 'root',
web_url: '',
can_merge: true,
};
const RESPONSE_MAP = { const RESPONSE_MAP = {
GET: { GET: {
'/gitlab-org/gitlab-shell/issues/5.json': { '/gitlab-org/gitlab-shell/issues/5.json': {
......
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