gitlab-org--gitlab-foss/spec/javascripts/repo/components/repo_commit_section_spec.js

160 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-07-27 10:49:25 -04:00
import Vue from 'vue';
import repoCommitSection from '~/repo/components/repo_commit_section.vue';
import RepoStore from '~/repo/stores/repo_store';
import RepoService from '~/repo/services/repo_service';
2017-07-27 10:49:25 -04:00
describe('RepoCommitSection', () => {
const branch = 'master';
const projectUrl = 'projectUrl';
2017-08-15 15:53:41 -04:00
const changedFiles = [{
2017-07-27 10:49:25 -04:00
id: 0,
changed: true,
url: `/namespace/${projectUrl}/blob/${branch}/dir/file0.ext`,
2017-08-15 15:53:41 -04:00
path: 'dir/file0.ext',
2017-07-27 10:49:25 -04:00
newContent: 'a',
}, {
id: 1,
changed: true,
url: `/namespace/${projectUrl}/blob/${branch}/dir/file1.ext`,
2017-08-15 15:53:41 -04:00
path: 'dir/file1.ext',
2017-07-27 10:49:25 -04:00
newContent: 'b',
2017-08-15 15:53:41 -04:00
}];
const openedFiles = changedFiles.concat([{
2017-07-27 10:49:25 -04:00
id: 2,
url: `/namespace/${projectUrl}/blob/${branch}/dir/file2.ext`,
2017-08-15 15:53:41 -04:00
path: 'dir/file2.ext',
2017-07-27 10:49:25 -04:00
changed: false,
2017-08-15 15:53:41 -04:00
}]);
2017-07-27 10:49:25 -04:00
RepoStore.projectUrl = projectUrl;
2017-08-15 15:53:41 -04:00
function createComponent(el) {
2017-07-27 10:49:25 -04:00
const RepoCommitSection = Vue.extend(repoCommitSection);
2017-08-15 15:53:41 -04:00
return new RepoCommitSection().$mount(el);
2017-07-27 10:49:25 -04:00
}
it('renders a commit section', () => {
RepoStore.isCommitable = true;
2017-08-15 15:53:41 -04:00
RepoStore.currentBranch = branch;
RepoStore.targetBranch = branch;
2017-07-27 10:49:25 -04:00
RepoStore.openedFiles = openedFiles;
2017-07-27 10:49:25 -04:00
const vm = createComponent();
2017-08-15 15:53:41 -04:00
const changedFileElements = [...vm.$el.querySelectorAll('.changed-files > li')];
2017-07-27 10:49:25 -04:00
const commitMessage = vm.$el.querySelector('#commit-message');
2017-08-15 15:53:41 -04:00
const submitCommit = vm.$refs.submitCommit;
const targetBranch = vm.$el.querySelector('.target-branch');
2017-07-27 10:49:25 -04:00
expect(vm.$el.querySelector(':scope > form')).toBeTruthy();
2017-08-15 15:53:41 -04:00
expect(vm.$el.querySelector('.staged-files').textContent.trim()).toEqual('Staged files (2)');
expect(changedFileElements.length).toEqual(2);
2017-07-27 10:49:25 -04:00
2017-08-15 15:53:41 -04:00
changedFileElements.forEach((changedFile, i) => {
expect(changedFile.textContent.trim()).toEqual(changedFiles[i].path);
2017-07-27 10:49:25 -04:00
});
expect(commitMessage.tagName).toEqual('TEXTAREA');
expect(commitMessage.name).toEqual('commit-message');
expect(submitCommit.type).toEqual('submit');
expect(submitCommit.disabled).toBeTruthy();
expect(submitCommit.querySelector('.fa-spinner.fa-spin')).toBeFalsy();
2017-08-15 15:53:41 -04:00
expect(vm.$el.querySelector('.commit-summary').textContent.trim()).toEqual('Commit 2 files');
expect(targetBranch.querySelector(':scope > label').textContent.trim()).toEqual('Target branch');
expect(targetBranch.querySelector('.help-block').textContent.trim()).toEqual(branch);
2017-07-27 10:49:25 -04:00
});
it('does not render if not isCommitable', () => {
RepoStore.isCommitable = false;
RepoStore.openedFiles = [{
id: 0,
changed: true,
}];
const vm = createComponent();
expect(vm.$el.innerHTML).toBeFalsy();
});
it('does not render if no changedFiles', () => {
RepoStore.isCommitable = true;
RepoStore.openedFiles = [];
const vm = createComponent();
expect(vm.$el.innerHTML).toBeFalsy();
});
it('shows commit submit and summary if commitMessage and spinner if submitCommitsLoading', (done) => {
const projectId = 'projectId';
const commitMessage = 'commitMessage';
RepoStore.isCommitable = true;
2017-08-15 15:53:41 -04:00
RepoStore.currentBranch = branch;
RepoStore.targetBranch = branch;
2017-07-27 10:49:25 -04:00
RepoStore.openedFiles = openedFiles;
RepoStore.projectId = projectId;
2017-08-15 15:53:41 -04:00
// We need to append to body to get form `submit` events working
// Otherwise we run into, "Form submission canceled because the form is not connected"
// See https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#form-submission-algorithm
const el = document.createElement('div');
document.body.appendChild(el);
2017-08-15 15:53:41 -04:00
const vm = createComponent(el);
2017-07-27 10:49:25 -04:00
const commitMessageEl = vm.$el.querySelector('#commit-message');
2017-08-15 15:53:41 -04:00
const submitCommit = vm.$refs.submitCommit;
2017-07-27 10:49:25 -04:00
vm.commitMessage = commitMessage;
Vue.nextTick(() => {
expect(commitMessageEl.value).toBe(commitMessage);
expect(submitCommit.disabled).toBeFalsy();
spyOn(vm, 'makeCommit').and.callThrough();
spyOn(RepoService, 'commitFiles').and.callFake(() => Promise.resolve());
2017-07-27 10:49:25 -04:00
submitCommit.click();
Vue.nextTick(() => {
expect(vm.makeCommit).toHaveBeenCalled();
expect(submitCommit.querySelector('.fa-spinner.fa-spin')).toBeTruthy();
const args = RepoService.commitFiles.calls.allArgs()[0];
const { commit_message, actions, branch: payloadBranch } = args[0];
2017-07-27 10:49:25 -04:00
expect(commit_message).toBe(commitMessage);
expect(actions.length).toEqual(2);
expect(payloadBranch).toEqual(branch);
2017-07-27 10:49:25 -04:00
expect(actions[0].action).toEqual('update');
expect(actions[1].action).toEqual('update');
expect(actions[0].content).toEqual(openedFiles[0].newContent);
expect(actions[1].content).toEqual(openedFiles[1].newContent);
2017-08-15 15:53:41 -04:00
expect(actions[0].file_path).toEqual(openedFiles[0].path);
expect(actions[1].file_path).toEqual(openedFiles[1].path);
2017-07-27 10:49:25 -04:00
done();
});
});
});
2017-08-07 08:20:51 -04:00
describe('methods', () => {
describe('resetCommitState', () => {
it('should reset store vars and scroll to top', () => {
const vm = {
submitCommitsLoading: true,
changedFiles: new Array(10),
commitMessage: 'commitMessage',
editMode: true,
};
repoCommitSection.methods.resetCommitState.call(vm);
expect(vm.submitCommitsLoading).toEqual(false);
expect(vm.changedFiles).toEqual([]);
expect(vm.commitMessage).toEqual('');
expect(vm.editMode).toEqual(false);
});
});
});
2017-07-27 10:49:25 -04:00
});