diff --git a/src/app/article/comment/post-comment.component.spec.ts b/src/app/article/comment/post-comment.component.spec.ts new file mode 100644 index 0000000..0e607a7 --- /dev/null +++ b/src/app/article/comment/post-comment.component.spec.ts @@ -0,0 +1,66 @@ +import {Provider, provide, Component} from 'ng-forward'; +import * as helpers from "../../../spec/helpers"; + +import {PostCommentComponent} from './post-comment.component'; + +const htmlTemplate: string = ''; + +describe("Components", () => { + describe("Post Comment Component", () => { + + beforeEach(angular.mock.module("templates")); + + let commentService = jasmine.createSpyObj("commentService", ["createInArticle"]); + let providers = [ + new Provider('CommentService', { useValue: commentService }), + new Provider('NotificationService', { useValue: helpers.mocks.notificationService }) + ].concat(helpers.provideFilters("translateFilter")); + + @Component({ selector: 'test-container-component', directives: [PostCommentComponent], template: htmlTemplate, providers: providers }) + class ContainerComponent { + article = { id: 1 }; + comment = { id: 2 }; + } + + it("render the post comment form", done => { + helpers.createComponentFromClass(ContainerComponent).then(fixture => { + expect(fixture.debugElement.queryAll("form").length).toEqual(1); + done(); + }); + }); + + it("emit an event when create comment", done => { + helpers.createComponentFromClass(ContainerComponent).then(fixture => { + let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; + commentService.createInArticle = jasmine.createSpy("createInArticle").and.returnValue(helpers.mocks.promiseResultTemplate({ data: {} })); + component["$rootScope"].$emit = jasmine.createSpy("$emit"); + component.save(); + expect(component["$rootScope"].$emit).toHaveBeenCalledWith(PostCommentComponent.EVENT_COMMENT_RECEIVED, jasmine.any(Object)); + done(); + }); + }); + + it("notify success when create comment", done => { + helpers.createComponentFromClass(ContainerComponent).then(fixture => { + let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; + commentService.createInArticle = jasmine.createSpy("createInArticle").and.returnValue(helpers.mocks.promiseResultTemplate({ data: {} })); + component["notificationService"].success = jasmine.createSpy("success"); + component.save(); + expect(component["notificationService"].success).toHaveBeenCalled(); + done(); + }); + }); + + it("set the reply id when reply to a comment", done => { + helpers.createComponentFromClass(ContainerComponent).then(fixture => { + let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; + component.comment = { reply_of_id: null }; + component.replyOf = { id: 10 }; + component.save(); + expect(component.comment.reply_of_id).toEqual(component.replyOf.id); + done(); + }); + }); + + }); +}); diff --git a/src/app/article/comment/post-comment.component.ts b/src/app/article/comment/post-comment.component.ts index f0a1012..8b8543c 100644 --- a/src/app/article/comment/post-comment.component.ts +++ b/src/app/article/comment/post-comment.component.ts @@ -12,14 +12,14 @@ export class PostCommentComponent { public static EVENT_COMMENT_RECEIVED = "comment.received"; @Input() article: noosfero.Article; - comment: noosfero.Comment; - @Input() replyOf: noosfero.Comment; + comment: noosfero.Comment; + constructor(private commentService: CommentService, private notificationService: NotificationService, private $rootScope: ng.IScope) { } save() { - if (this.replyOf) { + if (this.replyOf && this.comment) { this.comment.reply_of_id = this.replyOf.id; } this.commentService.createInArticle(this.article, this.comment).then((result: noosfero.RestResult) => { diff --git a/src/spec/mocks.ts b/src/spec/mocks.ts index d6de7b4..08571d5 100644 --- a/src/spec/mocks.ts +++ b/src/spec/mocks.ts @@ -75,7 +75,7 @@ export var mocks = { }, commentService: { getByArticle: (article: noosfero.Article) => { - return Promise.resolve({data: {}}); + return Promise.resolve({ data: {} }); } }, sessionWithCurrentUser: (user: any) => { @@ -118,6 +118,6 @@ export var mocks = { translate: (text: string) => { return text; } }, notificationService: { - + success: () => { } } }; -- libgit2 0.21.2