Commit 31fddefc authored by Chad Woolley's avatar Chad Woolley

Address review feedback

Improve tests and address other feedback
parent e2fb7374
...@@ -461,11 +461,12 @@ const Api = { ...@@ -461,11 +461,12 @@ const Api = {
}, },
issueTemplate(namespacePath, projectPath, key, type, callback) { issueTemplate(namespacePath, projectPath, key, type, callback) {
const url = Api.buildUrl(Api.issuableTemplatePath) const url = this.buildIssueTemplateUrl(
.replace(':key', encodeURIComponent(key)) Api.issuableTemplatePath,
.replace(':type', type) type,
.replace(':project_path', projectPath) projectPath,
.replace(':namespace_path', namespacePath); namespacePath,
).replace(':key', encodeURIComponent(key));
return axios return axios
.get(url) .get(url)
.then(({ data }) => callback(null, data)) .then(({ data }) => callback(null, data))
...@@ -473,16 +474,25 @@ const Api = { ...@@ -473,16 +474,25 @@ const Api = {
}, },
issueTemplates(namespacePath, projectPath, type, callback) { issueTemplates(namespacePath, projectPath, type, callback) {
const url = Api.buildUrl(Api.issuableTemplatesPath) const url = this.buildIssueTemplateUrl(
.replace(':type', type) Api.issuableTemplatesPath,
.replace(':project_path', projectPath) type,
.replace(':namespace_path', namespacePath); projectPath,
namespacePath,
);
return axios return axios
.get(url) .get(url)
.then(({ data }) => callback(null, data)) .then(({ data }) => callback(null, data))
.catch(callback); .catch(callback);
}, },
buildIssueTemplateUrl(path, type, projectPath, namespacePath) {
return Api.buildUrl(path)
.replace(':type', type)
.replace(':project_path', projectPath)
.replace(':namespace_path', namespacePath);
},
users(query, options) { users(query, options) {
const url = Api.buildUrl(this.usersPath); const url = Api.buildUrl(this.usersPath);
return axios.get(url, { return axios.get(url, {
......
...@@ -66,7 +66,8 @@ export default { ...@@ -66,7 +66,8 @@ export default {
this.$refs.modal.hide(); this.$refs.modal.hide();
}, },
initTemplates() { initTemplates() {
Api.issueTemplates(this.namespace, this.project, ISSUABLE_TYPE, (err, templates) => { const { namespace, project } = this;
Api.issueTemplates(namespace, project, ISSUABLE_TYPE, (err, templates) => {
if (err) return; // Error handled by global AJAX error handler if (err) return; // Error handled by global AJAX error handler
this.mergeRequestTemplates = templates; this.mergeRequestTemplates = templates;
}); });
......
...@@ -553,14 +553,15 @@ describe('Api', () => { ...@@ -553,14 +553,15 @@ describe('Api', () => {
}); });
describe('issueTemplate', () => { describe('issueTemplate', () => {
const namespace = 'some namespace';
const project = 'some project';
const templateKey = ' template #%?.key ';
const templateType = 'template type';
const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/templates/${templateType}/${encodeURIComponent(
templateKey,
)}`;
it('fetches an issue template', done => { it('fetches an issue template', done => {
const namespace = 'some namespace';
const project = 'some project';
const templateKey = ' template #%?.key ';
const templateType = 'template type';
const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/templates/${templateType}/${encodeURIComponent(
templateKey,
)}`;
mock.onGet(expectedUrl).reply(httpStatus.OK, 'test'); mock.onGet(expectedUrl).reply(httpStatus.OK, 'test');
Api.issueTemplate(namespace, project, templateKey, templateType, (error, response) => { Api.issueTemplate(namespace, project, templateKey, templateType, (error, response) => {
...@@ -568,14 +569,25 @@ describe('Api', () => { ...@@ -568,14 +569,25 @@ describe('Api', () => {
done(); done();
}); });
}); });
describe('when an error occurs while fetching an issue template', () => {
it('rejects the Promise', () => {
mock.onGet(expectedUrl).replyOnce(httpStatus.INTERNAL_SERVER_ERROR);
Api.issueTemplate(namespace, project, templateKey, templateType, () => {
expect(mock.history.get).toHaveLength(1);
});
});
});
}); });
describe('issueTemplates', () => { describe('issueTemplates', () => {
const namespace = 'some namespace';
const project = 'some project';
const templateType = 'template type';
const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/templates/${templateType}`;
it('fetches all templates by type', done => { it('fetches all templates by type', done => {
const namespace = 'some namespace';
const project = 'some project';
const templateType = 'template type';
const expectedUrl = `${dummyUrlRoot}/${namespace}/${project}/templates/${templateType}`;
const expectedData = [ const expectedData = [
{ key: 'Template1', name: 'Template 1', content: 'This is template 1!' }, { key: 'Template1', name: 'Template 1', content: 'This is template 1!' },
]; ];
...@@ -590,6 +602,16 @@ describe('Api', () => { ...@@ -590,6 +602,16 @@ describe('Api', () => {
done(); done();
}); });
}); });
describe('when an error occurs while fetching issue templates', () => {
it('rejects the Promise', () => {
mock.onGet(expectedUrl).replyOnce(httpStatus.INTERNAL_SERVER_ERROR);
Api.issueTemplates(namespace, project, templateType, () => {
expect(mock.history.get).toHaveLength(1);
});
});
});
}); });
describe('projectTemplates', () => { describe('projectTemplates', () => {
......
...@@ -36,8 +36,9 @@ describe('~/static_site_editor/components/edit_meta_modal.vue', () => { ...@@ -36,8 +36,9 @@ describe('~/static_site_editor/components/edit_meta_modal.vue', () => {
const buildMockAxios = () => { const buildMockAxios = () => {
mockAxios = new MockAdapter(axios); mockAxios = new MockAdapter(axios);
const templatesMergeRequestsPath = `templates/merge_request`;
mockAxios mockAxios
.onGet(`${namespace}/${project}/templates/merge_request`) .onGet(`${namespace}/${project}/${templatesMergeRequestsPath}`)
.reply(200, mergeRequestTemplates); .reply(200, mergeRequestTemplates);
}; };
......
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