Commit 1cb04e02 authored by Fatih Acet's avatar Fatih Acet

Merge branch 'migrate-diffs-specs' into 'master'

Migrate diffs specs to Jest + VTU

See merge request gitlab-org/gitlab!20652
parents d9019477 38e23eaf
import Vue from 'vue'; import { trimText } from 'helpers/text_helper';
import { mount, createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';
import CompareVersionsComponent from '~/diffs/components/compare_versions.vue'; import CompareVersionsComponent from '~/diffs/components/compare_versions.vue';
import Icon from '~/vue_shared/components/icon.vue';
import { createStore } from '~/mr_notes/stores'; import { createStore } from '~/mr_notes/stores';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper';
import diffsMockData from '../mock_data/merge_request_diffs'; import diffsMockData from '../mock_data/merge_request_diffs';
import getDiffWithCommit from '../mock_data/diff_with_commit'; import getDiffWithCommit from '../mock_data/diff_with_commit';
const localVue = createLocalVue();
localVue.use(Vuex);
describe('CompareVersions', () => { describe('CompareVersions', () => {
let vm; let wrapper;
const targetBranch = { branchName: 'tmp-wine-dev', versionIndex: -1 }; const targetBranch = { branchName: 'tmp-wine-dev', versionIndex: -1 };
beforeEach(() => { const createWrapper = props => {
const store = createStore(); const store = createStore();
store.state.diffs.addedLines = 10; store.state.diffs.addedLines = 10;
store.state.diffs.removedLines = 20; store.state.diffs.removedLines = 20;
store.state.diffs.diffFiles.push('test'); store.state.diffs.diffFiles.push('test');
vm = createComponentWithStore(Vue.extend(CompareVersionsComponent), store, { wrapper = mount(CompareVersionsComponent, {
sync: false,
attachToDocument: true,
localVue,
store,
propsData: {
mergeRequestDiffs: diffsMockData, mergeRequestDiffs: diffsMockData,
mergeRequestDiff: diffsMockData[0], mergeRequestDiff: diffsMockData[0],
targetBranch, targetBranch,
}).$mount(); ...props,
},
});
};
beforeEach(() => {
createWrapper();
});
afterEach(() => {
wrapper.destroy();
wrapper = null;
}); });
describe('template', () => { describe('template', () => {
it('should render Tree List toggle button with correct attribute values', () => { it('should render Tree List toggle button with correct attribute values', () => {
const treeListBtn = vm.$el.querySelector('.js-toggle-tree-list'); const treeListBtn = wrapper.find('.js-toggle-tree-list');
expect(treeListBtn).not.toBeNull(); expect(treeListBtn.exists()).toBe(true);
expect(treeListBtn.dataset.originalTitle).toBe('Hide file browser'); expect(treeListBtn.attributes('data-original-title')).toBe('Hide file browser');
expect(treeListBtn.querySelectorAll('svg use').length).not.toBe(0); expect(treeListBtn.findAll(Icon).length).not.toBe(0);
expect(treeListBtn.querySelector('svg use').getAttribute('xlink:href')).toContain( expect(treeListBtn.find(Icon).props('name')).toBe('collapse-left');
'#collapse-left',
);
}); });
it('should render comparison dropdowns with correct values', () => { it('should render comparison dropdowns with correct values', () => {
const sourceDropdown = vm.$el.querySelector('.mr-version-dropdown'); const sourceDropdown = wrapper.find('.mr-version-dropdown');
const targetDropdown = vm.$el.querySelector('.mr-version-compare-dropdown'); const targetDropdown = wrapper.find('.mr-version-compare-dropdown');
expect(sourceDropdown).not.toBeNull(); expect(sourceDropdown.exists()).toBe(true);
expect(targetDropdown).not.toBeNull(); expect(targetDropdown.exists()).toBe(true);
expect(sourceDropdown.querySelector('a span').innerHTML).toContain('latest version'); expect(sourceDropdown.find('a span').html()).toContain('latest version');
expect(targetDropdown.querySelector('a span').innerHTML).toContain(targetBranch.branchName); expect(targetDropdown.find('a span').html()).toContain(targetBranch.branchName);
}); });
it('should not render comparison dropdowns if no mergeRequestDiffs are specified', () => { it('should not render comparison dropdowns if no mergeRequestDiffs are specified', () => {
vm.mergeRequestDiffs = []; createWrapper({ mergeRequestDiffs: [] });
vm.$nextTick(() => { const sourceDropdown = wrapper.find('.mr-version-dropdown');
const sourceDropdown = vm.$el.querySelector('.mr-version-dropdown'); const targetDropdown = wrapper.find('.mr-version-compare-dropdown');
const targetDropdown = vm.$el.querySelector('.mr-version-compare-dropdown');
expect(sourceDropdown).toBeNull(); expect(sourceDropdown.exists()).toBe(false);
expect(targetDropdown).toBeNull(); expect(targetDropdown.exists()).toBe(false);
});
}); });
it('should render view types buttons with correct values', () => { it('should render view types buttons with correct values', () => {
const inlineBtn = vm.$el.querySelector('#inline-diff-btn'); const inlineBtn = wrapper.find('#inline-diff-btn');
const parallelBtn = vm.$el.querySelector('#parallel-diff-btn'); const parallelBtn = wrapper.find('#parallel-diff-btn');
expect(inlineBtn).not.toBeNull(); expect(inlineBtn.exists()).toBe(true);
expect(parallelBtn).not.toBeNull(); expect(parallelBtn.exists()).toBe(true);
expect(inlineBtn.dataset.viewType).toEqual('inline'); expect(inlineBtn.attributes('data-view-type')).toEqual('inline');
expect(parallelBtn.dataset.viewType).toEqual('parallel'); expect(parallelBtn.attributes('data-view-type')).toEqual('parallel');
expect(inlineBtn.innerHTML).toContain('Inline'); expect(inlineBtn.html()).toContain('Inline');
expect(parallelBtn.innerHTML).toContain('Side-by-side'); expect(parallelBtn.html()).toContain('Side-by-side');
}); });
it('adds container-limiting classes when showFileTree is false with inline diffs', () => { it('adds container-limiting classes when showFileTree is false with inline diffs', () => {
vm.isLimitedContainer = true; createWrapper({ isLimitedContainer: true });
vm.$nextTick(() => { const limitedContainer = wrapper.find('.container-limited.limit-container-width');
const limitedContainer = vm.$el.querySelector('.container-limited.limit-container-width');
expect(limitedContainer).not.toBeNull(); expect(limitedContainer.exists()).toBe(true);
});
}); });
it('does not add container-limiting classes when showFileTree is false with inline diffs', () => { it('does not add container-limiting classes when showFileTree is false with inline diffs', () => {
vm.isLimitedContainer = false; createWrapper({ isLimitedContainer: false });
vm.$nextTick(() => { const limitedContainer = wrapper.find('.container-limited.limit-container-width');
const limitedContainer = vm.$el.querySelector('.container-limited.limit-container-width');
expect(limitedContainer).toBeNull(); expect(limitedContainer.exists()).toBe(false);
});
}); });
}); });
describe('setInlineDiffViewType', () => { describe('setInlineDiffViewType', () => {
it('should persist the view type in the url', () => { it('should persist the view type in the url', () => {
const viewTypeBtn = vm.$el.querySelector('#inline-diff-btn'); const viewTypeBtn = wrapper.find('#inline-diff-btn');
viewTypeBtn.click(); viewTypeBtn.trigger('click');
expect(window.location.toString()).toContain('?view=inline'); expect(window.location.toString()).toContain('?view=inline');
}); });
...@@ -101,8 +114,8 @@ describe('CompareVersions', () => { ...@@ -101,8 +114,8 @@ describe('CompareVersions', () => {
describe('setParallelDiffViewType', () => { describe('setParallelDiffViewType', () => {
it('should persist the view type in the url', () => { it('should persist the view type in the url', () => {
const viewTypeBtn = vm.$el.querySelector('#parallel-diff-btn'); const viewTypeBtn = wrapper.find('#parallel-diff-btn');
viewTypeBtn.click(); viewTypeBtn.trigger('click');
expect(window.location.toString()).toContain('?view=parallel'); expect(window.location.toString()).toContain('?view=parallel');
}); });
...@@ -110,7 +123,7 @@ describe('CompareVersions', () => { ...@@ -110,7 +123,7 @@ describe('CompareVersions', () => {
describe('comparableDiffs', () => { describe('comparableDiffs', () => {
it('should not contain the first item in the mergeRequestDiffs property', () => { it('should not contain the first item in the mergeRequestDiffs property', () => {
const { comparableDiffs } = vm; const { comparableDiffs } = wrapper.vm;
const comparableDiffsMock = diffsMockData.slice(1); const comparableDiffsMock = diffsMockData.slice(1);
expect(comparableDiffs).toEqual(comparableDiffsMock); expect(comparableDiffs).toEqual(comparableDiffsMock);
...@@ -119,27 +132,25 @@ describe('CompareVersions', () => { ...@@ -119,27 +132,25 @@ describe('CompareVersions', () => {
describe('baseVersionPath', () => { describe('baseVersionPath', () => {
it('should be set correctly from mergeRequestDiff', () => { it('should be set correctly from mergeRequestDiff', () => {
expect(vm.baseVersionPath).toEqual(vm.mergeRequestDiff.base_version_path); expect(wrapper.vm.baseVersionPath).toEqual(wrapper.vm.mergeRequestDiff.base_version_path);
}); });
}); });
describe('commit', () => { describe('commit', () => {
beforeEach(done => { beforeEach(done => {
vm.$store.state.diffs.commit = getDiffWithCommit().commit; wrapper.vm.$store.state.diffs.commit = getDiffWithCommit().commit;
vm.mergeRequestDiffs = []; wrapper.mergeRequestDiffs = [];
vm.$nextTick(done); wrapper.vm.$nextTick(done);
}); });
it('renders latest version button', () => { it('renders latest version button', () => {
expect(vm.$el.querySelector('.js-latest-version').textContent.trim()).toBe( expect(trimText(wrapper.find('.js-latest-version').text())).toBe('Show latest version');
'Show latest version',
);
}); });
it('renders short commit ID', () => { it('renders short commit ID', () => {
expect(vm.$el.textContent).toContain('Viewing commit'); expect(wrapper.text()).toContain('Viewing commit');
expect(vm.$el.textContent).toContain(vm.commit.short_id); expect(wrapper.text()).toContain(wrapper.vm.commit.short_id);
}); });
}); });
}); });
const FIXTURE = 'merge_request_diffs/with_commit.json';
preloadFixtures(FIXTURE);
export default function getDiffWithCommit() {
return getJSONFixture(FIXTURE);
}
export default [
{
base_version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=37',
version_index: 4,
created_at: '2018-10-23T11:49:16.611Z',
commits_count: 4,
latest: true,
short_commit_sha: 'de7a8f7f',
version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=37',
compare_path:
'/gnuwget/wget2/merge_requests/6/diffs?diff_id=37&start_sha=de7a8f7f20c3ea2e0bef3ba01cfd41c21f6b4995',
},
{
base_version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=36',
version_index: 3,
created_at: '2018-10-23T11:46:40.617Z',
commits_count: 3,
latest: false,
short_commit_sha: 'e78fc18f',
version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=36',
compare_path:
'/gnuwget/wget2/merge_requests/6/diffs?diff_id=37&start_sha=e78fc18fa37acb2185c59ca94d4a964464feb50e',
},
{
base_version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=35',
version_index: 2,
created_at: '2018-10-04T09:57:39.648Z',
commits_count: 2,
latest: false,
short_commit_sha: '48da7e7e',
version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=35',
compare_path:
'/gnuwget/wget2/merge_requests/6/diffs?diff_id=37&start_sha=48da7e7e9a99d41c852578bd9cb541ca4d864b3e',
},
{
base_version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=20',
version_index: 1,
created_at: '2018-09-25T20:30:39.493Z',
commits_count: 1,
latest: false,
short_commit_sha: '47bac2ed',
version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=20',
compare_path:
'/gnuwget/wget2/merge_requests/6/diffs?diff_id=37&start_sha=47bac2ed972c5bee344c1cea159a22cd7f711dc0',
},
];
const FIXTURE = 'merge_request_diffs/with_commit.json'; // No new code should be added to this file. Instead, modify the
// file this one re-exports from. For more detail about why, see:
// https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/31349
preloadFixtures(FIXTURE); import getDiffWithCommit from '../../../frontend/diffs/mock_data/diff_with_commit';
export default function getDiffWithCommit() { export default getDiffWithCommit;
return getJSONFixture(FIXTURE);
}
export default [ // No new code should be added to this file. Instead, modify the
{ // file this one re-exports from. For more detail about why, see:
base_version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=37', // https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/31349
version_index: 4,
created_at: '2018-10-23T11:49:16.611Z', import diffsMockData from '../../../frontend/diffs/mock_data/merge_request_diffs';
commits_count: 4,
latest: true, export default diffsMockData;
short_commit_sha: 'de7a8f7f',
version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=37',
compare_path:
'/gnuwget/wget2/merge_requests/6/diffs?diff_id=37&start_sha=de7a8f7f20c3ea2e0bef3ba01cfd41c21f6b4995',
},
{
base_version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=36',
version_index: 3,
created_at: '2018-10-23T11:46:40.617Z',
commits_count: 3,
latest: false,
short_commit_sha: 'e78fc18f',
version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=36',
compare_path:
'/gnuwget/wget2/merge_requests/6/diffs?diff_id=37&start_sha=e78fc18fa37acb2185c59ca94d4a964464feb50e',
},
{
base_version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=35',
version_index: 2,
created_at: '2018-10-04T09:57:39.648Z',
commits_count: 2,
latest: false,
short_commit_sha: '48da7e7e',
version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=35',
compare_path:
'/gnuwget/wget2/merge_requests/6/diffs?diff_id=37&start_sha=48da7e7e9a99d41c852578bd9cb541ca4d864b3e',
},
{
base_version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=20',
version_index: 1,
created_at: '2018-09-25T20:30:39.493Z',
commits_count: 1,
latest: false,
short_commit_sha: '47bac2ed',
version_path: '/gnuwget/wget2/merge_requests/6/diffs?diff_id=20',
compare_path:
'/gnuwget/wget2/merge_requests/6/diffs?diff_id=37&start_sha=47bac2ed972c5bee344c1cea159a22cd7f711dc0',
},
];
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