Commit 6752a3bb authored by Fatih Acet's avatar Fatih Acet

Merge branch 'thenable-ajax-api-calls' into 'master'

Thenable ajax api calls

See merge request !13427
parents 610155e0 9eb28bbf
...@@ -96,18 +96,17 @@ const Api = { ...@@ -96,18 +96,17 @@ const Api = {
.done(projects => callback(projects)); .done(projects => callback(projects));
}, },
commitMultiple(id, data, callback) { commitMultiple(id, data) {
// see https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
const url = Api.buildUrl(Api.commitPath) const url = Api.buildUrl(Api.commitPath)
.replace(':id', id); .replace(':id', id);
return $.ajax({ return this.wrapAjaxCall({
url, url,
type: 'POST', type: 'POST',
contentType: 'application/json; charset=utf-8', contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data), data: JSON.stringify(data),
dataType: 'json', dataType: 'json',
}) });
.done(commitData => callback(commitData))
.fail(message => callback(message.responseJSON));
}, },
// Return text for a specific license // Return text for a specific license
......
...@@ -42,7 +42,9 @@ export default { ...@@ -42,7 +42,9 @@ export default {
actions, actions,
}; };
Store.submitCommitsLoading = true; Store.submitCommitsLoading = true;
Service.commitFiles(payload, this.resetCommitState); Service.commitFiles(payload)
.then(this.resetCommitState)
.catch(() => Flash('An error occured while committing your changes'));
}, },
resetCommitState() { resetCommitState() {
......
...@@ -65,15 +65,17 @@ const RepoService = { ...@@ -65,15 +65,17 @@ const RepoService = {
return urlArray.join('/'); return urlArray.join('/');
}, },
commitFiles(payload, cb) { commitFiles(payload) {
Api.commitMultiple(Store.projectId, payload, (data) => { return Api.commitMultiple(Store.projectId, payload)
if (data.short_id && data.stats) { .then(this.commitFlash);
Flash(`Your changes have been committed. Commit ${data.short_id} with ${data.stats.additions} additions, ${data.stats.deletions} deletions.`, 'notice'); },
} else {
Flash(data.message); commitFlash(data) {
} if (data.short_id && data.stats) {
cb(); window.Flash(`Your changes have been committed. Commit ${data.short_id} with ${data.stats.additions} additions, ${data.stats.deletions} deletions.`, 'notice');
}); } else {
window.Flash(data.message);
}
}, },
}; };
......
import Vue from 'vue'; import Vue from 'vue';
import repoCommitSection from '~/repo/components/repo_commit_section.vue'; import repoCommitSection from '~/repo/components/repo_commit_section.vue';
import RepoStore from '~/repo/stores/repo_store'; import RepoStore from '~/repo/stores/repo_store';
import Api from '~/api'; import RepoService from '~/repo/services/repo_service';
describe('RepoCommitSection', () => { describe('RepoCommitSection', () => {
const branch = 'master'; const branch = 'master';
...@@ -111,7 +111,7 @@ describe('RepoCommitSection', () => { ...@@ -111,7 +111,7 @@ describe('RepoCommitSection', () => {
expect(submitCommit.disabled).toBeFalsy(); expect(submitCommit.disabled).toBeFalsy();
spyOn(vm, 'makeCommit').and.callThrough(); spyOn(vm, 'makeCommit').and.callThrough();
spyOn(Api, 'commitMultiple'); spyOn(RepoService, 'commitFiles').and.callFake(() => Promise.resolve());
submitCommit.click(); submitCommit.click();
...@@ -119,10 +119,9 @@ describe('RepoCommitSection', () => { ...@@ -119,10 +119,9 @@ describe('RepoCommitSection', () => {
expect(vm.makeCommit).toHaveBeenCalled(); expect(vm.makeCommit).toHaveBeenCalled();
expect(submitCommit.querySelector('.fa-spinner.fa-spin')).toBeTruthy(); expect(submitCommit.querySelector('.fa-spinner.fa-spin')).toBeTruthy();
const args = Api.commitMultiple.calls.allArgs()[0]; const args = RepoService.commitFiles.calls.allArgs()[0];
const { commit_message, actions, branch: payloadBranch } = args[1]; const { commit_message, actions, branch: payloadBranch } = args[0];
expect(args[0]).toBe(projectId);
expect(commit_message).toBe(commitMessage); expect(commit_message).toBe(commitMessage);
expect(actions.length).toEqual(2); expect(actions.length).toEqual(2);
expect(payloadBranch).toEqual(branch); expect(payloadBranch).toEqual(branch);
......
import axios from 'axios'; import axios from 'axios';
import RepoService from '~/repo/services/repo_service'; import RepoService from '~/repo/services/repo_service';
import RepoStore from '~/repo/stores/repo_store';
import Api from '~/api';
describe('RepoService', () => { describe('RepoService', () => {
it('has default json format param', () => { it('has default json format param', () => {
...@@ -118,4 +120,52 @@ describe('RepoService', () => { ...@@ -118,4 +120,52 @@ describe('RepoService', () => {
}).catch(done.fail); }).catch(done.fail);
}); });
}); });
describe('commitFiles', () => {
it('calls commitMultiple and .then commitFlash', (done) => {
const projectId = 'projectId';
const payload = {};
RepoStore.projectId = projectId;
spyOn(Api, 'commitMultiple').and.returnValue(Promise.resolve());
spyOn(RepoService, 'commitFlash');
const apiPromise = RepoService.commitFiles(payload);
expect(Api.commitMultiple).toHaveBeenCalledWith(projectId, payload);
apiPromise.then(() => {
expect(RepoService.commitFlash).toHaveBeenCalled();
done();
}).catch(done.fail);
});
});
describe('commitFlash', () => {
it('calls Flash with data.message', () => {
const data = {
message: 'message',
};
spyOn(window, 'Flash');
RepoService.commitFlash(data);
expect(window.Flash).toHaveBeenCalledWith(data.message);
});
it('calls Flash with success string if short_id and stats', () => {
const data = {
short_id: 'short_id',
stats: {
additions: '4',
deletions: '5',
},
};
spyOn(window, 'Flash');
RepoService.commitFlash(data);
expect(window.Flash).toHaveBeenCalledWith(`Your changes have been committed. Commit ${data.short_id} with ${data.stats.additions} additions, ${data.stats.deletions} deletions.`, 'notice');
});
});
}); });
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