Commit 39b48cdf authored by Eric Eastwood's avatar Eric Eastwood
parent 810aa419
import Vue from 'vue';
import vueResource from 'vue-resource';
Vue.use(vueResource);
class RelatedIssuesService {
constructor(endpoint) {
this.relatedIssuesResource = Vue.resource(endpoint);
}
// eslint-disable-next-line class-methods-use-this
fetchIssueInfo(endpoint) {
return Vue.http.get(endpoint)
.then(res => res.json());
}
fetchRelatedIssues() {
return this.relatedIssuesResource.get()
.then(res => res.json());
}
addRelatedIssues(newIssueReferences) {
return this.relatedIssuesResource.save({}, {
issue_references: newIssueReferences,
})
.then(res => res.json());
}
// eslint-disable-next-line class-methods-use-this
removeRelatedIssue(endpoint) {
return Vue.http.delete(endpoint)
.then(res => res.json());
}
}
export default RelatedIssuesService;
import _ from 'underscore';
import Vue from 'vue';
import RelatedIssuesService from '~/issuable/related_issues/services/related_issues_service';
const issuable1 = {
reference: 'foo/bar#123',
title: 'some title',
path: '/foo/bar/issues/123',
state: 'opened',
destroy_relation_path: '/foo/bar/issues/123/related_issues/1',
};
describe('RelatedIssuesService', () => {
let service;
beforeEach(() => {
service = new RelatedIssuesService('');
});
describe('fetchIssueInfo', () => {
const interceptor = (request, next) => {
next(request.respondWith(JSON.stringify(issuable1), {
status: 200,
}));
};
beforeEach(() => {
Vue.http.interceptors.push(interceptor);
});
afterEach(() => {
Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
});
it('fetch issue info', (done) => {
service.fetchIssueInfo('...')
.then((issue) => {
expect(issue).toEqual(issuable1);
done();
})
.catch((err) => {
done.fail(`Failed to fetch issue:\n${err}`);
});
});
});
describe('fetchRelatedIssues', () => {
const interceptor = (request, next) => {
next(request.respondWith(JSON.stringify([issuable1]), {
status: 200,
}));
};
beforeEach(() => {
Vue.http.interceptors.push(interceptor);
});
afterEach(() => {
Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
});
it('fetch related issues', (done) => {
service.fetchRelatedIssues()
.then((relatedIssues) => {
expect(relatedIssues).toEqual([issuable1]);
done();
})
.catch((err) => {
done.fail(`Failed to fetch related issues:\n${err}`);
});
});
});
describe('addRelatedIssues', () => {
const interceptor = (request, next) => {
next(request.respondWith(JSON.stringify({
message: `${issuable1.reference} was successfully related`,
status: 'success',
}), {
status: 200,
}));
};
beforeEach(() => {
Vue.http.interceptors.push(interceptor);
});
afterEach(() => {
Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
});
it('add related issues', (done) => {
service.addRelatedIssues([issuable1.reference])
.then((resData) => {
expect(resData.status).toEqual('success');
done();
})
.catch((err) => {
done.fail(`Failed to add related issues:\n${err}`);
});
});
});
describe('removeRelatedIssue', () => {
const interceptor = (request, next) => {
next(request.respondWith(JSON.stringify({
message: 'Relation was removed',
status: 'success',
}), {
status: 200,
}));
};
beforeEach(() => {
Vue.http.interceptors.push(interceptor);
});
afterEach(() => {
Vue.http.interceptors = _.without(Vue.http.interceptors, interceptor);
});
it('remove related issue', (done) => {
service.removeRelatedIssue('...')
.then((resData) => {
expect(resData.status).toEqual('success');
done();
})
.catch((err) => {
done.fail(`Failed to fetch issue:\n${err}`);
});
});
});
});
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