Commit ef1af0b0 authored by Olena Horal-Koretska's avatar Olena Horal-Koretska

Merge branch 'vs-remove-deprecated-mount-component-from-mr-widget-header' into 'master'

Migrate mr_widget_header_spec to VTU

See merge request gitlab-org/gitlab!56474
parents 02ee1271 86b84d57
import Vue from 'vue'; import { shallowMount } from '@vue/test-utils';
import mountComponent from 'helpers/vue_mount_component_helper'; import { nextTick } from 'vue';
import headerComponent from '~/vue_merge_request_widget/components/mr_widget_header.vue'; import Header from '~/vue_merge_request_widget/components/mr_widget_header.vue';
describe('MRWidgetHeader', () => { describe('MRWidgetHeader', () => {
let vm; let wrapper;
let Component;
beforeEach(() => { const createComponent = (propsData = {}) => {
Component = Vue.extend(headerComponent); wrapper = shallowMount(Header, {
}); propsData,
});
};
afterEach(() => { afterEach(() => {
vm.$destroy(); wrapper.destroy();
gon.relative_url_root = ''; gon.relative_url_root = '';
}); });
const expectDownloadDropdownItems = () => { const expectDownloadDropdownItems = () => {
const downloadEmailPatchesEl = vm.$el.querySelector('.js-download-email-patches'); const downloadEmailPatchesEl = wrapper.find('.js-download-email-patches');
const downloadPlainDiffEl = vm.$el.querySelector('.js-download-plain-diff'); const downloadPlainDiffEl = wrapper.find('.js-download-plain-diff');
expect(downloadEmailPatchesEl.innerText.trim()).toEqual('Email patches'); expect(downloadEmailPatchesEl.text().trim()).toBe('Email patches');
expect(downloadEmailPatchesEl.querySelector('a').getAttribute('href')).toEqual( expect(downloadEmailPatchesEl.attributes('href')).toBe('/mr/email-patches');
'/mr/email-patches', expect(downloadPlainDiffEl.text().trim()).toBe('Plain diff');
); expect(downloadPlainDiffEl.attributes('href')).toBe('/mr/plainDiffPath');
expect(downloadPlainDiffEl.innerText.trim()).toEqual('Plain diff');
expect(downloadPlainDiffEl.querySelector('a').getAttribute('href')).toEqual(
'/mr/plainDiffPath',
);
}; };
describe('computed', () => { describe('computed', () => {
describe('shouldShowCommitsBehindText', () => { describe('shouldShowCommitsBehindText', () => {
it('return true when there are divergedCommitsCount', () => { it('return true when there are divergedCommitsCount', () => {
vm = mountComponent(Component, { createComponent({
mr: { mr: {
divergedCommitsCount: 12, divergedCommitsCount: 12,
sourceBranch: 'mr-widget-refactor', sourceBranch: 'mr-widget-refactor',
...@@ -42,11 +39,11 @@ describe('MRWidgetHeader', () => { ...@@ -42,11 +39,11 @@ describe('MRWidgetHeader', () => {
}, },
}); });
expect(vm.shouldShowCommitsBehindText).toEqual(true); expect(wrapper.vm.shouldShowCommitsBehindText).toBe(true);
}); });
it('returns false where there are no divergedComits count', () => { it('returns false where there are no divergedComits count', () => {
vm = mountComponent(Component, { createComponent({
mr: { mr: {
divergedCommitsCount: 0, divergedCommitsCount: 0,
sourceBranch: 'mr-widget-refactor', sourceBranch: 'mr-widget-refactor',
...@@ -56,13 +53,13 @@ describe('MRWidgetHeader', () => { ...@@ -56,13 +53,13 @@ describe('MRWidgetHeader', () => {
}, },
}); });
expect(vm.shouldShowCommitsBehindText).toEqual(false); expect(wrapper.vm.shouldShowCommitsBehindText).toBe(false);
}); });
}); });
describe('commitsBehindText', () => { describe('commitsBehindText', () => {
it('returns singular when there is one commit', () => { it('returns singular when there is one commit', () => {
vm = mountComponent(Component, { createComponent({
mr: { mr: {
divergedCommitsCount: 1, divergedCommitsCount: 1,
sourceBranch: 'mr-widget-refactor', sourceBranch: 'mr-widget-refactor',
...@@ -73,13 +70,13 @@ describe('MRWidgetHeader', () => { ...@@ -73,13 +70,13 @@ describe('MRWidgetHeader', () => {
}, },
}); });
expect(vm.commitsBehindText).toEqual( expect(wrapper.vm.commitsBehindText).toBe(
'The source branch is <a href="/foo/bar/master">1 commit behind</a> the target branch', 'The source branch is <a href="/foo/bar/master">1 commit behind</a> the target branch',
); );
}); });
it('returns plural when there is more than one commit', () => { it('returns plural when there is more than one commit', () => {
vm = mountComponent(Component, { createComponent({
mr: { mr: {
divergedCommitsCount: 2, divergedCommitsCount: 2,
sourceBranch: 'mr-widget-refactor', sourceBranch: 'mr-widget-refactor',
...@@ -90,7 +87,7 @@ describe('MRWidgetHeader', () => { ...@@ -90,7 +87,7 @@ describe('MRWidgetHeader', () => {
}, },
}); });
expect(vm.commitsBehindText).toEqual( expect(wrapper.vm.commitsBehindText).toBe(
'The source branch is <a href="/foo/bar/master">2 commits behind</a> the target branch', 'The source branch is <a href="/foo/bar/master">2 commits behind</a> the target branch',
); );
}); });
...@@ -100,7 +97,7 @@ describe('MRWidgetHeader', () => { ...@@ -100,7 +97,7 @@ describe('MRWidgetHeader', () => {
describe('template', () => { describe('template', () => {
describe('common elements', () => { describe('common elements', () => {
beforeEach(() => { beforeEach(() => {
vm = mountComponent(Component, { createComponent({
mr: { mr: {
divergedCommitsCount: 12, divergedCommitsCount: 12,
sourceBranch: 'mr-widget-refactor', sourceBranch: 'mr-widget-refactor',
...@@ -118,17 +115,17 @@ describe('MRWidgetHeader', () => { ...@@ -118,17 +115,17 @@ describe('MRWidgetHeader', () => {
}); });
it('renders source branch link', () => { it('renders source branch link', () => {
expect(vm.$el.querySelector('.js-source-branch').innerHTML).toEqual( expect(wrapper.find('.js-source-branch').html()).toContain(
'<a href="/foo/bar/mr-widget-refactor">mr-widget-refactor</a>', '<a href="/foo/bar/mr-widget-refactor">mr-widget-refactor</a>',
); );
}); });
it('renders clipboard button', () => { it('renders clipboard button', () => {
expect(vm.$el.querySelector('[data-testid="mr-widget-copy-clipboard"]')).not.toEqual(null); expect(wrapper.find('[data-testid="mr-widget-copy-clipboard"]')).not.toBe(null);
}); });
it('renders target branch', () => { it('renders target branch', () => {
expect(vm.$el.querySelector('.js-target-branch').textContent.trim()).toEqual('master'); expect(wrapper.find('.js-target-branch').text().trim()).toBe('master');
}); });
}); });
...@@ -151,71 +148,68 @@ describe('MRWidgetHeader', () => { ...@@ -151,71 +148,68 @@ describe('MRWidgetHeader', () => {
targetProjectFullPath: 'gitlab-org/gitlab-ce', targetProjectFullPath: 'gitlab-org/gitlab-ce',
}; };
afterEach(() => {
vm.$destroy();
});
beforeEach(() => { beforeEach(() => {
vm = mountComponent(Component, { createComponent({
mr: { ...mrDefaultOptions }, mr: { ...mrDefaultOptions },
}); });
}); });
it('renders checkout branch button with modal trigger', () => { it('renders checkout branch button with modal trigger', () => {
const button = vm.$el.querySelector('.js-check-out-branch'); const button = wrapper.find('.js-check-out-branch');
expect(button.textContent.trim()).toBe('Check out branch'); expect(button.text().trim()).toBe('Check out branch');
}); });
it('renders web ide button', () => { it('renders web ide button', async () => {
const button = vm.$el.querySelector('.js-web-ide'); const button = wrapper.find('.js-web-ide');
expect(button.textContent.trim()).toEqual('Open in Web IDE'); await nextTick();
expect(button.classList.contains('disabled')).toBe(false);
expect(button.getAttribute('href')).toEqual( expect(button.text().trim()).toBe('Open in Web IDE');
expect(button.classes('disabled')).toBe(false);
expect(button.attributes('href')).toBe(
'/-/ide/project/root/gitlab-ce/merge_requests/1?target_project=gitlab-org%2Fgitlab-ce', '/-/ide/project/root/gitlab-ce/merge_requests/1?target_project=gitlab-org%2Fgitlab-ce',
); );
}); });
it('renders web ide button in disabled state with no href', () => { it('renders web ide button in disabled state with no href', async () => {
const mr = { ...mrDefaultOptions, canPushToSourceBranch: false }; const mr = { ...mrDefaultOptions, canPushToSourceBranch: false };
vm = mountComponent(Component, { mr }); createComponent({ mr });
await nextTick();
const link = vm.$el.querySelector('.js-web-ide'); const link = wrapper.find('.js-web-ide');
expect(link.classList.contains('disabled')).toBe(true); expect(link.attributes('disabled')).toBe('true');
expect(link.getAttribute('href')).toBeNull(); expect(link.attributes('href')).toBeUndefined();
}); });
it('renders web ide button with blank query string if target & source project branch', (done) => { it('renders web ide button with blank query string if target & source project branch', async () => {
vm.mr.targetProjectFullPath = 'root/gitlab-ce'; createComponent({ mr: { ...mrDefaultOptions, targetProjectFullPath: 'root/gitlab-ce' } });
vm.$nextTick(() => { await nextTick();
const button = vm.$el.querySelector('.js-web-ide');
expect(button.textContent.trim()).toEqual('Open in Web IDE'); const button = wrapper.find('.js-web-ide');
expect(button.getAttribute('href')).toEqual(
'/-/ide/project/root/gitlab-ce/merge_requests/1?target_project=',
);
done(); expect(button.text().trim()).toBe('Open in Web IDE');
}); expect(button.attributes('href')).toBe(
'/-/ide/project/root/gitlab-ce/merge_requests/1?target_project=',
);
}); });
it('renders web ide button with relative URL', (done) => { it('renders web ide button with relative URL', async () => {
gon.relative_url_root = '/gitlab'; gon.relative_url_root = '/gitlab';
vm.mr.iid = 2;
vm.$nextTick(() => { createComponent({ mr: { ...mrDefaultOptions, iid: 2 } });
const button = vm.$el.querySelector('.js-web-ide');
expect(button.textContent.trim()).toEqual('Open in Web IDE'); await nextTick();
expect(button.getAttribute('href')).toEqual(
'/gitlab/-/ide/project/root/gitlab-ce/merge_requests/2?target_project=gitlab-org%2Fgitlab-ce',
);
done(); const button = wrapper.find('.js-web-ide');
});
expect(button.text().trim()).toBe('Open in Web IDE');
expect(button.attributes('href')).toBe(
'/gitlab/-/ide/project/root/gitlab-ce/merge_requests/2?target_project=gitlab-org%2Fgitlab-ce',
);
}); });
it('renders download dropdown with links', () => { it('renders download dropdown with links', () => {
...@@ -225,7 +219,7 @@ describe('MRWidgetHeader', () => { ...@@ -225,7 +219,7 @@ describe('MRWidgetHeader', () => {
describe('with a closed merge request', () => { describe('with a closed merge request', () => {
beforeEach(() => { beforeEach(() => {
vm = mountComponent(Component, { createComponent({
mr: { mr: {
divergedCommitsCount: 12, divergedCommitsCount: 12,
sourceBranch: 'mr-widget-refactor', sourceBranch: 'mr-widget-refactor',
...@@ -243,9 +237,9 @@ describe('MRWidgetHeader', () => { ...@@ -243,9 +237,9 @@ describe('MRWidgetHeader', () => {
}); });
it('does not render checkout branch button with modal trigger', () => { it('does not render checkout branch button with modal trigger', () => {
const button = vm.$el.querySelector('.js-check-out-branch'); const button = wrapper.find('.js-check-out-branch');
expect(button).toEqual(null); expect(button.exists()).toBe(false);
}); });
it('renders download dropdown with links', () => { it('renders download dropdown with links', () => {
...@@ -255,7 +249,7 @@ describe('MRWidgetHeader', () => { ...@@ -255,7 +249,7 @@ describe('MRWidgetHeader', () => {
describe('without diverged commits', () => { describe('without diverged commits', () => {
beforeEach(() => { beforeEach(() => {
vm = mountComponent(Component, { createComponent({
mr: { mr: {
divergedCommitsCount: 0, divergedCommitsCount: 0,
sourceBranch: 'mr-widget-refactor', sourceBranch: 'mr-widget-refactor',
...@@ -273,13 +267,13 @@ describe('MRWidgetHeader', () => { ...@@ -273,13 +267,13 @@ describe('MRWidgetHeader', () => {
}); });
it('does not render diverged commits info', () => { it('does not render diverged commits info', () => {
expect(vm.$el.querySelector('.diverged-commits-count')).toEqual(null); expect(wrapper.find('.diverged-commits-count').exists()).toBe(false);
}); });
}); });
describe('with diverged commits', () => { describe('with diverged commits', () => {
beforeEach(() => { beforeEach(() => {
vm = mountComponent(Component, { createComponent({
mr: { mr: {
divergedCommitsCount: 12, divergedCommitsCount: 12,
sourceBranch: 'mr-widget-refactor', sourceBranch: 'mr-widget-refactor',
...@@ -297,17 +291,13 @@ describe('MRWidgetHeader', () => { ...@@ -297,17 +291,13 @@ describe('MRWidgetHeader', () => {
}); });
it('renders diverged commits info', () => { it('renders diverged commits info', () => {
expect(vm.$el.querySelector('.diverged-commits-count').textContent).toEqual( expect(wrapper.find('.diverged-commits-count').text().trim()).toBe(
'The source branch is 12 commits behind the target branch', 'The source branch is 12 commits behind the target branch',
); );
expect(vm.$el.querySelector('.diverged-commits-count a').textContent).toEqual( expect(wrapper.find('.diverged-commits-count a').text().trim()).toBe('12 commits behind');
'12 commits behind', expect(wrapper.find('.diverged-commits-count a').attributes('href')).toBe(
); wrapper.vm.mr.targetBranchPath,
expect(vm.$el.querySelector('.diverged-commits-count a')).toHaveAttr(
'href',
vm.mr.targetBranchPath,
); );
}); });
}); });
......
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