comment-paragraph.service.spec.ts
4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import {CommentParagraphService} from "./comment-paragraph.service";
describe("Services", () => {
describe("Comment Paragraph Service", () => {
let $httpBackend: ng.IHttpBackendService;
let commentParagraphService: CommentParagraphService;
beforeEach(angular.mock.module("main", ($translateProvider: angular.translate.ITranslateProvider) => {
$translateProvider.translations('en', {});
}));
beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _CommentParagraphService_: CommentParagraphService) => {
$httpBackend = _$httpBackend_;
commentParagraphService = _CommentParagraphService_;
}));
describe("Succesfull requests", () => {
it("should return paragraph comments by article", (done) => {
let articleId = 1;
$httpBackend.expectGET(`/api/v1/articles/${articleId}/comment_paragraph_plugin/comments?without_reply=true`).respond(200, { comments: [{ body: "comment1" }] });
commentParagraphService.getByArticle(<noosfero.Article>{ id: articleId }).then((result: noosfero.RestResult<noosfero.Comment[]>) => {
expect(result.data).toEqual([{ body: "comment1" }]);
done();
});
$httpBackend.flush();
});
it("should create a paragraph comment", (done) => {
let articleId = 1;
$httpBackend.expectPOST(`/api/v1/articles/${articleId}/comment_paragraph_plugin/comments`).respond(200, { comment: { body: "comment1" } });
commentParagraphService.createInArticle(<noosfero.Article>{ id: articleId }, <noosfero.Comment>{ body: "comment1" }).then((result: noosfero.RestResult<noosfero.Comment>) => {
expect(result.data).toEqual({ body: "comment1" });
done();
});
$httpBackend.flush();
});
it("activate paragraph comments for an article", (done) => {
let articleId = 1;
$httpBackend.expectPOST(`/api/v1/articles/${articleId}/comment_paragraph_plugin/activate`).respond(200, { article: { title: "article1" } });
commentParagraphService.activateCommentParagraph(<noosfero.Article>{ id: articleId }).then((result: noosfero.RestResult<noosfero.Article>) => {
expect(result.data).toEqual({ title: "article1" });
done();
});
$httpBackend.flush();
});
it("deactivate paragraph comments for an article", (done) => {
let articleId = 1;
$httpBackend.expectPOST(`/api/v1/articles/${articleId}/comment_paragraph_plugin/deactivate`).respond(200, { article: { title: "article1" } });
commentParagraphService.deactivateCommentParagraph(<noosfero.Article>{ id: articleId }).then((result: noosfero.RestResult<noosfero.Article>) => {
expect(result.data).toEqual({ title: "article1" });
done();
});
$httpBackend.flush();
});
it("return counts for paragraph comments", (done) => {
let articleId = 1;
$httpBackend.expectGET(`/api/v1/articles/${articleId}/comment_paragraph_plugin/comments/count`).respond(200, { '1': 5, '2': 6 });
commentParagraphService.commentParagraphCount(<noosfero.Article>{ id: articleId }, '1').then((count: number) => {
expect(count).toEqual(5);
done();
});
$httpBackend.flush();
});
it("reset promise when comment paragraph counts fails", (done) => {
let articleId = 1;
commentParagraphService['articleService'] = jasmine.createSpyObj("articleService", ["getElement"]);
commentParagraphService['articleService'].getElement = jasmine.createSpy("getElement").and.returnValue(
{
customGET: (path: string) => {
return Promise.reject({});
}
}
);
commentParagraphService.commentParagraphCount(<noosfero.Article>{ id: articleId }, '1').catch(() => {
expect(commentParagraphService['commentParagraphCountsPromise']).toBeNull();
done();
});
});
});
});
});